Skip to content

Commit 6819e77

Browse files
authored
Fix naming issues with custom query root type (#77)
* Fix operation name for custom root query * Update change log
1 parent c490a72 commit 6819e77

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fixed
6+
7+
- Fix operations factories name when using a custom root query #75
8+
59
## 1.2.0 - 2023-01-20
610

711
### Added

packages/graphql-codegen-factories/src/operations/FactoriesOperationsVisitor.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from "path";
22
import { getBaseType } from "@graphql-codegen/plugin-helpers";
33
import { getConfigValue } from "@graphql-codegen/visitor-plugin-common";
4-
import { camelCase } from "change-case-all";
4+
import { camelCase, pascalCase } from "change-case-all";
55
import {
66
FragmentDefinitionNode,
77
GraphQLCompositeType,
@@ -173,7 +173,10 @@ export class FactoriesOperationsVisitor extends FactoriesBaseVisitor<
173173
selection: OperationDefinitionNode | SelectionNode
174174
): NormalizedSelection[] {
175175
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+
);
177180
const name = this.convertName(this.handleAnonymousOperation(selection), {
178181
suffix: operationSuffix,
179182
});

packages/graphql-codegen-factories/src/operations/__tests__/__snapshots__/plugin.ts.snap

+26
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,32 @@ export function createCreateUserMutationMock_createUser(props: Partial<CreateUse
297297
}
298298
`;
299299

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+
300326
exports[`plugin should support external fragments 1`] = `
301327
Object {
302328
"content": "export function createGetMeQueryMock(props: Partial<GetMeQuery> = {}): GetMeQuery {

packages/graphql-codegen-factories/src/operations/__tests__/plugin.ts

+30
Original file line numberDiff line numberDiff line change
@@ -587,4 +587,34 @@ describe("plugin", () => {
587587
);
588588
expect(output).toMatchSnapshot();
589589
});
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+
});
590620
});

0 commit comments

Comments
 (0)