Skip to content
This repository was archived by the owner on Oct 17, 2020. It is now read-only.

Files

Latest commit

ac439d5 · May 3, 2018

History

History
115 lines (91 loc) · 8.42 KB

02-API-Reference.md

File metadata and controls

115 lines (91 loc) · 8.42 KB

API Reference

GraphQLServer

constructor

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 if schema is not provided).
  • resolvers: Contains resolvers for the fields specified in typeDefs (required if schema is not provided).
  • schema: An instance of GraphQLSchema (required if typeDefs and resolvers 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 the key of each field corresponds to the name of a directive and the value the belonging SchemaDirectiveVisitor. More info here.
  • directiveResolvers: A map of directive resolvers where the key 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 on graphql-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

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: The options object defined below.
  • callback: A function that's invoked right before the server is started. As an example, the callback 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 the PORT 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 to false disables subscriptions entirely.
  • playground (default: '/'): Defines the endpoint where you can invoke the Playground. Setting to false 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 header X-GraphQL-Deduplicate will deduplicate the data.
  • getEndpoint (default: false): Adds a GraphQL HTTP GET endpoint to your server (defaults to endpoint if true) (used for leveraging CDN level caching). Setting to false means the web server only accepts POST requests.
  • bodyParserOptions: Lets you pass through options for the JSON body-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 to true, 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 to true, enable built-in support for Apollo Cache Control.

Example

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.`))

PubSub

See the original documentation in graphql-subscriptions GtiHub repository and in the Apollo Docs.