Skip to content

Commit 58ac464

Browse files
committed
Simplify args processing in "graphql" and "execute" functions
1 parent d3fd6c3 commit 58ac464

File tree

2 files changed

+33
-70
lines changed

2 files changed

+33
-70
lines changed

src/execution/execute.js

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -149,57 +149,39 @@ export function execute(
149149
fieldResolver
150150
) {
151151
// Extract arguments from object args if provided.
152-
const args = arguments.length === 1 ? argsOrSchema : undefined;
153-
const schema = args ? args.schema : argsOrSchema;
154-
return args ?
155-
executeImpl(
156-
schema,
157-
args.document,
158-
args.rootValue,
159-
args.contextValue,
160-
args.variableValues,
161-
args.operationName,
162-
args.fieldResolver
163-
) :
164-
executeImpl(
165-
schema,
152+
return arguments.length === 1 ?
153+
executeImpl(argsOrSchema) :
154+
executeImpl({
155+
schema: argsOrSchema,
166156
document,
167157
rootValue,
168158
contextValue,
169159
variableValues,
170160
operationName,
171161
fieldResolver
172-
);
162+
});
173163
}
174164

175-
function executeImpl(
176-
schema,
177-
document,
178-
rootValue,
179-
contextValue,
180-
variableValues,
181-
operationName,
182-
fieldResolver
183-
) {
165+
function executeImpl(args) {
184166
// If arguments are missing or incorrect, throw an error.
185167
assertValidExecutionArguments(
186-
schema,
187-
document,
188-
variableValues
168+
args.schema,
169+
args.document,
170+
args.variableValues
189171
);
190172

191173
// If a valid context cannot be created due to incorrect arguments,
192174
// a "Response" with only errors is returned.
193175
let context;
194176
try {
195177
context = buildExecutionContext(
196-
schema,
197-
document,
198-
rootValue,
199-
contextValue,
200-
variableValues,
201-
operationName,
202-
fieldResolver
178+
args.schema,
179+
args.document,
180+
args.rootValue,
181+
args.contextValue,
182+
args.variableValues,
183+
args.operationName,
184+
args.fieldResolver
203185
);
204186
} catch (error) {
205187
return Promise.resolve({ errors: [ error ] });
@@ -213,7 +195,7 @@ function executeImpl(
213195
// be executed. An execution which encounters errors will still result in a
214196
// resolved Promise.
215197
return Promise.resolve(
216-
executeOperation(context, context.operation, rootValue)
198+
executeOperation(context)
217199
).then(data => context.errors.length === 0 ?
218200
{ data } :
219201
{ errors: context.errors, data }
@@ -341,10 +323,9 @@ export function buildExecutionContext(
341323
*/
342324
function executeOperation(
343325
exeContext: ExecutionContext,
344-
operation: OperationDefinitionNode,
345-
rootValue: mixed
346326
): ?{[key: string]: mixed} {
347-
const type = getOperationRootType(exeContext.schema, operation);
327+
const { schema, operation, rootValue } = exeContext;
328+
const type = getOperationRootType(schema, operation);
348329
const fields = collectFields(
349330
exeContext,
350331
type,

src/graphql.js

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -75,63 +75,45 @@ export function graphql(
7575
fieldResolver
7676
) {
7777
// Extract arguments from object args if provided.
78-
const args = arguments.length === 1 ? argsOrSchema : undefined;
79-
const schema = args ? args.schema : argsOrSchema;
80-
return args ?
81-
graphqlImpl(
82-
schema,
83-
args.source,
84-
args.rootValue,
85-
args.contextValue,
86-
args.variableValues,
87-
args.operationName,
88-
args.fieldResolver
89-
) :
90-
graphqlImpl(
91-
schema,
78+
return arguments.length === 1 ?
79+
graphqlImpl(argsOrSchema) :
80+
graphqlImpl({
81+
schema: argsOrSchema,
9282
source,
9383
rootValue,
9484
contextValue,
9585
variableValues,
9686
operationName,
9787
fieldResolver
98-
);
88+
});
9989
}
10090

101-
function graphqlImpl(
102-
schema,
103-
source,
104-
rootValue,
105-
contextValue,
106-
variableValues,
107-
operationName,
108-
fieldResolver
109-
) {
91+
function graphqlImpl(args) {
11092
return new Promise(resolve => {
11193
// Parse
11294
let document;
11395
try {
114-
document = parse(source);
96+
document = parse(args.source);
11597
} catch (syntaxError) {
11698
return resolve({ errors: [ syntaxError ]});
11799
}
118100

119101
// Validate
120-
const validationErrors = validate(schema, document);
102+
const validationErrors = validate(args.schema, document);
121103
if (validationErrors.length > 0) {
122104
return resolve({ errors: validationErrors });
123105
}
124106

125107
// Execute
126108
resolve(
127109
execute(
128-
schema,
110+
args.schema,
129111
document,
130-
rootValue,
131-
contextValue,
132-
variableValues,
133-
operationName,
134-
fieldResolver
112+
args.rootValue,
113+
args.contextValue,
114+
args.variableValues,
115+
args.operationName,
116+
args.fieldResolver
135117
)
136118
);
137119
});

0 commit comments

Comments
 (0)