I would like to override a the jsonb type for a speficic field in a graphql schema produced by Hasura and run through graphql-code-generator.
I have a customList
field of type jsonb. Ths is used to contain an array of json objects. When using graphql-code-generator with the TypeScript plugin, the generated type resolves to any
. I am trying to figure out how to override this with a custom Type for that specific field only.
The below snippets show the relevant section of graphql schema, and the targetted graphql type overrides. So far, everything I have tried results in codegen errors
GraphQl Schema
//schema.json
...
{
"kind": "OBJECT",
"name": “MyEntity”,
"description": "columns and relationships of MyEntity",
"fields": [
...
{
"name": "customList",
"description": "",
"args": [
{
"name": "path",
"description": "JSON select path",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null
}
],
"type": {
"kind": "SCALAR",
"name": "jsonb",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
}
}
Targetted Override Types
//clientTypes.graphql
type ListItem {
itemId: string!
}
extend type MyEntity {
ccards: [ListItem!]
}
Thanks for any help!
You can point the codegen to a new file - let's say my-schema.js
, and then manipulate the schema the way you wish. You can use any tool you prefer for that (graphql-toolkit / graphql-compose / direct GraphQLSchema manipulation)
There is a scalars
config option for the Typescript plugin where you can define a custom type for any scalar.
First you have to define a custom client side schema. Extend the MyEntity
type to have a special scalar instead of Jsonb
client-schema.graphql
scalar CardList
extend type MyEntity {
ccards: CardList!
}
Then create a file that will contain type for this scalar:
scalars.ts
type ListItem {
itemId: string!
}
export type CardList = ListItem[]
Then add the new schema and a custom type to the .yml
config of the graphql codegen as follows:
schema:
- https://your-remote-schema.url/v1/graphql:
documents: "src/**/*.ts"
generates:
src/graphql/schema.ts:
schema: src/graphql/client-schema.graphql
plugins:
- typescript
- typescript-operations
config:
scalars:
CardList: ./scalars#CardList
Note: the path should be relative to the generated file
https://github.com/dotansimha/graphql-code-generator/issues/153#issuecomment-776735610
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.