Introduction to GraphQL
Learn useful terminology and concepts for using the GitHub GraphQL API.
In this article
GraphQL terminology
The GitHub GraphQL API represents an architectural and conceptual shift from the GitHub REST API. You will likely encounter some new terminology in the GraphQL APIreference docs.
Schema
A schema defines a GraphQL API's type system. It describes the complete set of possible data (objects, fields, relationships, everything) that a client can access. Calls from the client arevalidated andexecuted against the schema. A client can find information about the schema viaintrospection. A schema resides on the GraphQL API server. For more information, seeDiscovering the GraphQL API.
Field
A field is a unit of data you can retrieve from an object. As theofficial GraphQL docs say:"The GraphQL query language is basically about selecting fields on objects."
Theofficial spec also says about fields:
All GraphQL operations must specify their selections down to fields which return scalar values to ensure an unambiguously shaped response.
This means that if you try to return a field that is not a scalar, schema validation will throw an error. You must add nested subfields until all fields return scalars.
Argument
An argument is a set of key-value pairs attached to a specific field. Some fields require an argument.Mutations require an input object as an argument.
Implementation
A GraphQL schema may use the termimplements to define how an object inherits from aninterface.
Here's a contrived example of a schema that defines interfaceX
and objectY
:
interface X{some_field: String!other_field: String!}type Y implements X{some_field: String!other_field: String!new_field: String!}
This means objectY
requires the same fields/arguments/return types that interfaceX
does, while adding new fields specific to objectY
. (The!
means the field is required.)
In the reference docs, you'll find that:
Eachobject lists the interface(s)from which it inherits underImplements.
Eachinterface lists the objectsthat inherit from it underImplementations.
Connection
Connections let you query related objects as part of the same call. With connections, you can use a single GraphQL call where you would have to use multiple calls to a REST API. For more information, seeMigrating from REST to GraphQL.
It's helpful to picture a graph: dots connected by lines. The dots are nodes, the lines are edges. A connection defines a relationship between nodes.
Edge
Edges represent connections between nodes. When you query a connection, you traverse its edges to get to its nodes. Everyedges
field has anode
field and acursor
field. Cursors are used for pagination. For more information, seeUsing pagination in the GraphQL API.
Node
Node is a generic term for an object. You can look up a node directly, or you can access related nodes via a connection. If you specify anode
that does not return ascalar, you must include subfields until all fields return scalars. For information on accessing node IDs via the REST API and using them in GraphQL queries, seeUsing global node IDs.
Discovering the GraphQL API
GraphQL isintrospective. This means you can query a GraphQL schema for details about itself.
Query
__schema
to list all types defined in the schema and get details about each:query{ __schema{ types{ name kind description fields{ name}}}}
Query
__type
to get details about any type:query{ __type(name:"Repository"){ name kind description fields{ name}}}
You can also run anintrospection query of the schema via a
GET
request:curl -H "Authorization: bearer TOKEN" https://api.github.com/graphql
Note
If you get the response
"message": "Bad credentials"
or401 Unauthorized
, check that you are using a valid token. If you receive a403
error withResource not accessible by personal access token
, ensure that your fine-grained personal access token is targeted to the correct resource owner. For example, it must target the organization that owns the repository you are trying to access.The results are in JSON, so we recommend pretty-printing them for easier reading and searching. You can use a command-line tool likejq or pipe the results into
python -m json.tool
for this purpose.Alternatively, you can pass the
idl
media type to return the results in IDL format, which is a condensed version of the schema:$curl -H"Authorization: bearer TOKEN" -H"Accept: application/vnd.github.v4.idl" \https://api.github.com/graphql
Note
The introspection query is probably the only
GET
request you'll run in GraphQL. If you're passing a body, the GraphQL request method isPOST
, whether it's a query or a mutation.For more information about performing queries, seeForming calls with GraphQL.