在GraphQl Codegen中生成正确的类型而不是联合
我正在尝试迁移一个设置,该设置完全生成所有类型,就像服务器所拥有的类型一样,仅基于我们编写的文档节点。
我在.graphqlrc.js
中具有这种配置
/** @type {import('graphql-config').IGraphQLConfig} */
const graphqlConfig = {
schema: process.env.NEXT_PUBLIC_API_URL,
documents: './src/graphql/**/*.ts',
extensions: {
codegen: {
hooks: {
afterAllFileWrite: ['prettier --write'],
},
generates: {
'./src/__generated__/graphql.ts': {
plugins: [
'typescript',
'typescript-operations',
{
add: {
content: '/* eslint-disable */',
},
},
],
config: {
disableDescriptions: true,
},
},
'./src/__generated__/introspection-result.ts': {
plugins: ['fragment-matcher'],
config: {
useExplicitTyping: true,
},
},
},
},
},
}
,这会生成以下类似的东西
export type QueryName = {
__typename?: 'Query'
resource?:
| { __typename?: 'A' }
| { __typename?: 'B' }
| {
__typename?: 'C'
id: string
prop1: any
prop2: any
}
}
,而不是我期望生成的东西。我期望
export type QueryName = {
__typename?: 'Query'
resource?: {
__typename?: 'C'
id: string
prop1: any
prop2: any
}
}
像c
那样的问题。当前生成的类型将影响很多代码,而如果我能输出要实现的目标,我们只需要更改类型即可。
我已经尝试使用config找到在这里解决方案。请让我知道这是否可能,或者如果有什么东西可以解决这个问题。
提前致谢!
I am trying to migrate a setup which generates all the types exactly like what the server has into something which is based on just the document nodes that we've written.
I currenly have this configuration in .graphqlrc.js
/** @type {import('graphql-config').IGraphQLConfig} */
const graphqlConfig = {
schema: process.env.NEXT_PUBLIC_API_URL,
documents: './src/graphql/**/*.ts',
extensions: {
codegen: {
hooks: {
afterAllFileWrite: ['prettier --write'],
},
generates: {
'./src/__generated__/graphql.ts': {
plugins: [
'typescript',
'typescript-operations',
{
add: {
content: '/* eslint-disable */',
},
},
],
config: {
disableDescriptions: true,
},
},
'./src/__generated__/introspection-result.ts': {
plugins: ['fragment-matcher'],
config: {
useExplicitTyping: true,
},
},
},
},
},
}
and this generates something like below
export type QueryName = {
__typename?: 'Query'
resource?:
| { __typename?: 'A' }
| { __typename?: 'B' }
| {
__typename?: 'C'
id: string
prop1: any
prop2: any
}
}
that is not exactly what I was expecting to be generated. I am expecting something like
export type QueryName = {
__typename?: 'Query'
resource?: {
__typename?: 'C'
id: string
prop1: any
prop2: any
}
}
as I am only querying for C
. The types that is currently getting generated will affect a lot of codes whereas if I could output what I want to achieve, we only need to change the types.
I've tried playing with the config found here but could not find a solution. Please let me know if this is possible or if there's something I could take a look at to solve this.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最终,我最终使用 tiny-invariant 为此。考虑FF代码
I eventually end up using tiny-invariant package for this. Consider the ff code
有一个很好的解决方案!
例如,您有类似的东西:
然后,可以使用 /a>实用程序类型
从这些类型中,您将来可以构造任何类型。
There is a pretty good solution!
For example you have something like this:
Then, you can use the Extract utility type
From these types, you can construct any type in the future.
您也可以做到
以确保其指代联盟内部的类型
You could also do
to ensure that its referring to the type within the union you need