Skip to content

Commit

Permalink
fix(GraphQLHandler): move cookie parsing to parse phase (#1957)
Browse files Browse the repository at this point in the history
Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
  • Loading branch information
mattcosta7 and kettanaito committed Jan 12, 2024
1 parent 8624f31 commit ba87f99
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/core/handlers/GraphQLHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ describe('parse', () => {
})

expect(await handler.parse({ request })).toEqual({
cookies: {},
match: {
matches: true,
params: {
Expand Down Expand Up @@ -188,6 +189,7 @@ describe('parse', () => {
})

expect(await handler.parse({ request })).toEqual({
cookies: {},
match: {
matches: true,
params: {
Expand Down Expand Up @@ -215,6 +217,7 @@ describe('parse', () => {
})

expect(await handler.parse({ request })).toEqual({
cookies: {},
match: {
matches: true,
params: {
Expand Down Expand Up @@ -243,6 +246,7 @@ describe('parse', () => {
})

expect(await handler.parse({ request })).toEqual({
cookies: {},
match: {
matches: true,
params: {
Expand Down Expand Up @@ -272,6 +276,7 @@ describe('parse', () => {
})

expect(await handler.parse({ request })).toEqual({
cookies: {},
match: {
matches: true,
params: {
Expand Down Expand Up @@ -300,6 +305,7 @@ describe('parse', () => {
})

expect(await handler.parse({ request })).toEqual({
cookies: {},
match: {
matches: true,
params: {
Expand Down Expand Up @@ -327,6 +333,7 @@ describe('parse', () => {
})

expect(await handler.parse({ request })).toEqual({
cookies: {},
match: {
matches: true,
params: {
Expand Down Expand Up @@ -355,6 +362,7 @@ describe('parse', () => {
})

expect(await handler.parse({ request })).toEqual({
cookies: {},
match: {
matches: true,
params: {
Expand Down Expand Up @@ -393,6 +401,7 @@ describe('parse', () => {
),
}),
).resolves.toEqual({
cookies: {},
match: {
matches: true,
params: {},
Expand All @@ -418,6 +427,7 @@ describe('parse', () => {
),
}),
).resolves.toEqual({
cookies: {},
match: {
matches: true,
params: {},
Expand Down Expand Up @@ -452,6 +462,7 @@ describe('parse', () => {
),
}),
).resolves.toEqual({
cookies: {},
match: {
matches: false,
params: {},
Expand All @@ -471,6 +482,7 @@ describe('parse', () => {
),
}),
).resolves.toEqual({
cookies: {},
match: {
matches: false,
params: {},
Expand Down Expand Up @@ -499,6 +511,7 @@ describe('parse', () => {
),
}),
).resolves.toEqual({
cookies: {},
match: {
matches: false,
params: {},
Expand All @@ -518,6 +531,7 @@ describe('parse', () => {
),
}),
).resolves.toEqual({
cookies: {},
match: {
matches: false,
params: {},
Expand Down Expand Up @@ -726,6 +740,7 @@ describe('run', () => {

expect(result!.handler).toEqual(handler)
expect(result!.parsedResult).toEqual({
cookies: {},
match: {
matches: true,
params: {
Expand Down
12 changes: 7 additions & 5 deletions src/core/handlers/GraphQLHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface GraphQLHandlerInfo extends RequestHandlerDefaultInfo {

export type GraphQLRequestParsedResult = {
match: Match
cookies: Record<string, string>
} & (
| ParsedGraphQLRequest<GraphQLVariables>
/**
Expand Down Expand Up @@ -166,20 +167,23 @@ export class GraphQLHandler extends RequestHandler<
* need to parse it since there's no case where we would handle this
*/
const match = matchRequestUrl(new URL(args.request.url), this.endpoint)
const cookies = getAllRequestCookies(args.request)

if (!match.matches) {
return { match }
return { match, cookies }
}

const parsedResult = await this.parseGraphQLRequestOrGetFromCache(
args.request,
)

if (typeof parsedResult === 'undefined') {
return { match }
return { match, cookies }
}

return {
match,
cookies,
query: parsedResult.query,
operationType: parsedResult.operationType,
operationName: parsedResult.operationName,
Expand Down Expand Up @@ -225,13 +229,11 @@ Consider naming this operation or using "graphql.operation()" request handler to
request: Request
parsedResult: GraphQLRequestParsedResult
}) {
const cookies = getAllRequestCookies(args.request)

return {
query: args.parsedResult.query || '',
operationName: args.parsedResult.operationName || '',
variables: args.parsedResult.variables || {},
cookies,
cookies: args.parsedResult.cookies,
}
}

Expand Down

0 comments on commit ba87f99

Please sign in to comment.