David Hoerl
David Hoerl

Reputation: 41622

How to request the schema from a GraphQL service using curl?

I have a curl script (as a standin for real code) that can POST to my company's GraphQL endoint and get data. It's working fine.

It appears that it should also be possible to get the "schema" by crafting the appropriate request, but I have not found any way to do that at a HTTP level.

If it is possible, what would the curl look like (the data)?

Are there other requests other than "query" that I can use to gleen other information?

Upvotes: 6

Views: 10578

Answers (1)

possum
possum

Reputation: 2916

The body you need is pretty involved for an introspection query, but I think you're looking for something like this

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

and then you can do

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

Shamefully stolen from https://gist.github.com/martinheld/9fe32b7e2c8fd932599d36e921a2a825

Upvotes: 15

Related Questions