我有以下查询:
const getPage = gql`
query Page($path: String!) {
page(path: $path) @rest(type: "Page", path: "{args.path}") {
blocks @type(name: Block) {
name
posts @type(name: Post) {
body
author
}
}
authors @type(name: Author) {
name
}
}
}
Run Code Online (Sandbox Code Playgroud)
在blocks.posts.author只有一个AuthorId。作者对象包含所有可用的作者。
我想AuthorId用它的对应对象替换/匹配它。是否可以在一个查询中执行此操作?
我也不会介意仅对Author进行单独的查询(提取将被缓存,不会发出新请求),但是我仍然不知道如何通过2个查询来匹配它。
示例API响应
{
blocks: [
{
posts: [
{
id: 1,
title: 'My post',
author: 12,
}
]
}
],
authors: [
{
id: 12,
name: 'John Doe'
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想要的1个查询author在a内post …
现在我尝试在打字稿中将 apolloClient 与 graphql 和 rest api 一起使用。
因此,我应用了apollo-link-rest。但我收到以下错误。
./node_modules/apollo-link-rest/bundle.umd.js 找不到模块:无法解析“D:\forked\syntegrate_app_client\node_modules\apollo-link-rest”中的“graphql-anywhere/lib/async”
Type 'RestLink' is not assignable to type 'ApolloLink'.
Types of property 'split' are incompatible.
Type '(test: (op: import("d:/forked/syntegrate_app_client/node_modules/@apollo/client/link/core/types").Operation) => boolean, left: import("d:/forked/syntegrate_app_client/node_modules/@apollo/client/link/core/ApolloLink").ApolloLink | import("d:/forked/syntegrate_app_client/node_modules/@apollo/client/link/core/types")....' is not assignable to type '(test: (op: import("d:/forked/syntegrate_app_client/node_modules/apollo-link/lib/types").Operation) => boolean, left: import("d:/forked/syntegrate_app_client/node_modules/apollo-link/lib/link").ApolloLink | import("d:/forked/syntegrate_app_client/node_modules/apollo-link/lib/types").RequestHandler, right?: import("d...'.
Types of parameters 'test' and 'test' are incompatible.
Types of parameters 'op' and 'op' are incompatible.
Property 'toKey' is missing in type 'import("d:/forked/syntegrate_app_client/node_modules/@apollo/client/link/core/types").Operation' but required in type 'import("d:/forked/syntegrate_app_client/node_modules/apollo-link/lib/types").Operation'.ts(2322)
types.d.ts(24, …Run Code Online (Sandbox Code Playgroud) 我必须使用的其余 api 提供多个端点上的数据。结果中的对象可能具有 api 无法直接解析的关系,而是提供指向实际资源的 id。
示例:
为了简单起见,假设一个Person可以拥有多个Books.
现在api/person/{i}端点返回:
{ id: 1, name: "Phil", books: [1, 5, 17, 31] }
Run Code Online (Sandbox Code Playgroud)
端点api/book/{i}返回此(请注意,作者可能又是一个关系):
{ id: 5, title: "SPRINT", author: 123 }
Run Code Online (Sandbox Code Playgroud)
有什么方法可以教 apollo 客户端以我可以编写以下(或类似)查询的方式解析这些端点:
query fetchBooksOfUser($id: ID) {
person (id: $id) {
name,
books {
title
}
}
}
Run Code Online (Sandbox Code Playgroud)