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条回答
ゆ 、 Hurt°
2楼-- · 2020-02-16 07:16

using apollo cli

apollo schema:download --endpoint=http://localhost:4000/graphql schema.json
查看更多
Fickle 薄情
4楼-- · 2020-02-16 07:20

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
              }
            }
          }
        }
      }
    }
  }
}
查看更多
Evening l夕情丶
5楼-- · 2020-02-16 07:20

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>"
            }
          }
        }
      }
    }
  }
}
查看更多
闹够了就滚
6楼-- · 2020-02-16 07:22

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.

查看更多
看我几分像从前
7楼-- · 2020-02-16 07:34

Update

Using graphql-cli is now the recommended workflow to get and update your schema.

The following commands will get you started:

# 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

You can even listen for schema changes and continuously update your schema by running:

graphql get-schema --watch

In case you just want to download the GraphQL schema, use the following approach:

The easiest way to get a GraphQL schema is using the CLI tool get-graphql-schema.

You can install it via NPM:

npm install -g get-graphql-schema

There are two ways to get your schema. 1) GraphQL IDL format or 2) JSON introspection query format.

GraphQL IDL format

get-graphql-schema ENDPOINT_URL > schema.graphql

JSON introspection format

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

or

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

For more information you can refer to the following tutorial: How to download the GraphQL IDL Schema

查看更多
登录 后发表回答