【发布时间】:2021-08-19 22:05:12
【问题描述】:
到目前为止,我知道我需要建立自己的baseQuery。我可以像https://rtk-query-docs.netlify.app/examples/react-with-graphql 中的示例那样编写graphql 查询和突变,如果我将类型添加到query.builder 像这样builder.query<Device, void> 或者我必须使用像这样https://www.graphql-code-generator.com/docs/plugins/typescript-graphql-request#simple-request-middleware 这样的东西,我是否可以获得查询和突变的完整类型安全性。在后一种情况下,如果我为 graphql-request 库使用生成的钩子,我的baseQuery 应该如何看待。
这是来自2的钩子示例:
import { GraphQLClient } from 'graphql-request';
import { getSdk } from './sdk'; // THIS FILE IS THE GENERATED FILE
async function main() {
const client = new GraphQLClient('https://countries.trevorblades.com/');
const sdk = getSdk(client);
const { continents } = await sdk.continents(); // This is fully typed, based on the query
console.log(`GraphQL data:`, continents);
}
我在想这样的事情:
import {getSdk} from './sdk'
const client = new GraphQLClient('https://countries.trevorblades.com/');
const graphqlBaseQuery = (someGeneratedQueryOrMutation, client) => {
const something = someGeneratedQueryOrMutation(client);
const { continents } = await something.continents();
return { data: continents };
};
代码并没有真正的意义,但我希望你能明白我的意思。谢谢:)
【问题讨论】: