Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] main from graphql:main #201

Merged
merged 5 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v19
v20
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
},
"scripts": {
"preversion": ". ./resources/checkgit.sh && npm ci --ignore-scripts",
"version": "ts-node resources/gen-version.ts && npm test && git add src/version.ts",
"version": "node --loader ts-node/esm resources/gen-version.ts && npm test && git add src/version.ts",
"fuzzonly": "mocha --full-trace src/**/__tests__/**/*-fuzz.ts",
"changelog": "ts-node resources/gen-changelog.ts",
"benchmark": "ts-node resources/benchmark.ts",
"changelog": "node --loader ts-node/esm resources/gen-changelog.ts",
"benchmark": "node --loader ts-node/esm resources/benchmark.ts",
"test": "npm run lint && npm run check && npm run testonly:cover && npm run prettier:check && npm run check:spelling && npm run check:integrations",
"lint": "eslint --cache --max-warnings 0 --rulesdir resources/eslint-internal-rules/ .",
"check": "tsc --pretty",
Expand All @@ -47,10 +47,10 @@
"check:integrations": "mocha --full-trace resources/integration-test.ts",
"serve": "docusaurus serve --dir websiteDist/ --config website/docusaurus.config.cjs",
"start": "npm run build:website && npm run serve",
"build:website": "ts-node resources/build-docusaurus.ts",
"build:npm": "ts-node resources/build-npm.ts",
"build:deno": "ts-node resources/build-deno.ts",
"diff:npm": "ts-node resources/diff-npm-package.ts",
"build:website": "node --loader ts-node/esm resources/build-docusaurus.ts",
"build:npm": "node --loader ts-node/esm resources/build-npm.ts",
"build:deno": "node --loader ts-node/esm resources/build-deno.ts",
"diff:npm": "node --loader ts-node/esm resources/diff-npm-package.ts",
"gitpublish:npm": "bash ./resources/gitpublish.sh npm npmDist",
"gitpublish:deno": "bash ./resources/gitpublish.sh deno denoDist"
},
Expand Down
39 changes: 21 additions & 18 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 { GroupedFieldSet } from './buildFieldPlan.js';

interface IncrementalUpdate<TData = unknown, TExtensions = ObjMap<unknown>> {
pending: ReadonlyArray<PendingResult>;
Expand Down Expand Up @@ -609,29 +609,29 @@ export class IncrementalPublisher {
deferredGroupedFieldSetRecord: DeferredGroupedFieldSetRecord,
): IncrementalDeferResult {
const { data, deferredFragmentRecords } = deferredGroupedFieldSetRecord;
let maxLength = deferredFragmentRecords[0].path.length;
let maxIndex = 0;
for (let i = 1; i < deferredFragmentRecords.length; i++) {
const deferredFragmentRecord = deferredFragmentRecords[i];
let maxLength: number | undefined;
let idWithLongestPath: string | undefined;
for (const deferredFragmentRecord of deferredFragmentRecords) {
const id = deferredFragmentRecord.id;
if (id === undefined) {
continue;
}
const length = deferredFragmentRecord.path.length;
if (length > maxLength) {
if (maxLength === undefined || length > maxLength) {
maxLength = length;
maxIndex = i;
idWithLongestPath = id;
}
}
const recordWithLongestPath = deferredFragmentRecords[maxIndex];
const longestPath = recordWithLongestPath.path;
const subPath = deferredGroupedFieldSetRecord.path.slice(
longestPath.length,
);
const id = recordWithLongestPath.id;
const subPath = deferredGroupedFieldSetRecord.path.slice(maxLength);
const incrementalDeferResult: IncrementalDeferResult = {
// safe because `data``is always defined when the record is completed
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
data: data!,
// safe because `id` is defined once the fragment has been released as pending
// safe because `id` is always defined once the fragment has been released
// as pending and at least one fragment has been completed, so must have been
// released as pending
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
id: id!,
id: idWithLongestPath!,
};

if (subPath.length > 0) {
Expand Down Expand Up @@ -666,10 +666,13 @@ export class IncrementalPublisher {
return;
}

if (subsequentResultRecord._pending.size === 0) {
this._push(subsequentResultRecord);
} else {
if (subsequentResultRecord._pending.size > 0) {
this._introduce(subsequentResultRecord);
} else if (
subsequentResultRecord.deferredGroupedFieldSetRecords.size > 0 ||
subsequentResultRecord.children.size > 0
) {
this._push(subsequentResultRecord);
}
}

Expand Down
172 changes: 39 additions & 133 deletions src/execution/__tests__/defer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,21 +394,13 @@ describe('Execute: defer directive', () => {
}
`);
const result = await complete(document);
expectJSON(result).toDeepEqual([
{
data: {
hero: {
name: 'Luke',
},
expectJSON(result).toDeepEqual({
data: {
hero: {
name: 'Luke',
},
pending: [{ id: '0', path: ['hero'], label: 'DeferTop' }],
hasNext: true,
},
{
completed: [{ id: '0' }],
hasNext: false,
},
]);
});
});
it('Can defer a fragment that is also not deferred, non-deferred fragment is first', async () => {
const document = parse(`
Expand All @@ -423,21 +415,13 @@ describe('Execute: defer directive', () => {
}
`);
const result = await complete(document);
expectJSON(result).toDeepEqual([
{
data: {
hero: {
name: 'Luke',
},
expectJSON(result).toDeepEqual({
data: {
hero: {
name: 'Luke',
},
pending: [{ id: '0', path: ['hero'], label: 'DeferTop' }],
hasNext: true,
},
{
completed: [{ id: '0' }],
hasNext: false,
},
]);
});
});

it('Can defer an inline fragment', async () => {
Expand Down Expand Up @@ -481,19 +465,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 +751,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 Expand Up @@ -1494,21 +1448,13 @@ describe('Execute: defer directive', () => {
}
`);
const result = await complete(document);
expectJSON(result).toDeepEqual([
{
data: {
hero: {
friends: [{ name: 'Han' }, { name: 'Leia' }, { name: 'C-3PO' }],
},
expectJSON(result).toDeepEqual({
data: {
hero: {
friends: [{ name: 'Han' }, { name: 'Leia' }, { name: 'C-3PO' }],
},
pending: [{ id: '0', path: ['hero'] }],
hasNext: true,
},
{
completed: [{ id: '0' }],
hasNext: false,
},
]);
});
});

it('Deduplicates async iterable list fields', async () => {
Expand All @@ -1534,17 +1480,9 @@ describe('Execute: defer directive', () => {
},
},
});
expectJSON(result).toDeepEqual([
{
data: { hero: { friends: [{ name: 'Han' }] } },
pending: [{ id: '0', path: ['hero'] }],
hasNext: true,
},
{
completed: [{ id: '0' }],
hasNext: false,
},
]);
expectJSON(result).toDeepEqual({
data: { hero: { friends: [{ name: 'Han' }] } },
});
});

it('Deduplicates empty async iterable list fields', async () => {
Expand All @@ -1571,17 +1509,9 @@ describe('Execute: defer directive', () => {
},
},
});
expectJSON(result).toDeepEqual([
{
data: { hero: { friends: [] } },
pending: [{ id: '0', path: ['hero'] }],
hasNext: true,
},
{
completed: [{ id: '0' }],
hasNext: false,
},
]);
expectJSON(result).toDeepEqual({
data: { hero: { friends: [] } },
});
});

it('Does not deduplicate list fields with non-overlapping fields', async () => {
Expand Down Expand Up @@ -1655,17 +1585,9 @@ describe('Execute: defer directive', () => {
friends: () => [],
},
});
expectJSON(result).toDeepEqual([
{
data: { hero: { friends: [] } },
pending: [{ id: '0', path: ['hero'] }],
hasNext: true,
},
{
completed: [{ id: '0' }],
hasNext: false,
},
]);
expectJSON(result).toDeepEqual({
data: { hero: { friends: [] } },
});
});

it('Deduplicates null object fields', async () => {
Expand All @@ -1689,17 +1611,9 @@ describe('Execute: defer directive', () => {
nestedObject: () => null,
},
});
expectJSON(result).toDeepEqual([
{
data: { hero: { nestedObject: null } },
pending: [{ id: '0', path: ['hero'] }],
hasNext: true,
},
{
completed: [{ id: '0' }],
hasNext: false,
},
]);
expectJSON(result).toDeepEqual({
data: { hero: { nestedObject: null } },
});
});

it('Deduplicates promise object fields', async () => {
Expand All @@ -1722,17 +1636,9 @@ describe('Execute: defer directive', () => {
nestedObject: () => Promise.resolve({ name: 'foo' }),
},
});
expectJSON(result).toDeepEqual([
{
data: { hero: { nestedObject: { name: 'foo' } } },
pending: [{ id: '0', path: ['hero'] }],
hasNext: true,
},
{
completed: [{ id: '0' }],
hasNext: false,
},
]);
expectJSON(result).toDeepEqual({
data: { hero: { nestedObject: { name: 'foo' } } },
});
});

it('Handles errors thrown in deferred fragments', async () => {
Expand Down
12 changes: 4 additions & 8 deletions src/execution/__tests__/stream-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1780,33 +1780,29 @@ describe('Execute: stream directive', () => {
nestedFriendList: [],
},
},
pending: [
{ id: '0', path: ['nestedObject'] },
{ id: '1', path: ['nestedObject', 'nestedFriendList'] },
],
pending: [{ id: '0', path: ['nestedObject', 'nestedFriendList'] }],
hasNext: true,
},
{
incremental: [
{
items: [{ id: '1', name: 'Luke' }],
id: '1',
id: '0',
},
],
completed: [{ id: '0' }],
hasNext: true,
},
{
incremental: [
{
items: [{ id: '2', name: 'Han' }],
id: '1',
id: '0',
},
],
hasNext: true,
},
{
completed: [{ id: '1' }],
completed: [{ id: '0' }],
hasNext: false,
},
]);
Expand Down
Loading
Loading