【问题标题】:Apollo/GraphQL not able to get referenced entities when asking for the IDApollo/GraphQL 在询问 ID 时无法获取引用的实体
【发布时间】:2021-07-06 13:57:48
【问题描述】:

堆栈:

  • VueJS/Vuetify
  • Apollo 客户端
  • Spring Boot、MongoDB 和 GraphQL 服务器

我面临的问题是,当我使用 Apollo 执行我的 GraphQL 查询并且有一组引用实体时,抓取所有字段除了id 工作正常,但尽快正如我添加的那样

id {
   entityId
   organisationId
}

为了还获得id 字段,它返回所有相同实体的集合。

假设有三个用户:John、Jane 和 Jen;具体按这个顺序。当我执行查询时,它将返回 3 次 John 的对象,id 在 GraphQL 查询中时。如果 id 不在 GraphQL 查询中,它将正确返回所有实体。

当我通过 Postman 执行查询时,它可以正常工作。这似乎是 Apollo 特定的问题。

id 实体:

public class TradelyId implements Serializable {
    String entityId;
    String organisationId;
}

一些可能有助于描绘画面的 sn-ps:

@Document
@Getter
@Setter
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@ToString
@RequiredArgsConstructor
@NoArgsConstructor
public class Assignment implements Serializable {

    private static final long serialVersionUID = -165856150537588143L;

    @Id
    @NotNull
    @NonNull
    @EqualsAndHashCode.Include
    private TradelyId id;

    @NotNull
    @NonNull
    private Instant startDateTime;

    @NotNull
    @NonNull
    private Instant endDateTime;

    @NotNull
    @NonNull
    @DBRef
    @ShallowReference
    private Job job;

    @NotNull
    @NonNull
    @DBRef(lazy = true)
    @ShallowReference
    private List<User> users = new ArrayList<>();

}
type Assignment {
    id: TradelyId!
    startDateTime: String!
    endDateTime: String!
    job: Job!
    users: [User!]!
}

input AssignmentRefInput {
    id: TradelyIdQueryInput!
}

input AssignmentCreateInput {
    id: TradelyIdCreateInput!
    startDateTime: String!
    endDateTime: String!
    job: JobRefInput!
    users: [UserRefInput!]!
}

input AssignmentUpdateInput {
    id: TradelyIdQueryInput!
    startDateTime: String!
    endDateTime: String!
    job: JobRefInput!
    users: [UserRefInput!]!
}

extend type Mutation {
    createAssignment(assignment: AssignmentCreateInput!): TradelyId!
    updateAssignment(assignment: AssignmentUpdateInput!): TradelyId!
}

extend type Query {
    assignment(id: TradelyIdQueryInput!): Assignment
    assignments(id: String!): [Assignment!]!
    assignmentForJobId(id: TradelyIdQueryInput!): Assignment
    usersAvailableForAssignment(startDateTime: String!, endDateTime: String!): [User!]!
    assignmentsForUser: [Assignment!]!
}

【问题讨论】:

标签: graphql apollo-client


【解决方案1】:

从缓存中删除typename 似乎可行。

const cache = new InMemoryCache({
  addTypename: false,
});

const apolloClient = new ApolloClient({
  uri: '',
  cache: cache,
});

【讨论】:

猜你喜欢
  • 2019-07-19
  • 2020-08-16
  • 1970-01-01
  • 2018-06-13
  • 2021-09-18
  • 2013-08-09
  • 1970-01-01
  • 2017-04-16
  • 2018-06-12
相关资源
最近更新 更多