Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle input-object-type definitions #5

Merged
merged 3 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,16 @@ const generateMockValue = (
}
};

// eslint-disable-next-line @typescript-eslint/no-empty-interface
const getMockString = (typeName: string, fields: string) => {
return `
export const ${toMockName(typeName)} = (overrides?: Partial<${typeName}>): ${typeName} => {
return {
${fields}
...overrides
};
};`;
}

export interface TypescriptMocksPluginConfig {
typesFile?: string;
}
Expand Down Expand Up @@ -138,11 +147,27 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
},
};
},
InputObjectTypeDefinition: node => {
const fieldName = node.name.value;

return {
typeName: fieldName,
mockFn: () => {
const mockFields = node.fields ? node.fields.map((field) => {
const value = generateMockValue(fieldName, field.name.value, types, field.type);

return ` ${field.name.value}: ${value},`;
}).join('\n') : '';

return getMockString(fieldName, mockFields);
},
};
},
ObjectTypeDefinition: node => {
// This function triggered per each type
const typeName = node.name.value;

if (typeName === 'Query') {
if (typeName === 'Query' || typeName === 'Mutation') {
return null;
}

Expand All @@ -152,13 +177,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
mockFn: () => {
const mockFields = fields ? fields.map(({ mockFn }: any) => mockFn(typeName)).join('\n') : '';

return `
export const ${toMockName(typeName)} = (overrides?: Partial<${typeName}>): ${typeName} => {
return {
${mockFields}
...overrides
};
};`;
return getMockString(typeName, mockFields);
},
};
},
Expand Down
20 changes: 19 additions & 1 deletion tests/__snapshots__/typescript-mock-data.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
};
};

export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
return {
id: '1d6a9360-c92b-4660-8e5f-04155047bddc',
login: 'qui',
avatar: anAvatar(),
...overrides
};
};

export const aUser = (overrides?: Partial<User>): User => {
return {
id: 'a5756f00-41a6-422a-8a7d-d13ee6a63750',
Expand All @@ -23,7 +32,7 @@ export const aUser = (overrides?: Partial<User>): User => {

exports[`should generate mock data functions with external types file import 1`] = `
"/* eslint-disable @typescript-eslint/no-use-before-define,@typescript-eslint/no-unused-vars */
import { Avatar, User } from './types/graphql';
import { Avatar, UpdateUserInput, User } from './types/graphql';

export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
return {
Expand All @@ -33,6 +42,15 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
};
};

export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
return {
id: '1d6a9360-c92b-4660-8e5f-04155047bddc',
login: 'qui',
avatar: anAvatar(),
...overrides
};
};

export const aUser = (overrides?: Partial<User>): User => {
return {
id: 'a5756f00-41a6-422a-8a7d-d13ee6a63750',
Expand Down
14 changes: 12 additions & 2 deletions tests/typescript-mock-data.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '@graphql-codegen/testing';

import { buildSchema, print } from 'graphql';
import { buildSchema } from 'graphql';
import { plugin } from '../src';

const testSchema = buildSchema(/* GraphQL */ `
Expand All @@ -18,6 +18,16 @@ const testSchema = buildSchema(/* GraphQL */ `
type Query {
user: User!
}

input UpdateUserInput {
id: ID!
login: String
avatar: Avatar
}

type Mutation {
updateUser(user: UpdateUserInput): User
}
`);

it('can be called', async () => {
Expand All @@ -35,6 +45,6 @@ it('should generate mock data functions with external types file import', async
const result = await plugin(testSchema, [], { typesFile: './types/graphql.ts' });

expect(result).toBeDefined();
expect(result).toContain("import { Avatar, User } from './types/graphql';");
expect(result).toContain("import { Avatar, UpdateUserInput, User } from './types/graphql';");
expect(result).toMatchSnapshot();
});