GraphQL integration

const createGraphQLRouter = require('@arangodb/foxx/graphql');

Foxx bundles version 0.6 of thegraphql-sync module, which is asynchronous wrapper for the official JavaScript GraphQL referenceimplementation, to allow writing GraphQL schemas directly inside Foxx.

Additionally the @arangodb/foxx/graphql lets you create routers for servingGraphQL requests, which closely mimicks the behavior of theexpress-graphql module.

For more information on graphql-sync see thegraphql-js API reference(note that graphql-sync never wraps results in promises).

For an example of a GraphQL schema in Foxx that resolves fields using thedatabase see the GraphQL example service(also available from the Foxx store).

Examples

  1. const graphql = require('graphql-sync');
  2. const graphqlSchema = new graphql.GraphQLSchema({
  3. // ...
  4. });
  5. // Mounting a graphql endpoint directly in a service:
  6. module.context.use('/graphql', createGraphQLRouter({
  7. schema: graphqlSchema,
  8. graphiql: true
  9. }));
  10. // Or at the service's root URL:
  11. module.context.use(createGraphQLRouter({
  12. schema: graphqlSchema,
  13. graphiql: true
  14. }));
  15. // Or inside an existing router:
  16. router.get('/hello', function (req, res) {
  17. res.write('Hello world!');
  18. });
  19. router.use('/graphql', createGraphQLRouter({
  20. schema: graphqlSchema,
  21. graphiql: true
  22. }));

Note: ArangoDB aims for stability which means bundled dependencies willgenerally not be updated as quickly as their maintainers make updatesavailable on GitHub or NPM. Starting with ArangoDB 3.2, if you want to use anewer version than the one bundled with your target version of ArangoDB, youcan provide your own version of the library by passing it via the graphql option:

  1. const graphql = require('graphql-sync');
  2. const graphqlSchema = new graphql.Schema({
  3. //...
  4. });
  5. module.context.use(createGraphQLRouter({
  6. schema: graphqlSchema,
  7. graphiql: true,
  8. graphql: graphql
  9. }))

Starting with graphql 0.12 you can also usethe official graphql library if youinclude it in the node_modules folder of your service bundle:

  1. const graphql = require('graphql'); // 0.12 or later
  2. const graphqlSchema = new graphql.Schema({
  3. //...
  4. });
  5. module.context.use(createGraphQLRouter({
  6. schema: graphqlSchema,
  7. graphiql: true,
  8. graphql: graphql
  9. }))

Creating a router

createGraphQLRouter(options): Router

This returns a new router object with POST and GET routes for serving GraphQL requests.

Arguments

  • options: object

An object with any of the following properties:

  • schema: GraphQLSchema

A GraphQL Schema object from graphql-sync.

  • context: any (optional)

The GraphQL context that will be passed to the graphql() function fromgraphql-sync to handle GraphQL queries.

  • rootValue: object (optional)

The GraphQL root value that will be passed to the graphql() functionfrom graphql-sync to handle GraphQL queries.

  • pretty: boolean (Default: false)

If true, JSON responses will be pretty-printed.

  • formatError: Function (optional)

A function that will be used to format errors produced by graphql-sync.If omitted, the formatError function from graphql-sync will be used instead.

  • validationRules: Array<any> (optional)

Additional validation rules queries must satisfy in addition to thosedefined in the GraphQL spec.

  • graphiql: boolean (Default: false)

If true, the GraphiQL explorerwill be served when loaded directly from a browser.

  • graphql: object (optional)

If you need to use your own copy of the graphql-sync module instead ofthe one bundled with ArangoDB, here you can pass it in directly.

If a GraphQL Schema object is passed instead of an options object it will beinterpreted as the schema option.

Generated routes

The router handles GET and POST requests to its root path and accepts thefollowing parameters, which can be provided either as query parameters oras the POST request body:

  • query: string

A GraphQL query that will be executed.

  • variables: object | string (optional)

An object or a string containing a JSON object with runtime values to usefor any GraphQL query variables.

  • operationName: string (optional)

If the provided query contains multiple named operations, this specifieswhich operation should be executed.

  • raw: boolean (Default: false)

Forces a JSON response even if graphiql is enabled and the request wasmade using a browser.

The POST request body can be provided as JSON or as query string usingapplication/x-www-form-urlencoded. A request body passed asapplication/graphql will be interpreted as the query parameter.