Reputation: 287390
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
.
Upvotes: 10
Views: 16050
Reputation: 17354
The apollo
package has been deprecated.
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'
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 gitignor
ed it
Upvotes: 4
Reputation: 84657
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.
Upvotes: 14