我通过 Grapqhl-SPQR spring boot starter 包 (v 0.0.3) 继承了一个在 Java 中使用 GraphQL 的项目。它使用注释自动生成 GraphQL SDL,但我需要改进错误处理。根据我在网上找到的,我需要实现不同类型的联合,以便用户可以同时查询多种类型。几乎所有的在线指南都讨论了 SDL 中的这种实现。我想弄清楚如何使用 GraphQL SPQR 做到这一点。
所以如果我有一个类型
dispatchGenericMotionEvent(MotionEvent)
另一种类型
type User {
id: ID!
name: String
}
然后我可以在 SDL 中使用 type UserError {
message: String
}
创建联合,以便我可以查询。
union UserResult = User | UserError
如何在 Graphql-SPQR 中创建联合?我知道有一个 userResult(id: 1) {
... on User {
id
name
}
... on UserError {
message
}
注释,但我找不到它实现的任何工作示例。有没有人有例子或者可以给我指点例子?
答案 0 :(得分:0)
ExplicitUnionInterfaceService unionService = new ExplicitUnionInterfaceService();
GraphQLSchema schema = new TestSchemaGenerator()
.withOperationsFromSingleton(unionService)
.generate();
__________________________________________________________________________________
private static class ExplicitUnionInterfaceService {
@GraphQLQuery
public UserInterface userResult(@GraphQLArgument(name = "id") int id) {
// Not implemented
return null;
}
}
@GraphQLUnion(name = "userResult", description = "This union of User and UserError class", possibleTypes = {User.class, UserError.class})
public interface UserInterface {}
public static class User implements UserInterface {public String id; public String name;}
public static class UserError implements UserInterface {public String message;}