Skip to content
Draft
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
63 changes: 63 additions & 0 deletions packages/supermassive/src/__tests__/executeWithSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,69 @@ describe("executeWithSchema - @defer behavior", () => {
);
}

const topLevelDeferDefinitions = parse(`
type Query {
critical: String
deferred: String
}
`);

const topLevelDeferDocument = parse(`
{
critical
... @defer {
deferred
}
}
`);

test("emits top-level deferred patches when early execution is disabled and initial fields are synchronous", async () => {
const deferred = createDeferred<string>();

const result = await Promise.resolve(
executeWithSchema({
document: topLevelDeferDocument,
definitions: topLevelDeferDefinitions,
resolvers: {
Query: {
critical: () => "critical",
deferred: () => deferred.promise,
},
},
}),
);

expect(result).toMatchObject({
initialResult: {
data: {
critical: "critical",
},
hasNext: true,
},
});

if (!("initialResult" in result)) {
throw new Error("Expected an incremental result");
}

const subsequentResultPromise = result.subsequentResults.next();
deferred.resolve("deferred");

await expect(subsequentResultPromise).resolves.toMatchObject({
value: {
incremental: [
{
data: {
deferred: "deferred",
},
},
],
hasNext: false,
},
done: false,
});
});

test("includes deferred fields in the initial response when they complete within the merge timeout", async () => {
const result = await executeTestQuery(
{
Expand Down
34 changes: 20 additions & 14 deletions packages/supermassive/src/executeWithoutSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,16 @@ function executeOperation(
undefined,
exeContext.enableEarlyExecution ? patches : undefined,
);
if (!exeContext.enableEarlyExecution) {
startExecutingPatches(
exeContext,
rootTypeName,
rootValue,
path,
patches,
undefined,
);
}
result = buildResponse(exeContext, result);
break;
case "mutation":
Expand All @@ -352,6 +362,16 @@ function executeOperation(
groupedFieldSet,
exeContext.enableEarlyExecution ? patches : undefined,
);
if (!exeContext.enableEarlyExecution) {
startExecutingPatches(
exeContext,
rootTypeName,
rootValue,
path,
patches,
undefined,
);
}
result = buildResponse(exeContext, result);
break;
case "subscription": {
Expand All @@ -372,20 +392,6 @@ function executeOperation(
);
}

if (!exeContext.enableEarlyExecution) {
for (const patch of patches) {
const { label, groupedFieldSet: patchGroupedFieldSet } = patch;
executeDeferredFragment(
exeContext,
rootTypeName,
rootValue,
patchGroupedFieldSet,
label,
path,
);
}
}

return result;
} catch (error) {
exeContext.errors.push(error as GraphQLError);
Expand Down