使用 Hasura graphql 模式时如何自定义 graphql-code-generator 生成的字段的类型

arh*_*nee 6 typescript graphql hasura graphql-codegen

我想覆盖 Hasura 生成的 graphql 模式中特定字段的 jsonb 类型,并通过 graphql-code-generator 运行。

\n\n

我有一个customListjsonb 类型的字段。Ths 用于包含 json 对象数组。当将 graphql-code-generator 与 TypeScript 插件一起使用时,生成的类型解析为any. 我试图找出如何仅使用该特定字段的自定义类型来覆盖它。

\n\n

下面的代码片段显示了 graphql 模式的相关部分,以及目标 graphql 类型覆盖。到目前为止,我尝试过的所有操作都会导致代码生成错误

\n\n

图Ql模式

\n\n
  //schema.json  \n  ...\n  {\n    "kind": "OBJECT",\n    "name": \xe2\x80\x9cMyEntity\xe2\x80\x9d,\n    "description": "columns and relationships of MyEntity",\n    "fields": [\n        ...\n        {\n        "name": "customList",\n        "description": "",\n        "args": [\n            {\n            "name": "path",\n            "description": "JSON select path",\n            "type": {\n                "kind": "SCALAR",\n                "name": "String",\n                "ofType": null\n            },\n            "defaultValue": null\n            }\n        ],\n        "type": {\n            "kind": "SCALAR",\n            "name": "jsonb",\n            "ofType": null\n        },\n        "isDeprecated": false,\n        "deprecationReason": null\n        },\n     }\n  }\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

目标覆盖类型

\n\n
//clientTypes.graphql\n\ntype ListItem {\n  itemId: string!\n}\n\nextend type MyEntity {\n  ccards: [ListItem!]\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

谢谢你的帮助!

\n

Dot*_*mha 0

您可以将代码生成器指向一个新文件 - 比方说my-schema.js,然后按照您希望的方式操作架构。您可以使用您喜欢的任何工具(graphql-toolkit / graphql-compose / 直接 GraphQLSchema 操作)