Is GitHub API returning an invalid result for the schema query?

Viewed 202

https://facebook.github.io/graphql/draft/#sec-Schema-Introspection

type __Schema {
  types: [__Type!]!
  queryType: __Type!
  mutationType: __Type
  subscriptionType: __Type
  directives: [__Directive!]!
}

type __Type {
  kind: __TypeKind!
  name: String
  description: String
...

Information downloaded from https://developer.github.com/v4/guides/intro-to-graphql/#discovering-the-graphql-api (curl -H "Authorization: bearer token" https://api.github.com/graphql)

(beginning of the file

{
  "data": {
    "__schema": {
      "queryType": {
        "name": "Query"
      },
      "mutationType": {
        "name": "Mutation"
      },
      "subscriptionType": null,
      "types": [
        {
          "kind": "SCALAR",
          "name": "Boolean",
...

Question:

I interpreted this so this GitHub schema result is invalid because queryType doesn't specify a kind which is nonNullable (kind: __TypeKind!)

Is this result violating the schema rules or am I missing something?

1 Answers

This response passes validation because a missing field is not the same thing as a field that returns null. A field will be missing from the response only if it wasn't requested in the first place.

If you go to GitHub's GraphQL Explorer, you can generate an introspection query yourself, request the kind field as part of the selection set of the queryType field and it will return the field with a non-null value.

{
  __schema {
    queryType {
      kind
    }
  }
}

Response:

{
  "data": {
    "__schema": {
      "queryType": {
        "kind": "OBJECT"
      }
    }
  }
}

Fetching the schema by making a GET request to some endpoint is convenient, but it's not the standard way to introspect the schema. Instead, you should make the request using whatever selection set is needed against the endpoint itself. The drawback of doing it this less conventional way is made apparent by this question. In this case, whatever introspection query GitHub is making for you under the hood is missing one or more fields that could otherwise be requested. Because you're not the one making the introspection query, you don't know what to expect in terms of the shape of the response.

Related