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

Fix hook execution order when non-nullable field throws #492

Merged
merged 2 commits into from
Dec 16, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix hook execution order when non-nullable field throws",
"packageName": "@graphitation/supermassive",
"email": "[email protected]",
"dependentChangeType": "patch"
}
38 changes: 38 additions & 0 deletions packages/supermassive/src/__tests__/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,44 @@ describe.each([
resultHasErrors: true,
isStrictHookCallsOrder: true,
},
{
name: "error in sync resolver for non-nullable field 2",
document: `query GetPersonX
{
person(id: 1) {
name
xField
}
}`,
resolvers: {
...resolvers,
Person: {
name: async () => {
// NOTE: this has to be async
return "John Doe";
},
xField: () => {
// NOTE this has to be sync, to make sure it throws before Person.name is resolved
throw new Error("Resolver error");
},
},
} as UserResolvers,
expectedHookCalls: [
"BOE|GetPersonX",
"BFR|person",
"AFR|person|[object]|undefined",
"BFR|person.name",
"BFR|person.xField",
"AFR|person.xField|undefined|Resolver error",
"AFC|person.xField|undefined|Resolver error",
"AFR|person.name|John Doe|undefined",
"AFC|person.name|John Doe|undefined",
"AFC|person|undefined|Resolver error",
"ABR|GetPersonX",
],
resultHasErrors: true,
isStrictHookCallsOrder: true,
},
{
name: "error in async resolver for non-nullable field",
document: `query GetFilm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ type Person implements Node {
films: [Film]
bubblingError: String
bubblingListError: [String!]
xField: String!
}

type Starship implements Node {
Expand Down
40 changes: 26 additions & 14 deletions packages/supermassive/src/executeWithoutSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,23 +468,33 @@ function executeFields(
const results = Object.create(null);
let containsPromise = false;

for (const [responseName, fieldGroup] of groupedFieldSet) {
const fieldPath = addPath(path, responseName, parentTypeName);
const result = executeField(
exeContext,
parentTypeName,
sourceValue,
fieldGroup,
fieldPath,
incrementalDataRecord,
);
try {
for (const [responseName, fieldGroup] of groupedFieldSet) {
const fieldPath = addPath(path, responseName, parentTypeName);
const result = executeField(
exeContext,
parentTypeName,
sourceValue,
fieldGroup,
fieldPath,
incrementalDataRecord,
);

if (result !== undefined) {
results[responseName] = result;
if (isPromise(result)) {
containsPromise = true;
if (result !== undefined) {
results[responseName] = result;
if (isPromise(result)) {
containsPromise = true;
}
}
}
} catch (error) {
if (containsPromise) {
// Ensure that any promises returned by other fields are handled, as they may also reject.
return promiseForObject(results).finally(() => {
throw error;
});
}
throw error;
}

// If there are no promises, we can just return the object
Expand Down Expand Up @@ -2513,6 +2523,7 @@ class DeferredFragmentRecord {
isCompleted: boolean;
_exeContext: ExecutionContext;
_resolve?: (arg: PromiseOrValue<ObjMap<unknown> | null>) => void;

constructor(opts: {
label: string | undefined;
path: Path | undefined;
Expand Down Expand Up @@ -2561,6 +2572,7 @@ class StreamItemsRecord {
isCompleted: boolean;
_exeContext: ExecutionContext;
_resolve?: (arg: PromiseOrValue<Array<unknown> | null>) => void;

constructor(opts: {
label: string | undefined;
path: Path | undefined;
Expand Down
Loading