Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate the schema.graphql file when using Apollo Server?

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.

like image 497
pupeno Avatar asked Jun 20 '20 07:06

pupeno


People also ask

How do I get schema on Apollo server?

Download your server's schemaExpand the Apollo phase. Paste the Swift Package Manager Run Script from Add a code generation build step into the text area. This script uses your schema to generate the code that the Apollo iOS SDK uses to interact with your server.

How do I get all schema in GraphQL?

You have to set field schemaPath to non-existing file (it will be auto created after downloading schema) and url to GraphQL remote server. Next, in tab "Schemas and project structure" (in "GraphQL" tab), double click on selected "Endpoint" and click "Get GraphQL Schema from Endpoint (introspection)".

Does GraphQL have a schema?

Your GraphQL server uses a schema to describe the shape of your available data. This schema defines a hierarchy of types with fields that are populated from your back-end data stores. The schema also specifies exactly which queries and mutations are available for clients to execute.


1 Answers

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.

like image 56
Daniel Rearden Avatar answered Oct 31 '22 18:10

Daniel Rearden