Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

graphql

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?

like image 416
David H Avatar asked Mar 02 '23 12:03

David H


1 Answers

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

like image 58
possum Avatar answered Apr 01 '23 03:04

possum