Skip to content

Commit f5c9d66

Browse files
committed
step3
1 parent 974d92d commit f5c9d66

File tree

2 files changed

+38
-39
lines changed

2 files changed

+38
-39
lines changed

src/execution/__tests__/executor-test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,6 @@ describe('Execute: Handles basic execution tasks', () => {
871871
expectJSON(
872872
executeSync({ schema, document, operationName: 'Q' }),
873873
).toDeepEqual({
874-
data: null,
875874
errors: [
876875
{
877876
message: 'Schema is not configured to execute query operation.',
@@ -883,7 +882,6 @@ describe('Execute: Handles basic execution tasks', () => {
883882
expectJSON(
884883
executeSync({ schema, document, operationName: 'M' }),
885884
).toDeepEqual({
886-
data: null,
887885
errors: [
888886
{
889887
message: 'Schema is not configured to execute mutation operation.',
@@ -895,7 +893,6 @@ describe('Execute: Handles basic execution tasks', () => {
895893
expectJSON(
896894
executeSync({ schema, document, operationName: 'S' }),
897895
).toDeepEqual({
898-
data: null,
899896
errors: [
900897
{
901898
message:

src/execution/execute.ts

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -174,34 +174,7 @@ export function execute(args: ExecutionArgs): PromiseOrValue<ExecutionResult> {
174174
return { errors: exeContext };
175175
}
176176

177-
// Return a Promise that will eventually resolve to the data described by
178-
// The "Response" section of the GraphQL specification.
179-
//
180-
// If errors are encountered while executing a GraphQL field, only that
181-
// field and its descendants will be omitted, and sibling fields will still
182-
// be executed. An execution which encounters errors will still result in a
183-
// resolved Promise.
184-
//
185-
// Errors from sub-fields of a NonNull type may propagate to the top level,
186-
// at which point we still log the error and null the parent field, which
187-
// in this case is the entire response.
188-
try {
189-
const { operation } = exeContext;
190-
const result = executeOperation(exeContext, operation);
191-
if (isPromise(result)) {
192-
return result.then(
193-
(data) => buildResponse(data, exeContext.errors),
194-
(error) => {
195-
exeContext.errors.push(error);
196-
return buildResponse(null, exeContext.errors);
197-
},
198-
);
199-
}
200-
return buildResponse(result, exeContext.errors);
201-
} catch (error) {
202-
exeContext.errors.push(error);
203-
return buildResponse(null, exeContext.errors);
204-
}
177+
return executeOperation(exeContext, exeContext.operation);
205178
}
206179

207180
/**
@@ -330,12 +303,14 @@ export function buildExecutionContext(
330303
function executeOperation(
331304
exeContext: ExecutionContext,
332305
operation: OperationDefinitionNode,
333-
): PromiseOrValue<ObjMap<unknown>> {
306+
): PromiseOrValue<ExecutionResult> {
334307
const rootType = exeContext.schema.getRootType(operation.operation);
335308
if (rootType == null) {
336-
throw new GraphQLError(
337-
`Schema is not configured to execute ${operation.operation} operation.`,
338-
{ nodes: operation },
309+
return buildErrorResponse(
310+
new GraphQLError(
311+
`Schema is not configured to execute ${operation.operation} operation.`,
312+
{ nodes: operation },
313+
),
339314
);
340315
}
341316

@@ -364,13 +339,40 @@ function executeRootFields(
364339
rootType: GraphQLObjectType,
365340
rootFields: Map<string, ReadonlyArray<FieldNode>>,
366341
executeSerially: boolean,
367-
): PromiseOrValue<ObjMap<unknown>> {
342+
): PromiseOrValue<ExecutionResult> {
368343
const { rootValue } = exeContext;
369344
const path = undefined;
370345

371-
return executeSerially
372-
? executeFieldsSerially(exeContext, rootType, rootValue, path, rootFields)
373-
: executeFields(exeContext, rootType, rootValue, path, rootFields);
346+
// Return a Promise that will eventually resolve to the data described by
347+
// The "Response" section of the GraphQL specification.
348+
//
349+
// If errors are encountered while executing a GraphQL field, only that
350+
// field and its descendants will be omitted, and sibling fields will still
351+
// be executed. An execution which encounters errors will still result in a
352+
// resolved Promise.
353+
//
354+
// Errors from sub-fields of a NonNull type may propagate to the top level,
355+
// at which point we still log the error and null the parent field, which
356+
// in this case is the entire response.
357+
try {
358+
const data = executeSerially
359+
? executeFieldsSerially(exeContext, rootType, rootValue, path, rootFields)
360+
: executeFields(exeContext, rootType, rootValue, path, rootFields);
361+
362+
if (isPromise(data)) {
363+
return data.then(
364+
(resolvedData) => buildResponse(resolvedData, exeContext.errors),
365+
(error) => {
366+
exeContext.errors.push(error);
367+
return buildResponse(null, exeContext.errors);
368+
},
369+
);
370+
}
371+
return buildResponse(data, exeContext.errors);
372+
} catch (error) {
373+
exeContext.errors.push(error);
374+
return { errors: exeContext.errors, data: null };
375+
}
374376
}
375377

376378
/**

0 commit comments

Comments
 (0)