@@ -19,6 +19,21 @@ export function assertInvalidParams(
1919 } ,
2020 } ) ;
2121 }
22+
23+ if (
24+ 'operationName' in params &&
25+ typeof params . operationName !== 'string' &&
26+ params . operationName != null
27+ ) {
28+ throw createGraphQLError ( `Invalid operation name in the request body.` , {
29+ extensions : {
30+ http : {
31+ status : 400 ,
32+ } ,
33+ } ,
34+ } ) ;
35+ }
36+
2237 for ( const paramKey in params ) {
2338 if ( ( params as Record < string , unknown > ) [ paramKey ] == null ) {
2439 continue ;
@@ -134,6 +149,61 @@ export function isValidGraphQLParams(params: unknown): params is GraphQLParams {
134149 }
135150}
136151
152+ export function checkOperationName (
153+ operationName : string | undefined ,
154+ document : DocumentNode ,
155+ ) : void {
156+ const operations = listOperations ( document ) ;
157+
158+ if ( operationName != null ) {
159+ for ( const operation of operations ) {
160+ if ( operation . name ?. value === operationName ) {
161+ return ;
162+ }
163+ }
164+
165+ throw createGraphQLError (
166+ `Could not determine what operation to execute. There is no operation "${ operationName } " in the query.` ,
167+ {
168+ extensions : {
169+ http : {
170+ spec : true ,
171+ status : 400 ,
172+ } ,
173+ } ,
174+ } ,
175+ ) ;
176+ }
177+
178+ operations . next ( ) ;
179+ // If there is no operation name, we should have only one operation
180+ if ( ! operations . next ( ) . done ) {
181+ throw createGraphQLError (
182+ `Could not determine what operation to execute. The query contains multiple operation, an operation name should be provided.` ,
183+ {
184+ extensions : {
185+ http : {
186+ spec : true ,
187+ status : 400 ,
188+ } ,
189+ } ,
190+ } ,
191+ ) ;
192+ }
193+ }
194+
195+ export function isValidOperationName (
196+ operationName : string | undefined ,
197+ document : DocumentNode ,
198+ ) : boolean {
199+ try {
200+ checkOperationName ( operationName , document ) ;
201+ return true ;
202+ } catch {
203+ return false ;
204+ }
205+ }
206+
137207export function useCheckGraphQLQueryParams ( extraParamNames ?: string [ ] ) : Plugin {
138208 return {
139209 onParams ( { params } ) {
@@ -148,44 +218,7 @@ export function useCheckGraphQLQueryParams(extraParamNames?: string[]): Plugin {
148218 return ;
149219 }
150220
151- const { params : { operationName } = { } } = context ;
152- const operations = listOperations ( result ) ;
153-
154- if ( operationName != null ) {
155- for ( const operation of operations ) {
156- if ( operation . name ?. value === operationName ) {
157- return ;
158- }
159- }
160-
161- throw createGraphQLError (
162- `Could not determine what operation to execute. There is no operation "${ operationName } " in the query.` ,
163- {
164- extensions : {
165- http : {
166- spec : true ,
167- status : 400 ,
168- } ,
169- } ,
170- } ,
171- ) ;
172- }
173-
174- operations . next ( ) ;
175- // If there is no operation name, we should have only one operation
176- if ( ! operations . next ( ) . done ) {
177- throw createGraphQLError (
178- `Could not determine what operation to execute. There is no operation "${ operationName } " in the query.` ,
179- {
180- extensions : {
181- http : {
182- spec : true ,
183- status : 400 ,
184- } ,
185- } ,
186- } ,
187- ) ;
188- }
221+ checkOperationName ( context . params . operationName , result ) ;
189222 } ;
190223 } ,
191224 } ;
0 commit comments