GraphQL integration
The @arangodb/foxx/graphql
module lets you create routers for serving GraphQL requests, which closely mimics the behavior of the express-graphql
module.
For an example of a GraphQL schema in Foxx that resolves fields using the database see the (also available from the Foxx store).
Starting with graphql
version 0.12 you can use the official graphql library if you include it in the node_modules
folder of your service bundle and pass it to the graphql
option:
Foxx also bundles version 0.6 of the , which is a synchronous wrapper for the official JavaScript GraphQL reference implementation.
graphql-sync
is a thin wrapper for old versions of graphql
, allowing it to run in ArangoDB. This GraphQL server/schema implementation is deprecated and only shipped for backward compatibility. Version 0.12 and newer of the official graphql
package can be used directly. New projects should bundle their own copy of this module: https://www.npmjs.com/package/graphql
Examples
const graphql = require('graphql-sync');
const graphqlSchema = new graphql.GraphQLSchema({
// ...
});
// Mounting a graphql endpoint directly in a service:
module.context.use('/graphql', createGraphQLRouter({
schema: graphqlSchema,
graphiql: true
}));
// Or at the service's root URL:
module.context.use(createGraphQLRouter({
graphiql: true
}));
// Or inside an existing router:
router.get('/hello', function (req, res) {
res.write('Hello world!');
});
router.use('/graphql', createGraphQLRouter({
schema: graphqlSchema,
}));
For more information on graphql-sync
see the .
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:
-
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()
function from 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, theformatError
function fromgraphql-sync
will be used instead.validationRules:
Array<any>
(optional)Additional validation rules queries must satisfy in addition to those defined in the GraphQL spec.
graphiql:
boolean
(Default:false
)graphql:
object
(optional)If you need to use your own copy of the
graphql-sync
module instead of the 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 be interpreted as the schema option.
Generated routes
The router handles GET and POST requests to its root path and accepts the following parameters, which can be provided either as query parameters or as 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 use for any GraphQL query variables.
raw:
boolean
(Default:false
)Forces a JSON response even if graphiql is enabled and the request was made using a browser.