From 705273bdabd8fe7eae5beade80e48be0007bc9f5 Mon Sep 17 00:00:00 2001 From: Alan Plum Date: Tue, 28 Feb 2017 14:47:17 +0100 Subject: [PATCH] Allow passing own graphql-sync module to Foxx --- CHANGELOG | 4 +++- Documentation/Books/Manual/Foxx/GraphQL.mdpp | 20 +++++++++++++++++++ .../Manual/ReleaseNotes/NewFeatures32.mdpp | 4 +++- js/server/modules/@arangodb/foxx/graphql.js | 14 ++++++------- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index abfd1b858e..97bd3d4260 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,7 +1,9 @@ devel ----- -* don't let read-only transactions block the WAL collector +* don't let read-only transactions block the WAL collector + +* allow passing own `graphql-sync` module instance to Foxx GraphQL router v3.2.alpha2 (2017-02-20) diff --git a/Documentation/Books/Manual/Foxx/GraphQL.mdpp b/Documentation/Books/Manual/Foxx/GraphQL.mdpp index 70ab959f1c..e79a9afd46 100644 --- a/Documentation/Books/Manual/Foxx/GraphQL.mdpp +++ b/Documentation/Books/Manual/Foxx/GraphQL.mdpp @@ -41,6 +41,22 @@ router.use('/graphql', createGraphQLRouter({ })); ``` +**Note**: ArangoDB aims for stability which means bundled dependencies will generally not be updated as quickly as their maintainers make updates available on GitHub or NPM. Starting with ArangoDB 3.2, if you want to use a newer release of `graphql-sync` than the one bundled with your target version of ArangoDB, you can provide your own version of the library by passing it via the `graphql` option: + +```js +const graphql = require('graphql-sync'); +const graphqlSchema = new graphql.Schema({ + //... +}); +module.context.use(createGraphQLRouter({ + schema: graphqlSchema, + graphiql: true, + graphql: graphql +})) +``` + +This makes sure Foxx uses the `graphql-sync` module bundled in your service's `node_modules` folder (if available) instead of the built-in version. If you find `graphql-sync` itself lagging behind the official `graphql` module, consider [opening an issue on its GitHub repository](https://github.com/arangodb/graphql-sync/issues). + Creating a router ----------------- @@ -82,6 +98,10 @@ This returns a new router object with POST and GET routes for serving GraphQL re If `true`, the [GraphiQL](https://github.com/graphql/graphiql) explorer will 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 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 diff --git a/Documentation/Books/Manual/ReleaseNotes/NewFeatures32.mdpp b/Documentation/Books/Manual/ReleaseNotes/NewFeatures32.mdpp index f7f0da0da0..65c8df5425 100644 --- a/Documentation/Books/Manual/ReleaseNotes/NewFeatures32.mdpp +++ b/Documentation/Books/Manual/ReleaseNotes/NewFeatures32.mdpp @@ -57,4 +57,6 @@ Authentication Foxx ---- -The [cookie session transport](../Foxx/Sessions/Transports/Cookie.md) now supports all options supported by the [cookie method of the response object](../Foxx/Router/Response.md#cookie). \ No newline at end of file +The [cookie session transport](../Foxx/Sessions/Transports/Cookie.md) now supports all options supported by the [cookie method of the response object](../Foxx/Router/Response.md#cookie). + +It's now possible to provide your own version of the `graphql-sync` module when using the [GraphQL extensions for Foxx](../Foxx/GraphQL.md) by passing a copy of the module using the new _graphql_ option. \ No newline at end of file diff --git a/js/server/modules/@arangodb/foxx/graphql.js b/js/server/modules/@arangodb/foxx/graphql.js index a6e0608375..a358f73aef 100644 --- a/js/server/modules/@arangodb/foxx/graphql.js +++ b/js/server/modules/@arangodb/foxx/graphql.js @@ -25,7 +25,7 @@ const dd = require('dedent'); const assert = require('assert'); const joi = require('joi'); -const gql = require('graphql-sync'); +const gqlSync = require('graphql-sync'); const createRouter = require('@arangodb/foxx/router'); const GRAPHIQL_VERSION = '0.7.1'; @@ -33,7 +33,7 @@ const GRAPHIQL_VERSION = '0.7.1'; module.exports = function graphql (cfg) { assert(cfg, 'Must pass options for graphql'); - function getVariables(variables, res) { + function getVariables (variables, res) { if (typeof variables !== 'string') { return variables; } @@ -61,6 +61,7 @@ module.exports = function graphql (cfg) { function handler (req, res) { const options = typeof cfg === 'function' ? cfg(req, res) : cfg; + const gql = options.graphql || gqlSync; const params = typeof req.body === 'string' ? {query: req.body} : req.body || {}; const query = req.queryParams.query || params.query; @@ -91,8 +92,7 @@ module.exports = function graphql (cfg) { res.json(result, options.pretty); } - - function handleRequest() { + function handleRequest () { if (!query) { if (showGraphiQL) { return null; @@ -150,16 +150,16 @@ module.exports = function graphql (cfg) { } }; -function canDisplayGraphiQL(req, params) { +function canDisplayGraphiQL (req, params) { const raw = params.raw !== undefined; return !raw && req.accepts(['json', 'html']) === 'html'; } -function safeSerialize(data) { +function safeSerialize (data) { return data ? JSON.stringify(data).replace(/\//g, '\\/') : null; } -function renderGraphiQL(options) { +function renderGraphiQL (options) { const queryString = options.query; const variablesString = options.variables ? JSON.stringify(options.variables, null, 2) : null; const resultString = options.result ? JSON.stringify(options.result, null, 2) : null;