Skip to content

Commit a9fc409

Browse files
tmairdotansimhan1ru4l
authored
fix: unhandled rejection in apollo plugin for apollo v3 (#6539)
Co-authored-by: Dotan Simha <[email protected]> Co-authored-by: Laurin Quast <[email protected]>
1 parent 9a5ecc7 commit a9fc409

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

.changeset/light-llamas-divide.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-hive/apollo': patch
3+
---
4+
5+
Prevent GraphQL document with selection set not satisfiable by the server to cause unhandled
6+
rejections for Apollo Server v3 (see https://github.com/graphql-hive/console/pull/4958 and https://github.com/graphql-hive/console/issues/4935).

packages/libraries/apollo/src/index.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,29 @@ export function useHive(clientOrOptions: HiveClient | HivePluginOptions): Apollo
134134
} as any;
135135
}
136136

137+
let didFailValidation = false;
138+
137139
if (isLegacyV3) {
138140
return Promise.resolve({
139141
didResolveSource() {
140142
didResolveSource = true;
141143
},
144+
async validationDidStart() {
145+
return function onErrors(errors) {
146+
if (errors === null || errors === void 0 ? void 0 : errors.length) {
147+
didFailValidation = true;
148+
}
149+
};
150+
},
142151
async willSendResponse(ctx) {
152+
if (didFailValidation) {
153+
void complete(args, {
154+
action: 'abort',
155+
reason: 'Validation failed',
156+
logging: false,
157+
});
158+
return;
159+
}
143160
if (!didResolveSource) {
144161
void complete(args, {
145162
action: 'abort',
@@ -165,8 +182,6 @@ export function useHive(clientOrOptions: HiveClient | HivePluginOptions): Apollo
165182
});
166183
}
167184

168-
let didFailValidation = false;
169-
170185
return (async () => {
171186
let persistedDocumentError: GraphQLError | null = null;
172187
let persistedDocumentHash: string | undefined;

pnpm-lock.yaml

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)