简体   繁体   English

Apollo客户端:apollo-link-rest是否可以解决端点之间的关系?

[英]Apollo Client: can apollo-link-rest resolve relations between endpoints?

The rest api that I have to use provides data over multiple endpoints. 我必须使用的其余API在多个端点上提供数据。 The objects in the results might have relations that are are not resolved directly by the api, it rather provides ids that point to the actual resource. 结果中的对象可能具有无法由api直接解决的关系,而是提供了指向实际资源的ID。

Example: For simplicity's sake let's say a Person can own multiple Books . 示例:为了简单起见,假设一个Person可以拥有多本Books

Now the api/person/{i} endpoint returns this: 现在, api/person/{i}端点返回以下内容:

{ id: 1, name: "Phil", books: [1, 5, 17, 31] }

The api/book/{i} endpoint returns this (note that author might be a relation again): api/book/{i}端点返回此值(请注意,作者可能又是一个关系):

{ id: 5, title: "SPRINT", author: 123 }

Is there any way I can teach the apollo client to resolve those endpoints in a way that I can write the following (or a similar) query: 有什么方法可以教阿波罗客户端解决这些端点的方式,即可以编写以下(或类似的)查询:

query fetchBooksOfUser($id: ID) {
  person (id: $id) {
    name,
    books {
      title
    }
  }
}

I didn't try it (yet) in one query but sould be possible. 我还没有在一个查询中尝试过它,但是有可能实现。

Read docs strating from this 阅读来自此的文档

At the beggining I would try with sth like: 在开始的时候,我会尝试:

query fetchBooksOfUser($id: ID) {
  person (id: $id) @rest(type: "Person", path: "api/person/{args.id}") {
    name,
    books @rest(type: "Book", path: "api/book/{data.person.books.id}") {
      id,
      title
    }
  }
}

... but it probably won't work - probably it's not smart enough to work with arrays. ...但是它可能无法正常工作-可能不够聪明,无法使用数组。


UPDATE: See note for similiar example but using one, common parent-resolved param. 更新:请参见注释以获取类似示例,但使用一个常见的父级解析参数。 In your case we have partially resolved books as arrays of objects with id . 在您的情况下,我们已将books部分解析为id为的对象数组。 I don't know how to use these ids to resolve missing fields () on the same 'tree' level. 我不知道如何使用这些ids来解析同一“树”级别上的缺失字段()。


Other possibility - make related subrequests/subqueries (someway) in Person type patcher. 其他可能性-在“ Person类型修补程序中进行相关的子请求/子查询(有时)。 Should be possible. 应该是可能的。

Is this really needed to be one query? 这真的是一个查询吗? You can provide ids to child containers, each of them runing own query when needed. 您可以为子容器提供ID,每个子容器在需要时运行自己的查询。


UPDATE: 更新: Apollo will take care on batching 阿波罗(Apollo)将小心批次 (Not for REST, not for all graphql servers - read docs ). (不适用于REST,不适用于所有graphql服务器-请阅读docs )。

'it's handy' to construct one query but apollo will cache it normalizing response by types - data will be stored separately. 构造一个查询“很方便”,但是apollo将缓存该查询以按类型归一化响应-数据将单独存储。 Using one query keeps you within overfetching camp or template thinking (collect all possible data before one step rendering). 使用一个查询可以使您overfetching camptemplate thinking (第一步渲染之前收集所有可能的数据)。

Ract thinking keeps your data and view decomposed, used when needed, more specialised etc. Ract thinking使您的数据和视图保持分解,在需要时使用,更专业等。

<Person/> container will query for data needed to render itself and list of child-needed ids. <Person/>容器将查询呈现自身所需的数据以及需要的儿童ID列表。 Each <Book/> will query for own data using passed id . 每个<Book/>都会使用传递的id查询自己的数据。

As an alternative, you could set up your own GraphQL back-end as an intermediary between your front-end and the REST API you're planning to use. 作为替代方案,您可以设置自己的GraphQL后端,作为您的前端和计划使用的REST API之间的中介。

It's fairly easy to implement REST APIs as data sources in GraphQL using Apollo Server and a package such as apollo-datasource-rest which is maintained by the authors behind Apollo Server. 使用Apollo Server和由Apollo Server背后的作者维护的apollo-datasource-rest类的包,将REST API作为GraphQL中的数据源实现是相当容易的。

It would also allow you to scale if you ever have to use other data sources (DBs, 3rd party APIs, etc.) and would give you full control about exactly what data your queries return. 如果需要使用其他数据源(DB,第三方API等),它还可以扩展您的规模,并使您可以完全控制查询返回的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM