【问题标题】:How to request the schema from a GraphQL service using curl?如何使用 curl 从 GraphQL 服务请求模式?
【发布时间】:2021-02-23 19:45:01
【问题描述】:

我有一个 curl 脚本(作为真实代码的替代),它可以 POST 到我公司的 GraphQL endoint 并获取数据。它工作正常。

似乎也应该可以通过制作适当的请求来获取“模式”,但我还没有找到任何在 HTTP 级别上做到这一点的方法。

如果可能,卷曲会是什么样子(数据)?

除了“查询”之外,还有其他请求可以用来收集其他信息吗?

【问题讨论】:

    标签: graphql


    【解决方案1】:

    您需要的主体与自省查询有关,但我认为您正在寻找类似的东西

    introspection_query.json:

    { 
      "query": "query IntrospectionQuery {
          __schema {
            queryType { name }
            mutationType { name }
            subscriptionType { name }
            types {
              ...FullType
            }
            directives {
              name
              description
              locations
              args {
                ...InputValue
              }
            }
          }
        }
    
        fragment FullType on __Type {
          kind
          name
          description
          fields(includeDeprecated: true) {
            name
            description
            args {
              ...InputValue
            }
            type {
              ...TypeRef
            }
            isDeprecated
            deprecationReason
          }
          inputFields {
            ...InputValue
          }
          interfaces {
            ...TypeRef
          }
          enumValues(includeDeprecated: true) {
            name
            description
            isDeprecated
            deprecationReason
          }
          possibleTypes {
            ...TypeRef
          }
        }
    
        fragment InputValue on __InputValue {
          name
          description
          type { ...TypeRef }
          defaultValue
        }
    
        fragment TypeRef on __Type {
          kind
          name
          ofType {
            kind
            name
            ofType {
              kind
              name
              ofType {
                kind
                name
                ofType {
                  kind
                  name
                  ofType {
                    kind
                    name
                    ofType {
                      kind
                      name
                      ofType {
                        kind
                        name
                      }
                    }
                  }
                }
              }
            }
          }
        }"
    }
    

    然后你就可以了

    curl -i -X POST http://localhost:8080/graphql -H "Content-Type: application/json" -d @introspection_query.json
    

    可耻地从 https://gist.github.com/martinheld/9fe32b7e2c8fd932599d36e921a2a825

    【讨论】:

    • 像这样的自省查询是从 GraphQL 服务本身获取模式的 JSON 表示的唯一方法。但是如果你想用你选择的编程语言把它变成一个实际的模式对象,那么可能有一个函数可以做到这一点。例如,JavaScript 参考实现有this
    • 我很高兴你偷了这个!我自己永远不会找到它,我也从未想过要在 GraphQL 和内省上搜索(面对面!)。
    • 我刚刚在我的 API 上阅读了这篇文章,它成功了!!!对任何对此感兴趣的人的请求:请访问上面的 GitHub 链接并为要点加注星标。真的,在我添加一颗星之前没有一颗星。
    猜你喜欢
    • 2018-06-10
    • 2015-07-27
    • 2020-10-23
    • 2019-06-22
    • 1970-01-01
    • 2011-11-01
    • 2018-08-30
    • 1970-01-01
    • 2021-06-21
    相关资源
    最近更新 更多