首页 文章

获取GraphQL整个架构查询

提问于
浏览
35

我想从服务器获取架构 . 我可以获得所有类型的实体,但我无法获得属性 .

获得所有类型:

query {
  __schema {
    queryType {
      fields {
        name
        type {
          kind
          ofType {
            kind
            name
          }
        }
      }
    }
  }
}

如何获取类型的属性:

__type(name: "Person") {
    kind
    name
    fields {
      name
      type {
        kind
        name
        description
      }
    }
  }

如何只在1个请求中获取具有属性的所有类型?或者更好:我如何通过mutators,enums,types获得整个架构......

8 回答

  • 0

    Update

    现在,使用graphql-cli是获取和更新架构的推荐工作流程 .

    以下命令将帮助您入门:

    # install via NPM
    npm install -g graphql-cli
    
    # Setup your .graphqlconfig file (configure endpoints + schema path)
    graphql init
    
    # Download the schema from the server
    graphql get-schema
    

    您甚至可以通过运行以下命令来监听架构更改并不断更新架构:

    graphql get-schema --watch
    

    如果您只想下载GraphQL架构,请使用以下方法:

    获取GraphQL架构的最简单方法是使用CLI工具get-graphql-schema .

    您可以通过NPM安装它:

    npm install -g get-graphql-schema
    

    有两种方法可以获取架构 . 1)GraphQL IDL格式或2)JSON内省查询格式 .

    GraphQL IDL格式

    get-graphql-schema ENDPOINT_URL > schema.graphql
    

    JSON内省格式

    get-graphql-schema --json ENDPOINT_URL > schema.json
    

    要么

    get-graphql-schema -j ENDPOINT_URL > schema.json
    

    有关更多信息,请参阅以下教程:How to download the GraphQL IDL Schema

  • 48

    这是GraphiQL使用的查询(网络捕获):

    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
                  }
                }
              }
            }
          }
        }
      }
    }
    
  • 1

    您可以使用GraphQL-JS的内省查询来获取有关模式的所有信息:

    import { introspectionQuery } from 'graphql';
    

    如果您只想要类型的信息,可以使用:

    {
        __schema: {
            types: {
                ...fullType
            }
        }
    }
    

    其中使用了内省查询中的以下片段:

    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
                    }
                  }
                }
              }
            }
          }
        }
      }
    `;
    

    如果这看起来很复杂,那是因为字段可以被任意深度包含在nonNulls和Lists中,这意味着从技术上来说,即使上面的查询也不能反映完整的模式,如果你的字段包含超过7层(可能不是这种情况) ) .

    您可以看到introspectionQuery here的源代码 .

  • 0

    您可以使用IntelliJ插件 JS GraphQL 然后IDEA将要求您创建两个文件"graphql.config.json"和"graphql.schema.json"

    然后,您可以编辑“graphql.config.json”以指向本地或远程GraphQL服务器:

    "schema": {
    "README_request" : "To request the schema from a url instead, remove the 'file' JSON property above (and optionally delete the default graphql.schema.json file).",
    "request": {
      "url" : "http://localhost:4000",
      "method" : "POST",
      "README_postIntrospectionQuery" : "Whether to POST an introspectionQuery to the url. If the url always returns the schema JSON, set to false and consider using GET",
      "postIntrospectionQuery" : true,
      "README_options" : "See the 'Options' section at https://github.com/then/then-request",
      "options" : {
        "headers": {
          "user-agent" : "JS GraphQL"
        }
      }
    }
    

    之后,IDEA插件将自动从GraphQL服务器加载模式,并在控制台中显示模式json,如下所示:

    Loaded schema from 'http://localhost:4000': {"data":{"__schema":{"queryType":{"name":"Query"},"mutationType":{"name":"Mutation"},"subscriptionType":null,"types":[{"kind":"OBJECT","name":"Query","description":"","fields":[{"name":"launche
    
  • 0

    如果您想自己做,请阅读以下代码:

    有一个模块化的先进工具“graphql-cli”,考虑一下 . 它使用package“graphql”的buildClientSchema从内省数据构建IDL .graphql文件 .

  • 0

    您可以使用以下命令下载远程GraphQL服务器的模式 . 当命令成功时,您应该在当前工作目录中看到名为 schema.json 的新文件 .

    ~$ npx apollo-cli download-schema $GRAPHQL_URL --output schema.json

  • 9
  • 18

    使用apollo cli

    apollo schema:download --endpoint=http://localhost:4000/graphql schema.json
    

相关问题