-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
scalars
config option for custom GraphQL [scalar -> casua…
…l] mappings (#21) Some apps use custom scalar for date-related stuff. This caused issues with the way `graphql-codegen-typescript-mock-data` handles autogenerated data, since a "random string" (the current default), may not always be what you need. This PR allows you to define mapping from your custom scalars to a casual embedded generator, allowing you to specify exactly how your scalar's autogenerated value will be populated Closes #20 Co-authored-by: Corentin Ardeois <[email protected]>
- Loading branch information
Showing
4 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,80 @@ export const mockUser = (overrides?: Partial<User>): User => { | |
" | ||
`; | ||
exports[`should correctly generate the \`casual\` data for a non-string scalar mapping 1`] = ` | ||
" | ||
export const anAbcType = (overrides?: Partial<AbcType>): AbcType => { | ||
return { | ||
abc: overrides && overrides.hasOwnProperty('abc') ? overrides.abc! : 'sit', | ||
}; | ||
}; | ||
export const anAvatar = (overrides?: Partial<Avatar>): Avatar => { | ||
return { | ||
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '0550ff93-dd31-49b4-8c38-ff1cb68bdc38', | ||
url: overrides && overrides.hasOwnProperty('url') ? overrides.url! : 'aliquid', | ||
}; | ||
}; | ||
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): 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<User>): 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! : [41,98,185], | ||
}; | ||
}; | ||
" | ||
`; | ||
exports[`should correctly generate the \`casual\` data for a scalar mapping of type string 1`] = ` | ||
" | ||
export const anAbcType = (overrides?: Partial<AbcType>): AbcType => { | ||
return { | ||
abc: overrides && overrides.hasOwnProperty('abc') ? overrides.abc! : 'sit', | ||
}; | ||
}; | ||
export const anAvatar = (overrides?: Partial<Avatar>): Avatar => { | ||
return { | ||
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '0550ff93-dd31-49b4-8c38-ff1cb68bdc38', | ||
url: overrides && overrides.hasOwnProperty('url') ? overrides.url! : 'aliquid', | ||
}; | ||
}; | ||
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): 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<User>): 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! : '[email protected]', | ||
}; | ||
}; | ||
" | ||
`; | ||
exports[`should generate mock data functions 1`] = ` | ||
" | ||
export const anAbcType = (overrides?: Partial<AbcType>): AbcType => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -177,3 +177,19 @@ it('should add custom prefix if the `prefix` config option is specified', async | |
expect(result).not.toMatch(/const aUser/); | ||
expect(result).toMatchSnapshot(); | ||
}); | ||
|
||
it('should correctly generate the `casual` data for a scalar mapping of type string', async () => { | ||
const result = await plugin(testSchema, [], { scalars: { AnyObject: 'email' } }); | ||
|
||
expect(result).toBeDefined(); | ||
expect(result).toContain('[email protected]'); | ||
expect(result).toMatchSnapshot(); | ||
}); | ||
|
||
it('should correctly generate the `casual` data for a non-string scalar mapping', async () => { | ||
const result = await plugin(testSchema, [], { scalars: { AnyObject: 'rgb_array' } }); | ||
|
||
expect(result).toBeDefined(); | ||
expect(result).toContain(JSON.stringify([41, 98, 185])); | ||
expect(result).toMatchSnapshot(); | ||
}); |