File tree 4 files changed +65
-2
lines changed
packages/graphql-codegen-factories/src/operations
4 files changed +65
-2
lines changed Original file line number Diff line number Diff line change 2
2
3
3
## Unreleased
4
4
5
+ ### Fixed
6
+
7
+ - Fix operations factories name when using a custom root query #75
8
+
5
9
## 1.2.0 - 2023-01-20
6
10
7
11
### Added
Original file line number Diff line number Diff line change 1
1
import path from "path" ;
2
2
import { getBaseType } from "@graphql-codegen/plugin-helpers" ;
3
3
import { getConfigValue } from "@graphql-codegen/visitor-plugin-common" ;
4
- import { camelCase } from "change-case-all" ;
4
+ import { camelCase , pascalCase } from "change-case-all" ;
5
5
import {
6
6
FragmentDefinitionNode ,
7
7
GraphQLCompositeType ,
@@ -173,7 +173,10 @@ export class FactoriesOperationsVisitor extends FactoriesBaseVisitor<
173
173
selection : OperationDefinitionNode | SelectionNode
174
174
) : NormalizedSelection [ ] {
175
175
if ( selection . kind === Kind . OPERATION_DEFINITION ) {
176
- const operationSuffix = this . getOperationSuffix ( selection , parent . name ) ;
176
+ const operationSuffix = this . getOperationSuffix (
177
+ selection ,
178
+ pascalCase ( selection . operation )
179
+ ) ;
177
180
const name = this . convertName ( this . handleAnonymousOperation ( selection ) , {
178
181
suffix : operationSuffix ,
179
182
} ) ;
Original file line number Diff line number Diff line change @@ -297,6 +297,32 @@ export function createCreateUserMutationMock_createUser(props: Partial<CreateUse
297
297
}
298
298
` ;
299
299
300
+ exports [` plugin should support custom root Query 1` ] = `
301
+ Object {
302
+ " content" : " export function createGetUserQueryMock(props: Partial<GetUserQuery> = {}): GetUserQuery {
303
+ return {
304
+ __typename: \\" CustomQuery\\ " ,
305
+ user: null ,
306
+ ... props ,
307
+ };
308
+ }
309
+
310
+ export function createGetUserQueryMock_user(props: Partial<NonNullable<GetUserQuery[\\ "user\\ "]>> = { } ): NonNullable<GetUserQuery[\\ "user\\ "]> {
311
+ const user = schemaFactories .createUserMock ({
312
+ username: props .username ,
313
+ });
314
+ return {
315
+ __typename: \\" User\\ " ,
316
+ username: user .username ,
317
+ ... props ,
318
+ };
319
+ } ",
320
+ "prepend": Array [
321
+ "import * as schemaFactories from \\ "./factories\\ ";",
322
+ ],
323
+ }
324
+ ` ;
325
+
300
326
exports [` plugin should support external fragments 1` ] = `
301
327
Object {
302
328
" content" : " export function createGetMeQueryMock(props: Partial<GetMeQuery> = {}): GetMeQuery {
Original file line number Diff line number Diff line change @@ -587,4 +587,34 @@ describe("plugin", () => {
587
587
) ;
588
588
expect ( output ) . toMatchSnapshot ( ) ;
589
589
} ) ;
590
+
591
+ it ( "should support custom root Query" , async ( ) => {
592
+ const schema = buildSchema ( /* GraphQL */ `
593
+ type User {
594
+ username: String!
595
+ }
596
+
597
+ type CustomQuery {
598
+ user: User
599
+ }
600
+
601
+ schema {
602
+ query: CustomQuery
603
+ }
604
+ ` ) ;
605
+ const ast = parse ( /* GraphQL */ `
606
+ query GetUser {
607
+ user {
608
+ username
609
+ }
610
+ }
611
+ ` ) ;
612
+
613
+ const output = await plugin (
614
+ schema ,
615
+ [ { location : "GetUser.graphql" , document : ast } ] ,
616
+ { schemaFactoriesPath : "./factories" }
617
+ ) ;
618
+ expect ( output ) . toMatchSnapshot ( ) ;
619
+ } ) ;
590
620
} ) ;
You can’t perform that action at this time.
0 commit comments