Skip to content

Commit

Permalink
fix(hooks): ensure hooked options passed to createPostGraphileHttpReq…
Browse files Browse the repository at this point in the history
…uestHandler (#856)
  • Loading branch information
benjie committed Sep 22, 2018
1 parent a5b7a89 commit 5fc6da1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
26 changes: 17 additions & 9 deletions src/postgraphile/postgraphile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { PostGraphileOptions, mixed, HttpRequestHandler } from '../interfaces';
export interface PostgraphileSchemaBuilder {
_emitter: EventEmitter;
getGraphQLSchema: () => Promise<GraphQLSchema>;
options: PostGraphileOptions;
}

/**
Expand Down Expand Up @@ -57,6 +58,7 @@ export function getPostgraphileSchemaBuilder(
return {
_emitter,
getGraphQLSchema: () => Promise.resolve(gqlSchema || gqlSchemaPromise),
options,
};

async function createGqlSchema(): Promise<GraphQLSchema> {
Expand Down Expand Up @@ -107,26 +109,28 @@ export default function postgraphile(
maybeOptions?: PostGraphileOptions,
): HttpRequestHandler {
let schema: string | Array<string>;
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
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/postgraphile/withPostGraphileContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 5fc6da1

Please sign in to comment.