我想重写Hasura生成的graphql模式中的特定字段的jsonb类型,并通过graphql代码生成器运行。
我有一个类型为jsonb的customList字段。它用于包含一个json对象数组。当使用带有TypeScript插件的graphql代码生成器时,生成的类型解析为any。我试图弄清楚如何仅针对该特定字段使用自定义类型来覆盖该类型。
下面的代码片段显示了graphql模式的相关部分,以及目标的graphql类型重写。到目前为止,我尝试过的每一件事都会导致码元错误。
GraphQl模式
//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
},
}
}目标覆盖类型
//clientTypes.graphql
type ListItem {
itemId: string!
}
extend type MyEntity {
ccards: [ListItem!]
}谢谢你的帮助!
发布于 2021-08-18 13:45:06
有一个用于类型记录插件的scalars配置选项,您可以在其中为任何标量定义自定义类型。
首先,您必须定义一个自定义客户端模式。扩展MyEntity类型,使其具有一个特殊的标量,而不是Jsonb
client-schema.graphql
scalar CardList
extend type MyEntity {
ccards: CardList!
}然后创建一个包含此标量类型的文件:
scalars.ts
type ListItem {
itemId: string!
}
export type CardList = ListItem[]然后将新模式和自定义类型添加到graphql的.yml配置中,如下所示:
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注意:路径应该相对于生成的文件。
https://github.com/dotansimha/graphql-code-generator/issues/153#issuecomment-776735610
发布于 2020-03-22 21:03:14
您可以将代码元指向一个新文件,比如my-schema.js,然后按您希望的方式操作模式。您可以使用您喜欢的任何工具(graphql-toolkit /graphql-组合/直接GraphQLSchema操作)
https://stackoverflow.com/questions/59107222
复制相似问题