Skip to content

Commit 56eff8c

Browse files
committed
take review in account
1 parent 6d841c5 commit 56eff8c

File tree

2 files changed

+47
-32
lines changed

2 files changed

+47
-32
lines changed

packages/graphql-yoga/src/plugins/request-validation/use-check-graphql-query-params.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -154,38 +154,43 @@ export function useCheckGraphQLQueryParams(extraParamNames?: string[]): Plugin {
154154
return;
155155
}
156156

157-
let message: string | undefined;
157+
let numberOfOperations = 0;
158158

159-
const operations = result.definitions.filter(
160-
definition => definition.kind === Kind.OPERATION_DEFINITION,
161-
);
162-
163-
if (operationName) {
164-
const operationExists = operations.some(
165-
operation => operation.name?.value === operationName,
166-
);
159+
for (const definition of result.definitions) {
160+
if (definition.kind === Kind.OPERATION_DEFINITION) {
161+
if (definition.name?.value === operationName) {
162+
return;
163+
}
167164

168-
if (!operationExists) {
169-
if (operations.length === 1) {
170-
message = `Operation name "${operationName}" doesn't match the name defined in the query.`;
171-
} else {
172-
message = `Could not determine what operation to execute. There is no operation "${operationName}" in the query.`;
165+
numberOfOperations++;
166+
167+
if (operationName == null && numberOfOperations > 1) {
168+
throw createGraphQLError(
169+
'Could not determine what operation to execute. The query contains multiple operations, an operation name must be provided',
170+
{
171+
extensions: {
172+
http: {
173+
status: 400,
174+
},
175+
},
176+
},
177+
);
173178
}
174179
}
175-
} else if (operations.length > 1) {
176-
message =
177-
'Could not determine what operation to execute. The query contains multiple operations, an operation name must be provided';
178180
}
179181

180-
if (message) {
181-
throw createGraphQLError(message, {
182+
throw createGraphQLError(
183+
numberOfOperations === 1
184+
? `Operation name "${operationName}" doesn't match the name defined in the query.`
185+
: `Could not determine what operation to execute. There is no operation "${operationName}" in the query.`,
186+
{
182187
extensions: {
183188
http: {
184189
status: 400,
185190
},
186191
},
187-
});
188-
}
192+
},
193+
);
189194
};
190195
},
191196
};

packages/graphql-yoga/src/plugins/request-validation/use-prevent-mutation-via-get.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
import { DocumentNode, GraphQLError, Kind } from 'graphql';
1+
import { DocumentNode, getOperationAST, GraphQLError, OperationDefinitionNode } from 'graphql';
22
import { Maybe } from '@envelop/core';
3-
import { createGraphQLError, isDocumentNode } from '@graphql-tools/utils';
3+
import { createGraphQLError } from '@graphql-tools/utils';
44
import type { YogaInitialContext } from '../../types.js';
55
import type { Plugin } from '../types.js';
66

7-
export function assertMutationViaGet(method: string, document: Maybe<DocumentNode>) {
8-
const isMutation =
9-
document?.definitions.find(def => def.kind === Kind.OPERATION_DEFINITION)?.operation ===
10-
'mutation';
7+
export function assertMutationViaGet(
8+
method: string,
9+
document: Maybe<DocumentNode>,
10+
operationName?: string,
11+
) {
12+
const operation: OperationDefinitionNode | undefined = document
13+
? getOperationAST(document, operationName) ?? undefined
14+
: undefined;
1115

12-
if (isMutation && method === 'GET') {
16+
if (operation?.operation === 'mutation' && method === 'GET') {
1317
throw createGraphQLError('Can only perform a mutation operation from a POST request.', {
1418
extensions: {
1519
http: {
@@ -27,7 +31,15 @@ export function usePreventMutationViaGET(): Plugin<YogaInitialContext> {
2731
return {
2832
onParse() {
2933
// We should improve this by getting Yoga stuff from the hook params directly instead of the context
30-
return ({ result, context: { request } }) => {
34+
return ({
35+
result,
36+
context: {
37+
request,
38+
// the `params` might be missing in cases where the user provided
39+
// malformed context to getEnveloped (like `yoga.getEnveloped({})`)
40+
params: { operationName } = {},
41+
},
42+
}) => {
3143
// Run only if this is a Yoga request
3244
// the `request` might be missing when using graphql-ws for example
3345
// in which case throwing an error would abruptly close the socket
@@ -45,9 +57,7 @@ export function usePreventMutationViaGET(): Plugin<YogaInitialContext> {
4557
throw result;
4658
}
4759

48-
if (isDocumentNode(result)) {
49-
assertMutationViaGet(request.method, result);
50-
}
60+
assertMutationViaGet(request.method, result, operationName);
5161
};
5262
},
5363
};

0 commit comments

Comments
 (0)