Skip to content

Commit

Permalink
Simplify args handling in 'graphql', 'subscribe' and 'execute' funcs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jun 2, 2019
1 parent 376f1fd commit e876216
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 83 deletions.
39 changes: 16 additions & 23 deletions src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,38 +167,31 @@ export function execute(
/* eslint-enable no-redeclare */
// Extract arguments from object args if provided.
return arguments.length === 1
? executeImpl(
argsOrSchema.schema,
argsOrSchema.document,
argsOrSchema.rootValue,
argsOrSchema.contextValue,
argsOrSchema.variableValues,
argsOrSchema.operationName,
argsOrSchema.fieldResolver,
argsOrSchema.typeResolver,
)
: executeImpl(
argsOrSchema,
? executeImpl(argsOrSchema)
: executeImpl({
schema: argsOrSchema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
);
});
}

function executeImpl(
schema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
) {
function executeImpl(args: ExecutionArgs): ExecutionResult {
const {
schema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
} = args;

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

Expand Down
60 changes: 22 additions & 38 deletions src/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,26 +97,17 @@ export function graphql(
resolve(
// Extract arguments from object args if provided.
arguments.length === 1
? graphqlImpl(
argsOrSchema.schema,
argsOrSchema.source,
argsOrSchema.rootValue,
argsOrSchema.contextValue,
argsOrSchema.variableValues,
argsOrSchema.operationName,
argsOrSchema.fieldResolver,
argsOrSchema.typeResolver,
)
: graphqlImpl(
argsOrSchema,
? graphqlImpl(argsOrSchema)
: graphqlImpl({
schema: argsOrSchema,
source,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
),
}),
),
);
}
Expand Down Expand Up @@ -153,26 +144,17 @@ export function graphqlSync(
// Extract arguments from object args if provided.
const result =
arguments.length === 1
? graphqlImpl(
argsOrSchema.schema,
argsOrSchema.source,
argsOrSchema.rootValue,
argsOrSchema.contextValue,
argsOrSchema.variableValues,
argsOrSchema.operationName,
argsOrSchema.fieldResolver,
argsOrSchema.typeResolver,
)
: graphqlImpl(
argsOrSchema,
? graphqlImpl(argsOrSchema)
: graphqlImpl({
schema: argsOrSchema,
source,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
);
});

// Assert that the execution was synchronous.
if (isPromise(result)) {
Expand All @@ -182,16 +164,18 @@ export function graphqlSync(
return result;
}

function graphqlImpl(
schema,
source,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
): PromiseOrValue<ExecutionResult> {
function graphqlImpl(args: GraphQLArgs): PromiseOrValue<ExecutionResult> {
const {
schema,
source,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
} = args;

// Validate Schema
const schemaValidationErrors = validateSchema(schema);
if (schemaValidationErrors.length > 0) {
Expand All @@ -213,7 +197,7 @@ function graphqlImpl(
}

// Execute
return execute(
return execute({
schema,
document,
rootValue,
Expand All @@ -222,5 +206,5 @@ function graphqlImpl(
operationName,
fieldResolver,
typeResolver,
);
});
}
39 changes: 17 additions & 22 deletions src/subscription/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,17 @@ export function subscribe(
/* eslint-enable no-redeclare */
// Extract arguments from object args if provided.
return arguments.length === 1
? subscribeImpl(
argsOrSchema.schema,
argsOrSchema.document,
argsOrSchema.rootValue,
argsOrSchema.contextValue,
argsOrSchema.variableValues,
argsOrSchema.operationName,
argsOrSchema.fieldResolver,
argsOrSchema.subscribeFieldResolver,
)
: subscribeImpl(
argsOrSchema,
? subscribeImpl(argsOrSchema)
: subscribeImpl({
schema: argsOrSchema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
subscribeFieldResolver,
);
});
}

/**
Expand All @@ -124,15 +115,19 @@ function reportGraphQLError(error) {
}

function subscribeImpl(
schema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
subscribeFieldResolver,
) {
args: SubscriptionArgs,
): Promise<AsyncIterator<ExecutionResult> | ExecutionResult> {
const {
schema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
subscribeFieldResolver,
} = args;

const sourcePromise = createSourceEventStream(
schema,
document,
Expand Down

0 comments on commit e876216

Please sign in to comment.