到目前为止,我知道我需要建立自己的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。在后一种情况下,baseQuery
如果我为 graphql-request 库使用生成的钩子,我应该怎么看。
这是来自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 };
};
代码并没有真正的意义,但我希望你能明白我的意思。谢谢 :)