Skip to content

Commit fee8abb

Browse files
committed
Revert "Simplify args processing in "graphql" and "execute" functions"
This reverts commit 58ac464. Legibility improvements
1 parent 61272b4 commit fee8abb

File tree

2 files changed

+70
-33
lines changed

2 files changed

+70
-33
lines changed

src/execution/execute.js

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -149,39 +149,57 @@ export function execute(
149149
fieldResolver
150150
) {
151151
// Extract arguments from object args if provided.
152-
return arguments.length === 1 ?
153-
executeImpl(argsOrSchema) :
154-
executeImpl({
155-
schema: argsOrSchema,
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,
156166
document,
157167
rootValue,
158168
contextValue,
159169
variableValues,
160170
operationName,
161171
fieldResolver
162-
});
172+
);
163173
}
164174

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

173191
// If a valid context cannot be created due to incorrect arguments,
174192
// a "Response" with only errors is returned.
175193
let context;
176194
try {
177195
context = buildExecutionContext(
178-
args.schema,
179-
args.document,
180-
args.rootValue,
181-
args.contextValue,
182-
args.variableValues,
183-
args.operationName,
184-
args.fieldResolver
196+
schema,
197+
document,
198+
rootValue,
199+
contextValue,
200+
variableValues,
201+
operationName,
202+
fieldResolver
185203
);
186204
} catch (error) {
187205
return Promise.resolve({ errors: [ error ] });
@@ -195,7 +213,7 @@ function executeImpl(args) {
195213
// be executed. An execution which encounters errors will still result in a
196214
// resolved Promise.
197215
return Promise.resolve(
198-
executeOperation(context)
216+
executeOperation(context, context.operation, rootValue)
199217
).then(data => context.errors.length === 0 ?
200218
{ data } :
201219
{ errors: context.errors, data }
@@ -323,9 +341,10 @@ export function buildExecutionContext(
323341
*/
324342
function executeOperation(
325343
exeContext: ExecutionContext,
344+
operation: OperationDefinitionNode,
345+
rootValue: mixed
326346
): ?{[key: string]: mixed} {
327-
const { schema, operation, rootValue } = exeContext;
328-
const type = getOperationRootType(schema, operation);
347+
const type = getOperationRootType(exeContext.schema, operation);
329348
const fields = collectFields(
330349
exeContext,
331350
type,

src/graphql.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,45 +75,63 @@ export function graphql(
7575
fieldResolver
7676
) {
7777
// Extract arguments from object args if provided.
78-
return arguments.length === 1 ?
79-
graphqlImpl(argsOrSchema) :
80-
graphqlImpl({
81-
schema: argsOrSchema,
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,
8292
source,
8393
rootValue,
8494
contextValue,
8595
variableValues,
8696
operationName,
8797
fieldResolver
88-
});
98+
);
8999
}
90100

91-
function graphqlImpl(args) {
101+
function graphqlImpl(
102+
schema,
103+
source,
104+
rootValue,
105+
contextValue,
106+
variableValues,
107+
operationName,
108+
fieldResolver
109+
) {
92110
return new Promise(resolve => {
93111
// Parse
94112
let document;
95113
try {
96-
document = parse(args.source);
114+
document = parse(source);
97115
} catch (syntaxError) {
98116
return resolve({ errors: [ syntaxError ]});
99117
}
100118

101119
// Validate
102-
const validationErrors = validate(args.schema, document);
120+
const validationErrors = validate(schema, document);
103121
if (validationErrors.length > 0) {
104122
return resolve({ errors: validationErrors });
105123
}
106124

107125
// Execute
108126
resolve(
109127
execute(
110-
args.schema,
128+
schema,
111129
document,
112-
args.rootValue,
113-
args.contextValue,
114-
args.variableValues,
115-
args.operationName,
116-
args.fieldResolver
130+
rootValue,
131+
contextValue,
132+
variableValues,
133+
operationName,
134+
fieldResolver
117135
)
118136
);
119137
});

0 commit comments

Comments
 (0)