Skip to content

Commit cdbfbde

Browse files
committed
expose all of the execution environment to resolvers
presumably we should do this
1 parent e90cdbc commit cdbfbde

File tree

4 files changed

+45
-55
lines changed

4 files changed

+45
-55
lines changed

src/execution/__tests__/defer-test.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ describe('Execute: defer directive', () => {
205205
incremental: [
206206
{
207207
data: {
208+
id: '1',
208209
name: 'Luke',
209210
},
210211
path: ['hero'],
@@ -409,7 +410,9 @@ describe('Execute: defer directive', () => {
409410
{
410411
incremental: [
411412
{
412-
data: {},
413+
data: {
414+
name: 'Luke',
415+
},
413416
path: ['hero'],
414417
},
415418
],
@@ -444,7 +447,9 @@ describe('Execute: defer directive', () => {
444447
{
445448
incremental: [
446449
{
447-
data: {},
450+
data: {
451+
name: 'Luke',
452+
},
448453
path: ['hero'],
449454
},
450455
],
@@ -522,7 +527,7 @@ describe('Execute: defer directive', () => {
522527
]);
523528
});
524529

525-
it('Can deduplicate leaf fields present in the initial payload', async () => {
530+
it('Does not deduplicate leaf fields present in the initial payload', async () => {
526531
const document = parse(`
527532
query {
528533
hero {
@@ -580,7 +585,9 @@ describe('Execute: defer directive', () => {
580585
},
581586
},
582587
anotherNestedObject: {
583-
deeperObject: {},
588+
deeperObject: {
589+
foo: 'foo',
590+
},
584591
},
585592
},
586593
path: ['hero'],
@@ -591,7 +598,7 @@ describe('Execute: defer directive', () => {
591598
]);
592599
});
593600

594-
it('Can deduplicate fields with deferred fragments at multiple levels', async () => {
601+
it('Does not deduplicate fields with deferred fragments at multiple levels', async () => {
595602
const document = parse(`
596603
query {
597604
hero {
@@ -642,6 +649,7 @@ describe('Execute: defer directive', () => {
642649
incremental: [
643650
{
644651
data: {
652+
foo: 'foo',
645653
bar: 'bar',
646654
baz: 'baz',
647655
bak: 'bak',
@@ -651,6 +659,7 @@ describe('Execute: defer directive', () => {
651659
{
652660
data: {
653661
deeperObject: {
662+
foo: 'foo',
654663
bar: 'bar',
655664
baz: 'baz',
656665
},
@@ -661,6 +670,7 @@ describe('Execute: defer directive', () => {
661670
data: {
662671
nestedObject: {
663672
deeperObject: {
673+
foo: 'foo',
664674
bar: 'bar',
665675
},
666676
},
@@ -732,7 +742,7 @@ describe('Execute: defer directive', () => {
732742
]);
733743
});
734744

735-
it('can deduplicate fields with deferred fragments in different branches at multiple non-overlapping levels', async () => {
745+
it('Can deduplicate fields with deferred fragments in different branches at multiple non-overlapping levels', async () => {
736746
const document = parse(`
737747
query {
738748
a {

src/execution/__tests__/executor-test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ describe('Execute: Handles basic execution tasks', () => {
215215

216216
expect(resolvedInfo).to.have.all.keys(
217217
'fieldName',
218-
'fieldNodes',
218+
'fieldGroup',
219+
'deferDepth',
219220
'returnType',
220221
'parentType',
221222
'path',
@@ -238,9 +239,10 @@ describe('Execute: Handles basic execution tasks', () => {
238239
operation,
239240
});
240241

241-
const field = operation.selectionSet.selections[0];
242+
const fieldNode = operation.selectionSet.selections[0];
242243
expect(resolvedInfo).to.deep.include({
243-
fieldNodes: [field],
244+
fieldGroup: [{ fieldNode, depth: 0, deferDepth: undefined }],
245+
deferDepth: undefined,
244246
variableValues: { var: 'abc' },
245247
});
246248

src/execution/execute.ts

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import type {
3838
GraphQLTypeResolver,
3939
} from '../type/definition.js';
4040
import {
41-
getNamedType,
4241
isAbstractType,
4342
isLeafType,
4443
isListType,
@@ -631,26 +630,19 @@ function executeFieldsSerially(
631630
(results, [responseName, fieldGroup]) => {
632631
const fieldPath = exeContext.addPath(path, responseName, parentType.name);
633632

634-
const fieldName = fieldGroup[0].fieldNode.name.value;
635-
const fieldDef = exeContext.schema.getField(parentType, fieldName);
636-
if (!fieldDef) {
637-
return results;
638-
}
639-
640-
const returnType = fieldDef.type;
641-
642-
if (!shouldExecute(fieldGroup, returnType)) {
633+
if (!shouldExecute(fieldGroup)) {
643634
return results;
644635
}
645636
const result = executeField(
646637
exeContext,
647638
parentType,
648-
fieldDef,
649-
returnType,
650639
sourceValue,
651640
fieldGroup,
652641
fieldPath,
653642
);
643+
if (result === undefined) {
644+
return results;
645+
}
654646
if (isPromise(result)) {
655647
return result.then((resolvedResult) => {
656648
results[responseName] = resolvedResult;
@@ -666,25 +658,11 @@ function executeFieldsSerially(
666658

667659
function shouldExecute(
668660
fieldGroup: FieldGroup,
669-
returnType: GraphQLOutputType,
670661
deferDepth?: number | undefined,
671662
): boolean {
672-
if (deferDepth === undefined || !isLeafType(getNamedType(returnType))) {
673-
return fieldGroup.some(
674-
({ deferDepth: fieldDeferDepth }) => fieldDeferDepth === deferDepth,
675-
);
676-
}
677-
678-
let hasDepth = false;
679-
for (const { deferDepth: fieldDeferDepth } of fieldGroup) {
680-
if (fieldDeferDepth === undefined) {
681-
return false;
682-
}
683-
if (fieldDeferDepth === deferDepth) {
684-
hasDepth = true;
685-
}
686-
}
687-
return hasDepth;
663+
return fieldGroup.some(
664+
({ deferDepth: fieldDeferDepth }) => fieldDeferDepth === deferDepth,
665+
);
688666
}
689667

690668
/**
@@ -706,22 +684,10 @@ function executeFields(
706684
for (const [responseName, fieldGroup] of groupedFieldSet) {
707685
const fieldPath = exeContext.addPath(path, responseName, parentType.name);
708686

709-
const fieldName = fieldGroup[0].fieldNode.name.value;
710-
const fieldDef = exeContext.schema.getField(parentType, fieldName);
711-
if (!fieldDef) {
712-
continue;
713-
}
714-
715-
const returnType = fieldDef.type;
716-
717-
if (
718-
shouldExecute(fieldGroup, returnType, asyncPayloadRecord?.deferDepth)
719-
) {
687+
if (shouldExecute(fieldGroup, asyncPayloadRecord?.deferDepth)) {
720688
const result = executeField(
721689
exeContext,
722690
parentType,
723-
fieldDef,
724-
returnType,
725691
sourceValue,
726692
fieldGroup,
727693
fieldPath,
@@ -769,15 +735,19 @@ function toNodes(fieldGroup: FieldGroup): ReadonlyArray<FieldNode> {
769735
function executeField(
770736
exeContext: ExecutionContext,
771737
parentType: GraphQLObjectType,
772-
fieldDef: GraphQLField<unknown, unknown>,
773-
returnType: GraphQLOutputType,
774738
source: unknown,
775739
fieldGroup: FieldGroup,
776740
path: Path,
777741
asyncPayloadRecord?: AsyncPayloadRecord,
778742
): PromiseOrValue<unknown> {
779743
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
744+
const fieldName = fieldGroup[0].fieldNode.name.value;
745+
const fieldDef = exeContext.schema.getField(parentType, fieldName);
746+
if (!fieldDef) {
747+
return;
748+
}
780749

750+
const returnType = fieldDef.type;
781751
const resolveFn = fieldDef.resolve ?? exeContext.fieldResolver;
782752

783753
const info = buildResolveInfo(
@@ -786,6 +756,7 @@ function executeField(
786756
fieldGroup,
787757
parentType,
788758
path,
759+
asyncPayloadRecord,
789760
);
790761

791762
// Get the resolve function, regardless of if its result is normal or abrupt (error).
@@ -865,12 +836,14 @@ export function buildResolveInfo(
865836
fieldGroup: FieldGroup,
866837
parentType: GraphQLObjectType,
867838
path: Path,
839+
asyncPayloadRecord?: AsyncPayloadRecord | undefined,
868840
): GraphQLResolveInfo {
869841
// The resolve function's optional fourth argument is a collection of
870842
// information about the current execution state.
871843
return {
872844
fieldName: fieldDef.name,
873-
fieldNodes: toNodes(fieldGroup),
845+
fieldGroup,
846+
deferDepth: asyncPayloadRecord?.deferDepth,
874847
returnType: fieldDef.type,
875848
parentType,
876849
path,

src/type/definition.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,12 @@ export type GraphQLFieldResolver<
890890

891891
export interface GraphQLResolveInfo {
892892
readonly fieldName: string;
893-
readonly fieldNodes: ReadonlyArray<FieldNode>;
893+
readonly fieldGroup: ReadonlyArray<{
894+
fieldNode: FieldNode;
895+
depth: number;
896+
deferDepth: number | undefined;
897+
}>;
898+
readonly deferDepth: number | undefined;
894899
readonly returnType: GraphQLOutputType;
895900
readonly parentType: GraphQLObjectType;
896901
readonly path: Path;

0 commit comments

Comments
 (0)