9

When using Apollo Server to write a GraphQL server, how can I run a command on the server to generate the schema.graphql file for the client to consume? Note: I'm not using the Apollo Client, I'm using Relay.

I know I can run the GraphQL playground and download it from there, but I want a command line that I can automate.

I'm searching for something similar to rake graphql:schema:dump when using GraphQL Ruby which you can run on the server to generate the schema.graphql.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622

2 Answers2

13

You can use Apollo CLI for that. First install it:

npm install -g apollo

Then run this command as shown in the docs:

apollo client:download-schema --endpoint=URL_OF_YOUR_ENDPOINT schema.graphql

The command will generate either the introspection result or the schema in SDL depending on the extension used for the output file.

Your ApolloServer instance does not expose the schema it creates, but you can also run an introspection query directly against the instance:

const { getIntrospectionQuery, buildClientSchema, printSchema } = require('graphql')
const { ApolloServer } = require('apollo-server')

const apollo = new ApolloServer({ ... })
const { data } = await apollo.executeOperation({ query: getIntrospectionQuery() })
const schema = buildClientSchema(data)
console.log(printSchema(schema))

If you're passing an existing GraphQLSchema instance to Apollo Server, you can also just call printSchema on it directly.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • I'm not using Apollo client... if the server is running, isn't that barely a curl/wget? – Pablo Fernandez Jun 20 '20 at 11:21
  • I'm not sure how what client you're using factors into this. You asked for a CLI command to run to get a server's schema in SDL and this is one way of doing that. You can't just use curl for that because that would only return the introspection result. You need a way to turn that into a GraphQLSchema object and then print it. There's other ways of doing that, but you specifically asked for a CLI command. – Daniel Rearden Jun 20 '20 at 11:34
  • I was after a CLI command on the server that would generate the `schema.graphql` from the defined types, without needing to download anything. I updated my question to specify this. I'm sorry. – Pablo Fernandez Jun 20 '20 at 11:42
  • See edit. If you want to run that from the command line, you'll need to turn it into an npm script. – Daniel Rearden Jun 20 '20 at 14:54
  • Does this work for federated schemas? – Max Coplan Jan 05 '22 at 22:18
  • Suggested package has been deprecated – Vladyslav Zavalykhatko Nov 15 '22 at 13:48
3

The apollo package has been deprecated.

TLDR;

You can try to use @graphql-codegen/schema-ast and generate a schema to a separate file with the config:

generates:
  src/@generated/graphql.ts:
    # TS definitions, if any
  src/@generated/schema.graphql:
    plugins:
      - 'schema-ast'

Longer answer in case you struggle with a similar problem:

Our setup consists of @apollo/client and @graphql-codegen/cli. The latter one works with apollo studio schema (the codegen.yml supports it).

The problem we were trying to resolve was integrating an eslint plugin, that doesn't support Apollo schema.

We ended up downloading and generating the schema locally using the codegen plugin, and then referencing it in eslint. Also we gitignored it

Vladyslav Zavalykhatko
  • 15,202
  • 8
  • 65
  • 100