Skip to content

Commit

Permalink
use named arguments for buildExecutionContext
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Oct 4, 2021
1 parent 3b5983f commit 92912b4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/execution/__tests__/subscribe-test.ts
Expand Up @@ -411,7 +411,7 @@ describe('Subscription Initialization Phase', () => {
const document = parse('subscription { foo }');
const result = await subscribe({ schema, document });

const exeContext = buildExecutionContext(schema, document);
const exeContext = buildExecutionContext({ schema, document });
expect(await createSourceEventStream(exeContext)).to.deep.equal(result);
return result;
}
Expand Down
55 changes: 24 additions & 31 deletions src/execution/execute.ts
Expand Up @@ -172,30 +172,11 @@ export interface ExecutionArgs {
* a GraphQLError will be thrown immediately explaining the invalid input.
*/
export function execute(args: ExecutionArgs): PromiseOrValue<ExecutionResult> {
const {
schema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
} = args;
// If a valid execution context cannot be created due to incorrect arguments,
// a "Response" with only errors is returned.
let exeContext: ExecutionContext;
try {
exeContext = buildExecutionContext(
schema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
);
exeContext = buildExecutionContext(args);
} catch (error) {
// Note: if buildExecutionContext throws a GraphQLAggregateError, it will
// be of type GraphQLAggregateError<GraphQLError>, but this is checked explicitly.
Expand Down Expand Up @@ -284,17 +265,29 @@ function assertValidExecutionArguments(
*
* @internal
*/
export function buildExecutionContext(
schema: GraphQLSchema,
document: DocumentNode,
rootValue?: unknown,
contextValue?: unknown,
rawVariableValues?: Maybe<{ readonly [variable: string]: unknown }>,
operationName?: Maybe<string>,
fieldResolver?: Maybe<GraphQLFieldResolver<unknown, unknown>>,
typeResolver?: Maybe<GraphQLTypeResolver<unknown, unknown>>,
subscribeFieldResolver?: Maybe<GraphQLFieldResolver<unknown, unknown>>,
): ExecutionContext {
export function buildExecutionContext(args: {
schema: GraphQLSchema;
document: DocumentNode;
rootValue?: unknown;
contextValue?: unknown;
variableValues?: Maybe<{ readonly [variable: string]: unknown }>;
operationName?: Maybe<string>;
fieldResolver?: Maybe<GraphQLFieldResolver<unknown, unknown>>;
typeResolver?: Maybe<GraphQLTypeResolver<unknown, unknown>>;
subscribeFieldResolver?: Maybe<GraphQLFieldResolver<unknown, unknown>>;
}): ExecutionContext {
const {
schema,
document,
rootValue,
contextValue,
variableValues: rawVariableValues,
operationName,
fieldResolver,
typeResolver,
subscribeFieldResolver,
} = args;

// If arguments are missing or incorrect, throw an error.
assertValidExecutionArguments(schema, document, rawVariableValues);

Expand Down
23 changes: 1 addition & 22 deletions src/execution/subscribe.ts
Expand Up @@ -49,32 +49,11 @@ export interface SubscriptionArgs {
export async function subscribe(
args: SubscriptionArgs,
): Promise<AsyncGenerator<ExecutionResult, void, void> | ExecutionResult> {
const {
schema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
subscribeFieldResolver,
} = args;
// If a valid execution context cannot be created due to incorrect arguments,
// a "Response" with only errors is returned.
let exeContext: ExecutionContext;
try {
exeContext = buildExecutionContext(
schema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
subscribeFieldResolver,
);
exeContext = buildExecutionContext(args);
} catch (error) {
// Note: if buildExecutionContext throws a GraphQLAggregateError, it will
// be of type GraphQLAggregateError<GraphQLError>, but this is checked explicitly.
Expand Down

0 comments on commit 92912b4

Please sign in to comment.