Skip to content

Commit

Permalink
Simplify collectFields for @defer & @stream
Browse files Browse the repository at this point in the history
  • Loading branch information
robrichard committed Nov 6, 2023
1 parent 7a6d055 commit b520452
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 505 deletions.
19 changes: 12 additions & 7 deletions src/execution/IncrementalPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
GraphQLFormattedError,
} from '../error/GraphQLError.js';

import type { GroupedFieldSet } from './collectFields.js';
import type { DeferUsage, GroupedFieldSet } from './collectFields.js';

interface IncrementalUpdate<TData = unknown, TExtensions = ObjMap<unknown>> {
pending: ReadonlyArray<PendingResult>;
Expand Down Expand Up @@ -611,8 +611,11 @@ export class IncrementalPublisher {
const { data, deferredFragmentRecords } = deferredGroupedFieldSetRecord;
let maxLength = deferredFragmentRecords[0].path.length;
let maxIndex = 0;
for (let i = 1; i < deferredFragmentRecords.length; i++) {
for (let i = 0; i < deferredFragmentRecords.length; i++) {
const deferredFragmentRecord = deferredFragmentRecords[i];
if (deferredFragmentRecord.id == null) {
continue;
}
const length = deferredFragmentRecord.path.length;
if (length > maxLength) {
maxLength = length;
Expand Down Expand Up @@ -760,7 +763,6 @@ export class DeferredGroupedFieldSetRecord {
path: ReadonlyArray<string | number>;
deferredFragmentRecords: ReadonlyArray<DeferredFragmentRecord>;
groupedFieldSet: GroupedFieldSet;
shouldInitiateDefer: boolean;
errors: Array<GraphQLError>;
data: ObjMap<unknown> | undefined;
sent: boolean;
Expand All @@ -769,12 +771,10 @@ export class DeferredGroupedFieldSetRecord {
path: Path | undefined;
deferredFragmentRecords: ReadonlyArray<DeferredFragmentRecord>;
groupedFieldSet: GroupedFieldSet;
shouldInitiateDefer: boolean;
}) {
this.path = pathToArray(opts.path);
this.deferredFragmentRecords = opts.deferredFragmentRecords;
this.groupedFieldSet = opts.groupedFieldSet;
this.shouldInitiateDefer = opts.shouldInitiateDefer;
this.errors = [];
this.sent = false;
}
Expand All @@ -783,6 +783,8 @@ export class DeferredGroupedFieldSetRecord {
/** @internal */
export class DeferredFragmentRecord {
path: ReadonlyArray<string | number>;
pathObj: Path | undefined;
deferUsage: DeferUsage;
label: string | undefined;
id: string | undefined;
children: Set<SubsequentResultRecord>;
Expand All @@ -792,9 +794,12 @@ export class DeferredFragmentRecord {
pendingSent?: boolean;
_pending: Set<DeferredGroupedFieldSetRecord>;

constructor(opts: { path: Path | undefined; label: string | undefined }) {
constructor(opts: { path: Path | undefined; deferUsage: DeferUsage }) {
this.pathObj = opts.path;
this.path = pathToArray(opts.path);
this.label = opts.label;
this.deferUsage = opts.deferUsage;
this.label = opts.deferUsage.label;
this.deferUsage = opts.deferUsage;
this.children = new Set();
this.filtered = false;
this.deferredGroupedFieldSetRecords = new Set();
Expand Down
48 changes: 9 additions & 39 deletions src/execution/__tests__/defer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,19 +481,11 @@ describe('Execute: defer directive', () => {
}
`);
const result = await complete(document);
expectJSON(result).toDeepEqual([
{
data: {
hero: {},
},
pending: [{ id: '0', path: ['hero'] }],
hasNext: true,
},
{
completed: [{ id: '0' }],
hasNext: false,
expectJSON(result).toDeepEqual({
data: {
hero: {},
},
]);
});
});

it('Can separately emit defer fragments with different labels with varying fields', async () => {
Expand Down Expand Up @@ -775,40 +767,18 @@ describe('Execute: defer directive', () => {
data: { hero: { friends: [{}, {}, {}] } },
pending: [
{ id: '0', path: ['hero', 'friends', 0] },
{ id: '1', path: ['hero', 'friends', 0] },
{ id: '2', path: ['hero', 'friends', 0] },
{ id: '3', path: ['hero', 'friends', 0] },
{ id: '4', path: ['hero', 'friends', 1] },
{ id: '5', path: ['hero', 'friends', 1] },
{ id: '6', path: ['hero', 'friends', 1] },
{ id: '7', path: ['hero', 'friends', 1] },
{ id: '8', path: ['hero', 'friends', 2] },
{ id: '9', path: ['hero', 'friends', 2] },
{ id: '10', path: ['hero', 'friends', 2] },
{ id: '11', path: ['hero', 'friends', 2] },
{ id: '1', path: ['hero', 'friends', 1] },
{ id: '2', path: ['hero', 'friends', 2] },
],
hasNext: true,
},
{
incremental: [
{ data: { id: '2', name: 'Han' }, id: '0' },
{ data: { id: '3', name: 'Leia' }, id: '4' },
{ data: { id: '4', name: 'C-3PO' }, id: '8' },
],
completed: [
{ id: '1' },
{ id: '2' },
{ id: '3' },
{ id: '5' },
{ id: '6' },
{ id: '7' },
{ id: '9' },
{ id: '10' },
{ id: '11' },
{ id: '0' },
{ id: '4' },
{ id: '8' },
{ data: { id: '3', name: 'Leia' }, id: '1' },
{ data: { id: '4', name: 'C-3PO' }, id: '2' },
],
completed: [{ id: '0' }, { id: '1' }, { id: '2' }],
hasNext: false,
},
]);
Expand Down
Loading

0 comments on commit b520452

Please sign in to comment.