【问题标题】:How to find available operations in GraphQL URL?如何在 GraphQL URL 中找到可用的操作?
【发布时间】:2019-06-03 21:44:51
【问题描述】:

请原谅这种模糊的问题。我有一个网址http://example.com/graphql。我想知道有哪些操作可用。有没有办法查出来?

【问题讨论】:

    标签: python graphql


    【解决方案1】:

    GraphQL 有 introspection system 允许您查询其架构。您可以构造 GraphQL 查询来探索其类型。

    例如,要找出所有查询和变异根字段,您可以将以下 JSON 正文 POST 设置为 http://example.com/graphql,并将内容类型设置为 application/json

    {
       "query" : "{__schema {queryType {name fields {name}}mutationType {name fields {name}}}}"
    }
    

    或者使用GET并将GraphQL查询放到查询参数query

    http://example.com/graphql/?query={__schema {queryType {name fields {name}}mutationType {name fields {name}}}}
    

    这将返回类似的东西:

    {
      "data": {
        "__schema": {
          "queryType": {
            "name": "Query",
            "fields": [
              {
                "name": "searchFoo"
              },
              {
                "name": "searchBar"
              }
    
            ]
          },
          "mutationType": {
            "name": "Mutation",
            "fields": [
              {
                "name": "createFoo"
              },
              {
                "name": "updateFoo"
              }
            ]
          }
        }
      }
    

    但是更简单的方法是使用 GraphQL 客户端,例如 GraphiQLInsomnia 或.....它很可能会为您提供一个交互式 UI 来探索 GraphQL Schema 并具有一些自动完成/帮助您构建 GraphQL 查询的提示功能。

    【讨论】:

    • 更简单的方法是使用已经集成了 GraphQL 游乐场的 Apollo Server。 apollographql.com/docs/apollo-server/features/…
    • @xwlee 我更希望探索现有的 graphQL 设置。
    • 我在 PostMan 中尝试过,它返回了POST body missing. Did you forget use body-parser middleware?
    • @Volatil3 。不知道你的服务器上发生了什么。我只是在这个公共 GraphQL API https://graphql.brandfolder.com/graphql 上使用 Postman 尝试它,它可以工作
    • @Volatil3 。突然我想通了,试试https://www.zillow.com/graphql/?query={__schema%20{queryType%20{name%20fields%20{name}}mutationType%20{name%20fields%20{name}}}}。试了好几次,有的能用,有的没用……
    猜你喜欢
    • 2010-09-30
    • 2017-05-03
    • 2017-03-18
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    相关资源
    最近更新 更多