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

Add config for prefixing types #29

Merged
merged 3 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ Allows you to define mappings for your custom scalars. Allows you to map any Gra
[casual](https://github.com/boo1ean/casual#embedded-generators) embedded generator (string or
function key)

### typesPrefix (`string`, defaultValue: '')

Useful if you have globally exported types under a certain namespace.
e.g If the types file is something like this

```
declare namespace Api {
type User {
...
}
}
```

Setting the typesPrefix to `Api.` will create the following mock data

```
export const aUser = (overrides?: Partial<Api.User>): Api.User => {
```

Allows you to define mappings for your custom scalars. Allows you to map any GraphQL Scalar to a
[casual](https://github.com/boo1ean/casual#embedded-generators) embedded generator (string or
function key)
ardeois marked this conversation as resolved.
Show resolved Hide resolved

## Example of usage

**codegen.yml**
Expand Down
18 changes: 16 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,16 @@ const getMockString = (
typenamesConvention: NamingConvention,
addTypename = false,
prefix,
typesPrefix = '',
) => {
const casedName = createNameConverter(typenamesConvention)(typeName);
const typename = addTypename ? `\n __typename: '${casedName}',` : '';
return `
export const ${toMockName(typeName, casedName, prefix)} = (overrides?: Partial<${casedName}>): ${casedName} => {
export const ${toMockName(
typeName,
casedName,
prefix,
)} = (overrides?: Partial<${typesPrefix}${casedName}>): ${typesPrefix}${casedName} => {
return {${typename}
${fields}
};
Expand All @@ -212,6 +217,7 @@ export interface TypescriptMocksPluginConfig {
addTypename?: boolean;
prefix?: string;
scalars?: ScalarMap;
typesPrefix?: string;
}

interface TypeItem {
Expand Down Expand Up @@ -301,7 +307,14 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
.join('\n')
: '';

return getMockString(fieldName, mockFields, typenamesConvention, false, config.prefix);
return getMockString(
fieldName,
mockFields,
typenamesConvention,
false,
config.prefix,
config.typesPrefix,
);
},
};
},
Expand All @@ -325,6 +338,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
typenamesConvention,
!!config.addTypename,
config.prefix,
config.typesPrefix,
);
},
};
Expand Down
37 changes: 37 additions & 0 deletions tests/__snapshots__/typescript-mock-data.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,43 @@ export const mockUser = (overrides?: Partial<User>): User => {
"
`;

exports[`should add typesPrefix to all types when option is specified 1`] = `
"
export const anAbcType = (overrides?: Partial<Api.AbcType>): Api.AbcType => {
return {
abc: overrides && overrides.hasOwnProperty('abc') ? overrides.abc! : 'sit',
};
};

export const anAvatar = (overrides?: Partial<Api.Avatar>): Api.Avatar => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '0550ff93-dd31-49b4-8c38-ff1cb68bdc38',
url: overrides && overrides.hasOwnProperty('url') ? overrides.url! : 'aliquid',
};
};

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

export const aUser = (overrides?: Partial<Api.User>): Api.User => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 'a5756f00-41a6-422a-8a7d-d13ee6a63750',
creationDate: overrides && overrides.hasOwnProperty('creationDate') ? overrides.creationDate! : '1970-01-09T16:33:21.532Z',
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'libero',
avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : anAvatar(),
status: overrides && overrides.hasOwnProperty('status') ? overrides.status! : Status.Online,
customStatus: overrides && overrides.hasOwnProperty('customStatus') ? overrides.customStatus! : AbcStatus.HasXyzStatus,
scalarValue: overrides && overrides.hasOwnProperty('scalarValue') ? overrides.scalarValue! : 'neque',
};
};
"
`;

exports[`should correctly generate the \`casual\` data for a non-string scalar mapping 1`] = `
"
export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
Expand Down
9 changes: 9 additions & 0 deletions tests/typescript-mock-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,12 @@ it('should correctly generate the `casual` data for a non-string scalar mapping'
expect(result).toContain(JSON.stringify([41, 98, 185]));
expect(result).toMatchSnapshot();
});

it('should add typesPrefix to all types when option is specified', async () => {
const result = await plugin(testSchema, [], { typesPrefix: 'Api.' });

expect(result).toBeDefined();
expect(result).toMatch(/: Api.User/);
expect(result).not.toMatch(/: User/);
expect(result).toMatchSnapshot();
});