Skip to content

Commit

Permalink
Allow providing an object of arguments to graphql().
Browse files Browse the repository at this point in the history
As well as execute() and subscribe()

Closes #356
  • Loading branch information
leebyron committed May 18, 2017
1 parent 1b41959 commit 0889d58
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 10 deletions.
39 changes: 37 additions & 2 deletions src/execution/execute.js
Expand Up @@ -109,16 +109,51 @@ export type ExecutionResult = {
*
* If the arguments to this function do not result in a legal execution context,
* a GraphQLError will be thrown immediately explaining the invalid input.
*
* Accepts either an object with named arguments, or individual arguments.
*/
export function execute(
declare function execute({|
schema: GraphQLSchema,
document: DocumentNode,
rootValue?: mixed,
contextValue?: mixed,
variableValues?: ?{[key: string]: mixed},
operationName?: ?string,
fieldResolver?: ?GraphQLFieldResolver<any, any>
|}, ..._: []): Promise<ExecutionResult>;
/* eslint-disable no-redeclare */
declare function execute(
schema: GraphQLSchema,
document: DocumentNode,
rootValue?: mixed,
contextValue?: mixed,
variableValues?: ?{[key: string]: mixed},
operationName?: ?string,
fieldResolver?: ?GraphQLFieldResolver<any, any>
): Promise<ExecutionResult> {
): Promise<ExecutionResult>;
export function execute(
argsOrSchema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver
) {
// Extract arguments from object args if provided.
const args = arguments.length === 1 ? argsOrSchema : undefined;
const schema = args ? args.schema : argsOrSchema;
if (args) {
/* eslint-disable no-param-reassign */
document = args.document;
rootValue = args.rootValue;
contextValue = args.contextValue;
variableValues = args.variableValues;
operationName = args.operationName;
fieldResolver = args.fieldResolver;
/* eslint-enable no-param-reassign, no-redeclare */
}

// If a valid context cannot be created due to incorrect arguments,
// this will throw an error.
const context = buildExecutionContext(
Expand Down
46 changes: 40 additions & 6 deletions src/graphql.js
Expand Up @@ -16,7 +16,6 @@ import type { GraphQLFieldResolver } from './type/definition';
import type { GraphQLSchema } from './type/schema';
import type { ExecutionResult } from './execution/execute';


/**
* This is the primary entry point function for fulfilling GraphQL operations
* by parsing, validating, and executing a GraphQL document along side a
Expand All @@ -26,6 +25,8 @@ import type { ExecutionResult } from './execution/execute';
* may wish to separate the validation and execution phases to a static time
* tooling step, and a server runtime step.
*
* Accepts either an object with named arguments, or individual arguments:
*
* schema:
* The GraphQL type system to use when validating and executing a query.
* source:
Expand All @@ -45,25 +46,58 @@ import type { ExecutionResult } from './execution/execute';
* If not provided, the default field resolver is used (which looks for a
* value or method on the source value with the field's name).
*/
export function graphql(
declare function graphql({|
schema: GraphQLSchema,
source: string | Source,
rootValue?: mixed,
contextValue?: mixed,
variableValues?: ?{[key: string]: mixed},
operationName?: ?string,
fieldResolver?: ?GraphQLFieldResolver<any, any>
): Promise<ExecutionResult> {
|}, ..._: []): Promise<ExecutionResult>;
/* eslint-disable no-redeclare */
declare function graphql(
schema: GraphQLSchema,
source: Source | string,
rootValue?: mixed,
contextValue?: mixed,
variableValues?: ?{[key: string]: mixed},
operationName?: ?string,
fieldResolver?: ?GraphQLFieldResolver<any, any>
): Promise<ExecutionResult>;
export function graphql(
argsOrSchema,
source,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver
) {
// Extract arguments from object args if provided.
const args = arguments.length === 1 ? argsOrSchema : undefined;
const schema = args ? args.schema : argsOrSchema;
if (args) {
/* eslint-disable no-param-reassign */
source = args.source;
rootValue = args.rootValue;
contextValue = args.contextValue;
variableValues = args.variableValues;
operationName = args.operationName;
fieldResolver = args.fieldResolver;
/* eslint-enable no-param-reassign, no-redeclare */
}

return new Promise(resolve => {
const documentAST = parse(source);
const validationErrors = validate(schema, documentAST);
const document = parse(source);
const validationErrors = validate(schema, document);
if (validationErrors.length > 0) {
resolve({ errors: validationErrors });
} else {
resolve(
execute(
schema,
documentAST,
document,
rootValue,
contextValue,
variableValues,
Expand Down
42 changes: 40 additions & 2 deletions src/subscription/subscribe.js
Expand Up @@ -35,8 +35,10 @@ import type { GraphQLFieldResolver } from '../type/definition';
*
* If the arguments to this function do not result in a legal execution context,
* a GraphQLError will be thrown immediately explaining the invalid input.
*
* Accepts either an object with named arguments, or individual arguments.
*/
export function subscribe(
declare function subscribe({|
schema: GraphQLSchema,
document: DocumentNode,
rootValue?: mixed,
Expand All @@ -45,7 +47,43 @@ export function subscribe(
operationName?: ?string,
fieldResolver?: ?GraphQLFieldResolver<any, any>,
subscribeFieldResolver?: ?GraphQLFieldResolver<any, any>
): AsyncIterator<ExecutionResult> {
|}, ..._: []): AsyncIterator<ExecutionResult>;
/* eslint-disable no-redeclare */
declare function subscribe(
schema: GraphQLSchema,
document: DocumentNode,
rootValue?: mixed,
contextValue?: mixed,
variableValues?: ?{[key: string]: mixed},
operationName?: ?string,
fieldResolver?: ?GraphQLFieldResolver<any, any>,
subscribeFieldResolver?: ?GraphQLFieldResolver<any, any>
): AsyncIterator<ExecutionResult>;
export function subscribe(
argsOrSchema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
subscribeFieldResolver
) {
// Extract arguments from object args if provided.
const args = arguments.length === 1 ? argsOrSchema : undefined;
const schema = args ? args.schema : argsOrSchema;
if (args) {
/* eslint-disable no-param-reassign */
document = args.document;
rootValue = args.rootValue;
contextValue = args.contextValue;
variableValues = args.variableValues;
operationName = args.operationName;
fieldResolver = args.fieldResolver;
subscribeFieldResolver = args.subscribeFieldResolver;
/* eslint-enable no-param-reassign, no-redeclare */
}

const subscription = createSourceEventStream(
schema,
document,
Expand Down

0 comments on commit 0889d58

Please sign in to comment.