constructor(props: Props): GraphQLServer
The props
argument accepts the following keys:
typeDefs
: Contains GraphQL type definitions in SDL or file path to type definitions (required ifschema
is not provided).resolvers
: Contains resolvers for the fields specified intypeDefs
(required ifschema
is not provided).schema
: An instance ofGraphQLSchema
(required iftypeDefs
andresolvers
are not provided).context
: Contains custom data being passed through your resolver chain. This can be passed in as an object, or as a function with the signature(req: Request) => any
.schemaDirectives
: A map of schema directives where thekey
of each field corresponds to the name of a directive and the value the belongingSchemaDirectiveVisitor
. More info here.directiveResolvers
: A map of directive resolvers where thekey
of each field corresponds to the name of a directive and the value the belonging directive resolver. More info here.middlwares
: A list of middleware functions that are based ongraphql-middleware
.resolverValidationOptions
: A list of validation options for your resolvers. More info here.
Here is simple example of using the GraphQLServer
constructor:
const typeDefs = `
type Query {
hello(name: String): String!
}
`
const resolvers = {
Query: {
hello: (_, { name }) => `Hello ${name || 'World'}`,
},
}
const server = new GraphQLServer({ typeDefs, resolvers })
start(options: Options, callback: ((options: Options) => void) = (() => null)): Promise<void>
Once your GraphQLServer
is instantiated, you can call the start
method on it. It takes two arguments:
options
: Theoptions
object defined below.callback
: A function that's invoked right before the server is started. As an example, thecallback
can be used to print information that the server was now started.
The options
argument accepts the following keys:
port
(default:4000
): Determines the port your server will be listening on (note that you can also specify the port by setting thePORT
environment variable).cors
: Contains configuration options for cors.uploads
: Lets you specify maximum file capacities. More info here.endpoint
(default:'/'
): Defines the HTTP endpoint of your server.subscriptions
(default:'/'
): Defines the subscriptions (websocket) endpoint of your server. Setting tofalse
disables subscriptions entirely.playground
(default:'/'
): Defines the endpoint where you can invoke the Playground. Setting tofalse
disables the Playground endpoint entirely.https
: Options for securing your web server via HTTPS. More info here.deduplicator
(default:true
): Enables graphql-deduplicator. Once enabled sending the headerX-GraphQL-Deduplicate
will deduplicate the data.getEndpoint
(default:false
): Adds a GraphQL HTTP GET endpoint to your server (defaults toendpoint
iftrue
) (used for leveraging CDN level caching). Setting tofalse
means the web server only accepts POST requests.bodyParserOptions
: Lets you pass through options for the JSONbody-parser
used by Express. More info here.
Note that the options
argument also exposes a number of fields from apollo-server
. Here is an overview of the keys of the inherited fields (source: Apollo Server docs):
rootValue
: The value passed to the first resolve function.formatError
: A function to apply to every error before sending the response to clients.validationRules
: Additional GraphQL validation rules to be applied to client-specified queries.formatParams
: A function applied for each query in a batch to format parameters before execution.formatResponse
: A function applied to each response after execution.tracing
: When set totrue
, collect and expose trace data in the Apollo Tracing format.logFunction
: A function called for logging events such as execution times.fieldResolver
: A custom default field resolver.debug
: A boolean that will print additional debug logging if execution errors occur.cacheControl
: When set totrue
, enable built-in support for Apollo Cache Control.
const options = {
port: 8000,
endpoint: '/graphql',
subscriptions: '/subscriptions',
playground: '/playground',
}
server.start(options, ({ port }) => console.log(`Server started, listening on port ${port} for incoming requests.`))
See the original documentation in graphql-subscriptions
GtiHub repository and in the Apollo Docs.