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

[Enhancement] Provide a map of scalars and their TypeScript types. #419

Open
deftomat opened this issue Jul 17, 2020 · 9 comments
Open

[Enhancement] Provide a map of scalars and their TypeScript types. #419

deftomat opened this issue Jul 17, 2020 · 9 comments

Comments

@deftomat
Copy link

deftomat commented Jul 17, 2020

We found this package and it has all the scalars which we need 🥳 . Also, we are using graphql-code-generator to generate types for our resolvers.

The problem is, that this generator don't know nothing about these scalars. So, every scalar is typed as any. To fix this, one can provide a hint to generator like scalar Date will actually be an instance of Date or scalar PositiveInt is number.

So, I was wondering if it is possible to export a map of scalar names and their actual TypeScript types as strings. See scalars section in https://graphql-code-generator.com/docs/plugins/typescript-resolvers.

This would allows us to pass this information into generator and keep in up to date with scalars provided by this package.

EXAMPLE:

export const types = {
  'Date': 'Date',
  'PositiveInt': 'number',
  'EmailAddress': 'string',
  'JSONObject': 'Record<string, any>'
}
@Urigo
Copy link
Owner

Urigo commented Aug 3, 2020

@deftomat this is a really cool idea, would you be interested in trying out to create a PR for it?
We would gladly support you!

@deftomat
Copy link
Author

deftomat commented Aug 5, 2020

Sure, I will!

@vespertilian
Copy link
Contributor

I would be very keen to have this :)

@andreialecu
Copy link

@deftomat I also would like this and could work on a PR for it, but I wonder why your example is JavaScript.

As far as I can tell, graphql-code-generator's configuration is YAML. Do you have an example of how this might be bridged between JS and YAML?

Because otherwise a simple list in the README that can be copy pasted into the codegen config would probably do.

@andreialecu
Copy link

@ardatan since you're a graphql-code-generator contributor as well.

How would this best be implemented?

@deftomat
Copy link
Author

Its in JS because we are using it in our custom Webpack plugin. You can trigger the generator through JS API.

@andreialecu
Copy link

While digging through the graphql-code-generator source I found that scalars can actually point to a source file where they can be defined in code:

dotansimha/graphql-code-generator#2418

Test here shows how to use it:
dotansimha/graphql-code-generator#2600

So it seems this should work with something like scalars: my-scalars.ts

This functionality is undocumented from what I can see, besides looking at these two PRs.

my-scalars.ts would look similar to:

export * from 'graphql-scalars/type-map';

export const OtherScalar: 'string';

graphql-scalars/type-map would need to contain something like:

export const Date: 'Date';
export const PositiveInt: 'number';
export const EmailAddress: 'string';
export const JSONObject: 'Record<string, any>';
...

@Urigo
Copy link
Owner

Urigo commented Mar 20, 2021

@andreialecu would you be willing to PR the docs on GraphQL Codegen's website and also maybe add a section to the README here with examples of people who wants to use both packages together?

cc @tvvignesh as he will soon create a docs website for GraphQL Scalars as well, based on the current readme

@tobiasdiez
Copy link

It would be nice if the corresponding typescript type is also added in the documentation in the subpage for each scalar type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants