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
.
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.
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)".
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With