From 5fc6da1793eaa35d73c6210277b04abef2f16478 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Sat, 22 Sep 2018 14:38:06 +0100 Subject: [PATCH] fix(hooks): ensure hooked options passed to createPostGraphileHttpRequestHandler (#856) --- src/postgraphile/postgraphile.ts | 26 ++++++++++++++------- src/postgraphile/withPostGraphileContext.ts | 4 +++- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/postgraphile/postgraphile.ts b/src/postgraphile/postgraphile.ts index 389cda0793..66107e8f54 100644 --- a/src/postgraphile/postgraphile.ts +++ b/src/postgraphile/postgraphile.ts @@ -11,6 +11,7 @@ import { PostGraphileOptions, mixed, HttpRequestHandler } from '../interfaces'; export interface PostgraphileSchemaBuilder { _emitter: EventEmitter; getGraphQLSchema: () => Promise; + options: PostGraphileOptions; } /** @@ -57,6 +58,7 @@ export function getPostgraphileSchemaBuilder( return { _emitter, getGraphQLSchema: () => Promise.resolve(gqlSchema || gqlSchemaPromise), + options, }; async function createGqlSchema(): Promise { @@ -107,26 +109,28 @@ export default function postgraphile( maybeOptions?: PostGraphileOptions, ): HttpRequestHandler { let schema: string | Array; - let options: PostGraphileOptions; + // These are the raw options we're passed in; getPostgraphileSchemaBuilder + // must process them with `pluginHook` before we can rely on them. + let incomingOptions: PostGraphileOptions; // If the second argument is undefined, use defaults for both `schema` and - // `options`. + // `incomingOptions`. if (typeof schemaOrOptions === 'undefined') { schema = 'public'; - options = {}; + incomingOptions = {}; } // If the second argument is a string or array, it is the schemas so set the // `schema` value and try to use the third argument (or a default) for - // `options`. + // `incomingOptions`. else if (typeof schemaOrOptions === 'string' || Array.isArray(schemaOrOptions)) { schema = schemaOrOptions; - options = maybeOptions || {}; + incomingOptions = maybeOptions || {}; } - // Otherwise the second argument is the options so set `schema` to the - // default and `options` to the second argument. + // Otherwise the second argument is the incomingOptions so set `schema` to the + // default and `incomingOptions` to the second argument. else { schema = 'public'; - options = schemaOrOptions; + incomingOptions = schemaOrOptions; } // Do some things with `poolOrConfig` so that in the end, we actually get a @@ -145,7 +149,11 @@ export default function postgraphile( poolOrConfig || {}, ); - const { getGraphQLSchema, _emitter } = getPostgraphileSchemaBuilder(pgPool, schema, options); + const { getGraphQLSchema, options, _emitter } = getPostgraphileSchemaBuilder( + pgPool, + schema, + incomingOptions, + ); return createPostGraphileHttpRequestHandler({ ...options, getGqlSchema: getGraphQLSchema, diff --git a/src/postgraphile/withPostGraphileContext.ts b/src/postgraphile/withPostGraphileContext.ts index 8b8206c332..7661fefbcd 100644 --- a/src/postgraphile/withPostGraphileContext.ts +++ b/src/postgraphile/withPostGraphileContext.ts @@ -61,7 +61,9 @@ const withDefaultPostGraphileContext: WithPostGraphileContextFn = async ( const definition = queryDocumentAst.definitions[i]; if (definition.kind === Kind.OPERATION_DEFINITION) { if (!operationName && operation) { - throw new Error('Multiple unnamed operations present in GraphQL query.'); + throw new Error( + 'Multiple operations present in GraphQL query, you must specify an `operationName` so we know which one to execute.', + ); } else if (!operationName || (definition.name && definition.name.value === operationName)) { operation = definition; }