Skip to content
This repository has been archived by the owner on Jun 24, 2021. It is now read-only.

Commit

Permalink
Add cf-graphql version extension
Browse files Browse the repository at this point in the history
  • Loading branch information
jelz committed May 8, 2017
1 parent a83a070 commit 105cab5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dev/server.js
Expand Up @@ -25,7 +25,7 @@ client.getContentTypes()
const ui = cfGraphql.helpers.graphiql({title: 'cf-graphql dev server'});
app.get('/', (_, res) => res.set(ui.headers).status(ui.statusCode).end(ui.body));

const opts = {timeline: true, detailedErrors: false};
const opts = {version: true, timeline: true, detailedErrors: false};
const ext = cfGraphql.helpers.expressGraphqlExtension(client, schema, opts);
app.use('/graphql', graphqlHTTP(ext));

Expand Down
31 changes: 26 additions & 5 deletions src/helpers/express-graphql-extension.js
Expand Up @@ -2,7 +2,10 @@

// result of a `createExtension` call can be passed to express-graphql
//
// options available: "timeline" and "detailedErrors" (both default to false)
// options available (all default to false):
// - timeline: includes timing of HTTP calls
// - detailedErrors: includes stacks of logged exceptions
// - version: includes cf-graphql's version
//
// timeline extension and detailed errors are nice for development, but most
// likely you want to skip them in your production setup
Expand All @@ -18,18 +21,36 @@ function createExtension (client, schema, options = {}) {
context: {entryLoader},
schema,
graphiql: false,
extensions: options.timeline ? extensions : undefined,
extensions: prepareExtensions(start, options, entryLoader),
formatError: options.detailedErrors ? formatError : undefined
};
};
}

function prepareExtensions (start, options, entryLoader) {
if (!options.version && !options.timeline) {
return;
}

return () => {
const extensions = [];

function extensions () {
return {
if (options.version) {
extensions.push({
'cf-graphql': {version: require('../../package.json').version}
});
}

if (options.timeline) {
extensions.push({
time: Date.now()-start,
timeline: entryLoader.getTimeline().map(httpCall => {
return Object.assign({}, httpCall, {start: httpCall.start-start});
})
};
});
}

return Object.assign({}, ...extensions);
};
}

Expand Down
11 changes: 11 additions & 0 deletions test/helpers/express-graphql-extension.test.js
Expand Up @@ -23,6 +23,17 @@ test('express-graphql-extension: default options', function (t) {
t.end();
});

test('express-graphql-extension: cf-graphql version extension', function (t) {
const extension = createExtension(client, schema, {version: true});
const extensions = extension().extensions();

t.deepEqual(extensions['cf-graphql'], {
version: require('../../package.json').version
});

t.end();
});

test('express-graphql-extension: timeline extension', function (t) {
const extension = createExtension(client, schema, {timeline: true});
const extensions = extension().extensions();
Expand Down

0 comments on commit 105cab5

Please sign in to comment.