Generate schema.json with GraphiQL or GraphQL endpoint

csm232s picture csm232s · Nov 3, 2015 · Viewed 8.9k times · Source

I'm having issues creating a schema.json file that can be parsed with babel-relay-plugin without running into an error. Taking a look at the schema.json file included in relay's example folder, I tried to copy the query in GraphiQL but I can't seem to get it right. I'm using Laravel as the backend. Is this something I can accomplish through GraphiQL or sending a request to the GraphQL endpoint and saving the response?

The error occurring when attempting to parse the schema.json file:

Cannot read property 'reduce' of undefined while parsing file: /Users/username/Sites/Homestead/Code/ineedmg-graphql/resources/assets/js/app.js

Last attempt using GraphiQL:

{
  __schema {
    queryType { 
      name
    },
    types {
        kind,
        name,
        description,
        fields {
            name,
            description,
            type {
              name,
              kind,
              ofType {
                name
                description
              }
            }
            isDeprecated,
            deprecationReason,
        },
      inputFields {
        name
        description
      }
      interfaces {
        kind
        name
        description
      },
      enumValues {
        name
        description
        isDeprecated
        deprecationReason
      }
    },
    mutationType { 
      name
    },
    directives {
      name,
      description,
      onOperation,
      onFragment,
      onField,
      args {
        name
        description
        defaultValue
      }
    }
  }
}

Answer

taion picture taion · Nov 5, 2015

Yup! If you take a look at the examples, you can see that they generate the schema.json file by executing a query: https://github.com/relayjs/relay-starter-kit/blob/84da9351d100f97e6ed4f08bc70a893779e61c29/scripts/updateSchema.js#L11

This query is in fact just this specific query from graphql-js: https://github.com/graphql/graphql-js/blob/v0.4.12/src/utilities/introspectionQuery.js#L11-L89.