[英]How to customize the type for a graphql-code-generator produced field when consuming a Hasura graphql schema
我想覆蓋由 Hasura 生成的 graphql 模式中特定字段的 jsonb 類型並通過 graphql-code-generator 運行。
我有一個customList
類型的customList
字段。 Ths 用於包含一組 json 對象。 將 graphql-code-generator 與 TypeScript 插件一起使用時,生成的類型解析為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!]
}
謝謝你的幫助!
您可以將代碼生成器指向一個新文件 - 比如說my-schema.js
,然后按照您希望的方式操作架構。 您可以使用您喜歡的任何工具(graphql-toolkit / graphql-compose / 直接 GraphQLSchema 操作)
Typescript 插件有一個scalars
配置選項,您可以在其中為任何標量定義自定義類型。
首先,您必須定義自定義客戶端架構。 擴展MyEntity
類型以具有特殊的標量而不是 Jsonb
客戶端schema.graphql
scalar CardList
extend type MyEntity {
ccards: CardList!
}
然后創建一個包含此標量類型的文件:
標量.ts
type ListItem {
itemId: string!
}
export type CardList = ListItem[]
然后將新模式和自定義類型添加到.yml
生成器的.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.