3

I'm writing a GraphQL interface between GraphQL and REST services using Apollo Server. This interface will provide a single GraphQL endpoint for an Apollo Client front.

The single service at the moment is a KeystoneJS app which provides a GraphQL endpoint, through (as far as I know) Apollo Server. To keep things simple for the moment, I'm downloading the GraphQL schema from the KeystoneJS server GraphQL Playground and using it as my interface's GraphQL schema (after removing definitions that Keystone generates and Apollo Server doesn't understand).

I'd like to automate this process -- that is, somehow 'grab' the GraphQL schema that KeystoneJS/Apollo Server generates, just as if I were to download it from the GraphQL Playground. Is there a way to do this from the endpoint? (I don't want to touch the KeystoneJS internals, just access the schema through the endpoint)

Cerulean
  • 5,543
  • 9
  • 59
  • 111
  • 1
    If you're currently using GraphQL Playground to run an introspection query, is there any reason you can't just make a request to the KeystoneJS endpoint with the same query using an HTTP library of your choice? – Daniel Rearden Feb 28 '20 at 12:50
  • @DanielRearden -- I had thought about that. I'm new to GraphQL so haven't used introspections before. As I see it from https://graphql.org/learn/introspection/, one can get JSON that gives you information about specific aspects of the schema. I suppose I could use that to reconstruct the SDL of the complete schema, but I am working with a large schema -- at present 820 lines, and it will grow -- with lots of nested types. Is there a way to simply request the entire schema through introspections and quickly/easily convert it to SDL? Is it easy enough to do using introspections? – Cerulean Mar 02 '20 at 09:09
  • My bad -- I forgot Playground had a "download" button for the schema so I misunderstood you when you said you were downloading the schema via Playground. I assumed you were already using introspection to do so. – Daniel Rearden Mar 02 '20 at 14:07

1 Answers1

8

You can run an introspection query against any GraphQL service (assuming they haven't disabled introspection -- some services do that in production). The result of this query can be passed to buildClientSchema to reconstruct the schema from the introspection result (without any of the field resolution logic, naturally). You can then convert the returned GraphQLSchema object into SDL by calling printSchema.

Here's an example using axios, but you can use any http library you like to make the request:

const { buildClientSchema, getIntrospectionQuery, printSchema } = require('graphql')
const axios = require('axios')

const ENDPOINT_URL = "";

(async () => {
    const res = await axios.post(ENDPOINT_URL, { query: getIntrospectionQuery() })
    const schema = buildClientSchema(res.data.data)
    const sdl = printSchema(schema)
    console.log(sdl)
})()

Note: the first "data" is what the axios API calls the actual JSON response from the server, while the second "data" is the actual "data" property, which is what we want to pass to buildClientSchema.

ThomasP1988
  • 4,597
  • 3
  • 30
  • 36
Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • 1
    FWIW, I imagine instead of doing the request with axios, you can also use [executeQuery](https://www.keystonejs.com/keystonejs/keystone/#executequeryquerystring-config) if you have access to the Keystone instance. – Daniel Rearden Mar 02 '20 at 16:15
  • This would be in a separate application, so unless I exposed it through the Keystone API, I wouldn't.... – Cerulean Mar 02 '20 at 16:49