Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hooks): ensure hooked options passed to createPostGraphileHttpRequestHandler #856

Merged
merged 2 commits into from
Sep 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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