Skip to content

Commit

Permalink
Fix enums initial value
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouzi committed Jan 20, 2021
1 parent 43a9091 commit b7814e8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/FactoriesVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ export class FactoriesVisitor extends BaseVisitor<
return this.config.enumsAsTypes
? `"${this.enums[name].getValues()[0].value}"`
: `${name}.${this.convertName(
this.enums[name].getValues()[0].name
this.enums[name].getValues()[0].name,
{
transformUnderscore: true,
}
)}`;
}

Expand Down
6 changes: 6 additions & 0 deletions src/__integration__/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
enum UserRole {
SUPER_ADMIN
ADMIN
}

type User {
id: ID!
role: UserRole!
}

type Query {
Expand Down
11 changes: 10 additions & 1 deletion src/__integration__/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export type Maybe<T> = T | null;
export type Exact<T extends { [key: string]: any }> = { [K in keyof T]: T[K] };
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
Expand All @@ -9,9 +11,15 @@ export type Scalars = {
Float: number;
};

export enum UserRole {
SuperAdmin = 'SUPER_ADMIN',
Admin = 'ADMIN'
}

export type User = {
__typename?: 'User';
id: Scalars['ID'];
role: UserRole;
};

export type Query = {
Expand All @@ -23,6 +31,7 @@ export function createUserMock(props: Partial<User>): User {
return {
__typename: "User",
id: "",
role: UserRole.SuperAdmin,
...props,
};
}
11 changes: 11 additions & 0 deletions src/__tests__/__snapshots__/plugin.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ exports[`plugin should not create factories for Query and Mutation 1`] = `
"
`;
exports[`plugin should support enums with an underscore 1`] = `
"export function createUserMock(props: Partial<User>): User {
return {
__typename: \\"User\\",
role: UserRole.SuperAdmin,
...props,
};
}
"
`;
exports[`plugin should use enums as types 1`] = `
"export function createUserMock(props: Partial<User>): User {
return {
Expand Down
13 changes: 13 additions & 0 deletions src/__tests__/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,17 @@ describe("plugin", () => {
})
).toMatchSnapshot();
});

it("should support enums with an underscore", () => {
const schema = buildSchema(`
enum UserRole {
SUPER_ADMIN
ADMIN
}
type User {
role: UserRole!
}
`);
expect(plugin(schema, [], {})).toMatchSnapshot();
});
});

0 comments on commit b7814e8

Please sign in to comment.