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

Using Type Definitions with GraphQL Code Generator #953

Closed
tubbo opened this issue Jul 8, 2021 · 2 comments
Closed

Using Type Definitions with GraphQL Code Generator #953

tubbo opened this issue Jul 8, 2021 · 2 comments

Comments

@tubbo
Copy link

tubbo commented Jul 8, 2021

Is your feature request related to a problem? Please describe.

In order to use this library alongside GraphQL Code Generator (with GraphQL Config), we had to write a script that extracted the type definitions and converted them into an executable schema:

const { typeDefs } = require('graphql-scalars')
const { buildSchema, print } = require('graphql')

const scalars = typeDefs.join('\n')

module.exports = buildSchema(scalars)

This works, but it took a while to figure out that this is what I needed to do. It might be difficult for newer folks to use this library if it doesn't play nice with their tooling.

Describe the solution you'd like

The script I mentioned above (rewritten into TS of course) could be included into this library as an alternative entry point like graphql-scalars/schema, which could be imported in a codegen/graphql config like so:

schema:
  - graphql-scalars/schema
  - api/src/**/*.gql
documents: web/src/**/*.gql

Describe alternatives you've considered

As I said before, the script that just adds all the scalar types together is working for us right now, but we'd like to use the type definitions from this library in other applications which won't have access to that script, so it's not very scalable. Which is weird, since this library is all about scalars. ;)

Additional context

This library has really won us over, there are a lot of types here that we wanted in our schema but didn't have the time budget to implement properly. So the fact that y'all have done it in this project is fantastic, and I wanted to say thanks so much for building this! We've already seen the benefits of a richly typed schema and haven't even taken it to production yet.

@claraberendsen
Copy link

I encountered the same issue here, and was able to resolve it similarly. However I also have a problem with mapping the correct TS type in the graphql code generator config. Now all the auto generated types on these scalars are type any in TS, and I think we are missing type-safety by doing that.

@ardatan
Copy link
Collaborator

ardatan commented Aug 16, 2021

To import scalars definitions explicitly. You can just do manually;

const { typeDefs } = require('graphql-scalars')
const { parse } = require('graphql')

export default parse(typeDefs.join('\n'));

Other than that, you should configure types for scalars in GraphQL Code Generator. This seems out of scope for this library.
See dotansimha/graphql-code-generator#1532

@ardatan ardatan closed this as completed Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants