Get GraphQL whole schema query

后端 未结 10 1094
花落未央
花落未央 2020-12-22 23:23

I want to get the schema from the server. I can get all entities with the types but I\'m unable to get the properties.

Getting all types:

query {
  _         


        
相关标签:
10条回答
  • 2020-12-23 00:03

    Refer to https://stackoverflow.com/a/42010467/10189759

    Would like to point out that if authentications are needed, that you probably cannot just use the config file generated from graphql init

    You might have to do something like this, for example, using the github graphql API

    {
      "projects": {
        "graphqlProjectTestingGraphql": {
          "schemaPath": "schema.graphql",
          "extensions": {
            "endpoints": {
              "dev": {
                "url": "https://api.github.com/graphql",
                "headers": {
                  "Authorization": "Bearer <Your token here>"
                }
              }
            }
          }
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-23 00:05

    This is the query that GraphiQL uses (network capture):

    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
                  }
                }
              }
            }
          }
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-23 00:07

    using apollo cli

    apollo schema:download --endpoint=http://localhost:4000/graphql schema.json
    
    0 讨论(0)
  • 2020-12-23 00:10

    You could use apollo codegen:client. See https://github.com/apollographql/apollo-tooling#apollo-clientcodegen-output

    0 讨论(0)
  • 2020-12-23 00:17

    You can use GraphQL-JS's introspection query to get everything you'd like to know about the schema:

    import { introspectionQuery } from 'graphql';
    

    If you want just the information for types, you can use this:

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

    Which uses the following fragment from the introspection query:

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

    If that seems complicated, it's because fields can be arbitrarility deeply wrapped in nonNulls and Lists, which means that technically even the query above does not reflect the full schema if your fields are wrapped in more than 7 layers (which probably isn't the case).

    You can see the source code for introspectionQuery here.

    0 讨论(0)
  • 2020-12-23 00:19

    If you want to do it by your self, read these code:

    There is a modular state-of-art tool 「graphql-cli」, consider looking at it. It uses package 「graphql」's buildClientSchema to build IDL .graphql file from introspection data.

    • graphql-cli get-schema :integrated into graphql-cli part 1
    • graphql-config EndpointsExtension :integrated into graphql-cli part 2
    0 讨论(0)
提交回复
热议问题