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

feat: add scalars config option for custom GraphQL [scalar -> casual] mappings #21

Merged
merged 6 commits into from
Jul 14, 2020

Conversation

3nvi
Copy link
Contributor

@3nvi 3nvi commented Jul 13, 2020

Background

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

Caveats

To make configuration simple, a few "shortcuts" were taken. Namely:

  • the mapping only accepts a casual key (function or string) as a valid autogenerated value in order to align with the rest of the library
  • if a function is passed, then it will be called with its default arguments, since providing custom parameters for a function through a YAML file, would complicate things more than normal

@3nvi
Copy link
Contributor Author

3nvi commented Jul 13, 2020

There is a minorr issue with stringification that tests didn't catch. Will update the PR.

Update: Done

Copy link
Owner

@ardeois ardeois left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @3nvi for your contribution, great idea !
I have few comments before I approve

src/index.ts Show resolved Hide resolved

const emailRegex = /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;
expect(result).toBeDefined();
expect(emailRegex.test(result as string)).toBeTruthy();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our test suite force casual to return a predictable result, so I think you can check the result string directly instead of using a regex

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh cool, didn't dig in too much and with a quick pass, I wasn't able to find something. I'll make sure to update

tests/typescript-mock-data.spec.ts Show resolved Hide resolved
src/index.ts Show resolved Hide resolved
@ardeois ardeois added the minor Increment the minor version when merged label Jul 14, 2020
@ardeois ardeois merged commit 3c442db into ardeois:master Jul 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
minor Increment the minor version when merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Custom Scalar Definitions
2 participants