1
0
Fork 0

Allow passing own graphql-sync module to Foxx

This commit is contained in:
Alan Plum 2017-02-28 14:47:17 +01:00
parent bac8fecad7
commit 705273bdab
No known key found for this signature in database
GPG Key ID: 8ED72A9A323B6EFD
4 changed files with 33 additions and 9 deletions

View File

@ -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)

View File

@ -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

View File

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

View File

@ -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;