Skip to content

Commit 3a7ef74

Browse files
authored
Improve Fastify integration (#3930)
* Improve Fastify integration * Go * Fix tests
1 parent ff7c23b commit 3a7ef74

File tree

12 files changed

+130
-196
lines changed

12 files changed

+130
-196
lines changed

.changeset/neat-goats-see.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@graphql-yoga/apollo-managed-federation': patch
3+
'@graphql-yoga/plugin-apollo-inline-trace': patch
4+
'@graphql-yoga/plugin-defer-stream': patch
5+
'@graphql-yoga/plugin-jwt': patch
6+
'graphql-yoga': patch
7+
'@graphql-yoga/subscription': patch
8+
---
9+
10+
Bump `@whatwg-node/server`

examples/fastify/src/app.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,11 @@ export function buildApp(logging = true) {
118118
app.route({
119119
url: graphQLServer.graphqlEndpoint,
120120
method: ['GET', 'POST', 'OPTIONS'],
121-
handler: async (req, reply) => {
122-
const response = await graphQLServer.handleNodeRequestAndResponse(req, reply, {
121+
handler: (req, reply) =>
122+
graphQLServer.handleNodeRequestAndResponse(req, reply, {
123123
req,
124124
reply,
125-
});
126-
for (const [name, value] of response.headers) {
127-
reply.header(name, value);
128-
}
129-
130-
reply.status(response.status);
131-
132-
reply.send(response.body);
133-
134-
return reply;
135-
},
125+
}),
136126
});
137127

138128
return [app, graphQLServer.graphqlEndpoint] as const;

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ if (process.env.LEAKS_TEST === 'true') {
4545
testMatch.push('!**/dist/**', '!**/.bob/**');
4646

4747
module.exports = {
48+
prettierPath: null,
4849
testEnvironment: 'node',
4950
rootDir: ROOT_DIR,
5051
restoreMocks: true,

packages/graphql-yoga/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"@graphql-yoga/subscription": "workspace:^",
5959
"@whatwg-node/fetch": "^0.10.5",
6060
"@whatwg-node/promise-helpers": "^1.2.4",
61-
"@whatwg-node/server": "^0.10.1",
61+
"@whatwg-node/server": "^0.10.2",
6262
"dset": "^3.1.4",
6363
"lru-cache": "^10.0.0",
6464
"tslib": "^2.8.1"

packages/plugins/apollo-inline-trace/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"@graphql-tools/delegate": "^10.1.1",
5353
"@graphql-tools/federation": "^3.0.0",
5454
"@whatwg-node/fetch": "^0.10.1",
55-
"@whatwg-node/server": "^0.10.1",
55+
"@whatwg-node/server": "^0.10.2",
5656
"graphql": "16.10.0",
5757
"graphql-yoga": "workspace:*"
5858
},

packages/plugins/apollo-managed-federation/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
},
5757
"devDependencies": {
5858
"@whatwg-node/fetch": "^0.10.1",
59-
"@whatwg-node/server": "^0.10.1",
59+
"@whatwg-node/server": "^0.10.2",
6060
"graphql": "16.10.0",
6161
"graphql-yoga": "workspace:^",
6262
"typescript": "5.8.2"

packages/plugins/defer-stream/__tests__/defer-stream.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,14 @@ Content-Length: 44
149149
{"data":{"stream":["A","B"]},"hasNext":true}
150150
---
151151
Content-Type: application/json; charset=utf-8
152-
Content-Length: 69
152+
Content-Length: 68
153153
154-
{"incremental":[{"items":["C"],"path":["stream",2]}],"hasNext":false}
154+
{"incremental":[{"items":["C"],"path":["stream",2]}],"hasNext":true}
155+
---
156+
Content-Type: application/json; charset=utf-8
157+
Content-Length: 17
158+
159+
{"hasNext":false}
155160
-----
156161
"
157162
`);

packages/plugins/defer-stream/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"devDependencies": {
4747
"@graphql-tools/executor-http": "^1.1.9",
4848
"@whatwg-node/fetch": "^0.10.1",
49-
"@whatwg-node/server": "^0.10.1",
49+
"@whatwg-node/server": "^0.10.2",
5050
"fetch-multipart-graphql": "3.2.2",
5151
"graphql": "16.10.0",
5252
"graphql-yoga": "workspace:*",

packages/plugins/jwt/src/plugin.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ export function useJWT(options: JwtPluginOptions): Plugin<{
2929
const payloadByContext = new WeakMap<object, PluginPayload>();
3030
const payloadByRequest = new WeakMap<Request, PluginPayload>();
3131
const validatedRequestAndContextSet = new WeakSet<object>();
32+
33+
function handleError(e: unknown) {
34+
// User-facing errors should be handled based on the configuration.
35+
// These errors are handled based on the value of "reject.invalidToken" config.
36+
if (e instanceof GraphQLError) {
37+
if (normalizedOptions.reject.invalidToken) {
38+
throw e;
39+
}
40+
41+
return null;
42+
}
43+
44+
// Server/internal errors should be thrown, so they can be handled by the error handler and be masked.
45+
throw e;
46+
}
47+
3248
const lookupToken = (payload: ExtractTokenFunctionParams) => {
3349
const iterator = normalizedOptions.tokenLookupLocations[Symbol.iterator]();
3450
function iterate(): MaybePromise<{
@@ -50,6 +66,7 @@ export function useJWT(options: JwtPluginOptions): Plugin<{
5066
}
5167
return iterate();
5268
},
69+
handleError,
5370
);
5471
}
5572
return iterate();
@@ -82,21 +99,6 @@ export function useJWT(options: JwtPluginOptions): Plugin<{
8299
return iterate();
83100
};
84101

85-
function handleError(e: unknown) {
86-
// User-facing errors should be handled based on the configuration.
87-
// These errors are handled based on the value of "reject.invalidToken" config.
88-
if (e instanceof GraphQLError) {
89-
if (normalizedOptions.reject.invalidToken) {
90-
throw e;
91-
}
92-
93-
return;
94-
}
95-
96-
// Server/internal errors should be thrown, so they can be handled by the error handler and be masked.
97-
throw e;
98-
}
99-
100102
const lookupAndValidate = (payload: ExtractTokenFunctionParams) => {
101103
// Mark the context and request as validated, so we don't process them again.
102104
if (payload.serverContext) {
@@ -132,15 +134,20 @@ export function useJWT(options: JwtPluginOptions): Plugin<{
132134
decodedToken = jsonwebtoken.decode(lookupResult.token, { complete: true });
133135
} catch (e) {
134136
logger.warn(`Failed to decode JWT authentication token: `, e);
135-
throw badRequestError(`Invalid authentication token provided`);
137+
if (normalizedOptions.reject.invalidToken) {
138+
throw badRequestError(`Invalid authentication token provided`);
139+
}
140+
return null;
136141
}
137142

138143
if (!decodedToken) {
139144
logger.warn(
140145
`Failed to extract payload from incoming token, please make sure the token is a valid JWT.`,
141146
);
142-
143-
throw badRequestError(`Invalid authentication token provided`);
147+
if (normalizedOptions.reject.invalidToken) {
148+
throw badRequestError(`Invalid authentication token provided`);
149+
}
150+
return null;
144151
}
145152

146153
// Fetch the signing key based on the key id.

packages/subscription/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"devDependencies": {
6666
"@graphql-yoga/redis-event-target": "workspace:*",
6767
"@types/ioredis-mock": "8.2.5",
68-
"@whatwg-node/server": "^0.10.1",
68+
"@whatwg-node/server": "^0.10.2",
6969
"ioredis-mock": "8.9.0"
7070
},
7171
"publishConfig": {

0 commit comments

Comments
 (0)