Graphql-code-generator: Provide a simple way to override schema type for a given field, prior to codegen step

Created on 29 Nov 2019  路  4Comments  路  Source: dotansimha/graphql-code-generator

Is your feature request related to a problem? Please describe.
My schema has a jsonb scalar fileld type. However, the TypeScript codgen plugin simply resolves this to an any TypeScript type. I am looking for a way to provide a custom override type for that field, so that I can get access the JSon contents of that field in a stongly typed way.

Describe the solution you'd like
I would like to simple mechanism to provide a custom type for a given field, so that the code-generation uses the custom priovided type instead of the original schema provided type

Describe alternatives you've considered

  • I have defined an additional client schema with the desired field type, but (logically) get codegen errors saying that the field is already defined

    • I have looked for ways of easily altering the schema prior to the code-gen step, but am struggling to figure it out.

Additional context
I am using an Hasura API, with typescript and typescript-react-apollo plugins

core enhancement

Most helpful comment

@dotansimha - thanks for your response, but that doesn't seem to work in my case. I get the following error:

  Unable to merge GraphQL type "MyEntity": Field "myjsonbfield" already defined with a different type. 
  Declared as "MyCustomFieldType", but you tried to override with "jsonb"

Some Futher Details:

  • Im using the Hasura GraphQL engine, which always results in a "jsonb" scalar for all jsonb field types
  • I have a client .graphql where I tried to redeclare the field type

Sidenotes

All 4 comments

@ahrnee just overriding the field doesn't work?
redeclare it in a .graphql file and add it to your schema:, i think it should override the field type.

@dotansimha - thanks for your response, but that doesn't seem to work in my case. I get the following error:

  Unable to merge GraphQL type "MyEntity": Field "myjsonbfield" already defined with a different type. 
  Declared as "MyCustomFieldType", but you tried to override with "jsonb"

Some Futher Details:

  • Im using the Hasura GraphQL engine, which always results in a "jsonb" scalar for all jsonb field types
  • I have a client .graphql where I tried to redeclare the field type

Sidenotes

Same problem here.
We generate types from Datocms GraphQL API.
Most of enums are just Scalars['String'].
We really need to override for some fields and we can not change the source API.
We tried to define a local schema but we have the same Error

Unable to merge GraphQL type "TitleRecord": Field "variant" already defined with a different type. Declared as "Variant", but you tried to override with "String"
        Error: Unable to merge GraphQL type "TitleRecord": Field "variant" already defined with a different type. Declared as "Variant", but you tried to override with "String"

It would be very nice to allow merge if needed

I think the best way to do that is to use a custom schema loader, then you can manipulate your schema in any way you wish to. (for example, using graphql-compose to change the schema).

Closing.

Was this page helpful?
0 / 5 - 0 ratings