[英]Graphql typescript typing and codegen
The library called apollo-codegen allows one to create types on the client for a graphQL schema. 称为apollo-codegen的库允许人们在客户端上为graphQL模式创建类型。 However it expect the
gql
queries to be put in a .graphql
file. 但是,它希望将
gql
查询放入.graphql
文件中。
My question is: how can I use the query when they are in a .graphql
file ? 我的问题是:当它们位于
.graphql
文件中时,如何使用查询?
I was previously doing this in my .ts
files 我以前在
.ts
文件中这样做
const ssQuery = gql`
subscription suppliers {
suppliers {
id,
name
}
}
`;
Assuming you're using webpack, graphql-tag includes a loader to import the queries. 假设您使用的是webpack, graphql-tag包含一个加载程序以导入查询。
// webpack.config.js
loaders: [
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: 'graphql-tag/loader'
}
]
// in your project
import MY_QUERY from './my-query.graphql'
graphql(MY_QUERY)(MyComponent)
Be mindful that doing this means you should have only one query/mutation/subscription per file. 请注意,这样做意味着每个文件只能有一个查询/变异/订阅。 However, you can also create files for fragments and import them inside the graphql file (not sure if apollo-codegen supports this syntax though).
但是,您也可以为片段创建文件并将其导入graphql文件中(尽管不确定apollo-codegen是否支持此语法)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.