到目前为止,我了解到我需要构建自己的baseQuery。我可以像这个例子中写graphql查询和变异一样:https://rtk-query-docs.netlify.app/examples/react-with-graphql,如果我向query.builder添加类型如
以下是来自2的钩子示例:
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 };
};
代码不是很有意义,但我希望你能理解我的意思。谢谢 :)