Get GraphQL whole schema query

2020-02-16 07:07发布

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 {
  __schema {
    queryType {
      fields {
        name
        type {
          kind
          ofType {
            kind
            name
          }
        }
      }
    }
  }
}

How to get the properties for type:

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

How can I get all types with the properties in only 1 request? Or ever better: How can I get the whole schema with the mutators, enums, types ...

9条回答
闹够了就滚
2楼-- · 2020-02-16 07:35

You can use IntelliJ plugin JS GraphQL then IDEA will ask you create two files "graphql.config.json" and "graphql.schema.json"

Then you can edit "graphql.config.json" to point to your local or remote GraphQL server:

"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"
    }
  }
}

After that IDEA plugin will auto load schema from GraphQL server and show the schema json in the console like this:

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
查看更多
▲ chillily
3楼-- · 2020-02-16 07:39

You can download a remote GraphQL server's schema with the following command. When the command succeeds, you should see a new file named schema.json in the current working directory.

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

查看更多
你好瞎i
4楼-- · 2020-02-16 07:40

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.

查看更多
登录 后发表回答