简体   繁体   English

阿波罗联盟实体参考工作不正常

[英]Apollo Federation Entity Reference Not Working Properly

I've been wrestling with Apollo entities and their references for awhile now.一段时间以来,我一直在与 Apollo 实体及其参考文献搏斗。 I've followed this article to implement a relationship between articles (articles service) and their author (person service).我已经按照这篇文章来实现文章(文章服务)和他们的作者(个人服务)之间的关系。 The relevant code is as follows:相关代码如下:

// Article service type definitions
export const typeDefs = gql`
  type Article @key(fields: "_id") @key(fields: "slug") {
    _id: ID!
    author: Person!
    category: String
    content: String
    media: String
    published: Boolean
    slug: String!
    subtitle: String
    title: String!
  }

...
`;
// Article service resolvers
...
Article: {
  author: <IArticleAuthor> ((article) => {
    return { __typename: 'Person', _id: article.author };
  }),
},
...
// Person service resolvers
...
__resolveReference: (reference: { _id: string }) => {
  return Person.findOne({ _id: { $eq: reference._id }});
},
...

The problem that occurs is that I only get the object that is returned in the article service resolvers page { __typename: 'Person', _id: article.author };出现的问题是我只得到了文章服务解析器页面{ __typename: 'Person', _id: article.author }; when I actually make a request.当我真正提出请求时。 I was expecting that the __resolverReference function would be called once I made a request to fetch articles but it does not.我期待一旦我提出获取文章的请求,就会调用__resolverReference function 但它没有。 Is there something that I'm missing to get this to work properly?我是否缺少一些东西才能使其正常工作?

It ended up being a problem in the Person service.它最终成为 Person 服务中的一个问题。 I had the __resolveReference method attached to the resolver object at the top-level instead of within a Person property.我将__resolveReference方法附加到解析器 object 的顶层而不是Person属性中。

export const resolvers = {
  __resolveReference: (reference) => Person.findOne({ _id: { $eq: reference.id } });
}

versus相对

export const resolvers = {
  Person: {
    __resolveReference: (reference) => Person.findOne({ _id: { $eq: reference.id } });
  }
}

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

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