我正在使用 graphql codegen 钩子(Hook),其结果类型为:ApolloReactCommon.QueryResult
,这意味着它是一个具有 data
的对象, loading
和 error
字段。
一般情况下,我会使用 data
在其他组件中,我需要定义它的类型。我目前需要写这样的东西来获得 data
类型:
type MyPost = MyPostQueryHookResult['data']
我本来希望得到这个
MyPost
为我创建的类型。有没有办法让它自动生成?我的配置:
generates:
src/generated/graphql.tsx:
plugins:
- "typescript"
- "typescript-operations"
- "typescript-react-apollo"
config:
withHooks: true
最佳答案
最好的方法是在构建查询时使用片段。将为每个片段生成类型,然后可以直接使用它们。
来源:from the main contributor of graphql-codegen
关于typescript - graphql codegen 可以自动为 hooks 的结果数据字段生成类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58733198/