gpt4 book ai didi

java - GraphQL + Java - 如何过滤子字段?

转载 作者:行者123 更新时间:2023-11-30 08:32:11 27 4
gpt4 key购买 nike

我正在使用 graphql-javagraphql-java-annotations在我的 Spring 应用程序中(spring-graphql-common 似乎非常有限,看起来它不再维护了),我正在努力处理子字段操作。

我有两个类 AB 我想使用 GraphQL 访问它们,它们是:

class A {
@GraphQLField
Integer id;
@GraphQLField
String name;
@GraphQLField
List<B> listOfB;
}

class B {
@GraphQLField
Integer id;
@GraphQLField
String code;
}

我可以使用以下模式成功查询 A 中的所有字段以及 B 中的字段:

@Component
public class Schema
{
public GraphQLObjectType aType;
public GraphQLSchema aSchema;
public GraphQLObjectType queryType;

@Autowired
public Schema(DataFetcher dataFetcher) throws IllegalAccessException, InstantiationException, NoSuchMethodException
{
aType = GraphQLAnnotations.object(A.class);

queryType =
GraphQLObjectType
.newObject()
.name("QueryType")
.field(GraphQLFieldDefinition.newFieldDefinition()
.name("a")
.type(aType)
.argument(GraphQLArgument.newArgument()
.name("id")
.type(new GraphQLNonNull(Scalars.GraphQLInt))
.build())
.dataFetcher(dataFetcher)
.build())
.build();

aSchema = GraphQLSchema.newSchema()
.query(queryType)
.build();
}
}

我可以使用以下请求成功过滤 a:

{
a(id:5)
{
id,
name,
listOfB
{
code
}
}
}

但是当我尝试过滤 listOfB 时,例如使用 take 仅选择 X 记录时,我遇到错误 UnknownArgument 类型的验证错误:未知参数取:

{
a(id:5)
{
id,
name,
listOfB(take:3)
{
code
}
}
}

我明白这个错误是什么意思,因为我还没有为 A 上的 listOfB 字段声明任何可能的参数,但我不知道我怎么可能使用 graphql-java-annotations 来完成。

我已经为 listOfB 添加了一个特定的 DataFetcher,在类 A 中使用以下代码:

@GraphQLDataFetcher(BDataFetcher.class)
private List<B> listOfB;

当我检索 listOfB 时它确实被调用了。

您是否知道我如何在使用 graphql-java-annotations 时向字段添加参数?如果没有,是否有任何解决方法?

在没有注释的情况下定义模式并使用 graphql-java 的“经典”方式不是一种选择,因为我的模式真的很大 :/

谢谢:)

最佳答案

这不是我最终使用的解决方案,因为它不符合我的设计,但您可以执行以下操作:

public class MyClass
{
@GraphQLField
private Integer id;
private List<String> items;

@GraphQLField
public List<String> items(DataFetchingEnvironment environment, @GraphQLName("take") Integer take, @GraphQLName("after") Integer after)
{
List<String> items = ((MyClass) environment.getSource()).getItems();

Integer fromIndex = 0;
Integer toIndex = items.size();
if (after != null)
fromIndex = after + 1;
if (take != null)
toIndex = fromIndex + take;

return items.subList(fromIndex, toIndex);
}
}

这很糟糕,因为您没有使用 DataFetcher,因此您的代码将被复制/粘贴到任何地方,但它仍然有效。

但我找到了一种使用 DataFetcher 的方法,据说这很奇怪,我猜这不是对库的正确使用,但谁知道呢:

public class MyClass
{
@GraphQLField
private Integer id;
private List<String> items;

@GraphQLField
@GraphQLDataFetcher(MyClassDataFetcher.class)
public List<String> items(DataFetchingEnvironment environment, @GraphQLName("take") Integer take, @GraphQLName("after") Integer after)
{
return null;
}

}

这将使参数 take 并且 afterMyClassDataFetcher.get() 方法中的 DataFetchingEnvironment ,所以你有一个没有被调用的空方法,但参数仍然传递给你的 DataFetcher

同样,这并不理想,因为我怀疑它的使用方式,但这是使用 DataFetcher 和我所知道的参数的唯一方法。

现在,我没有使用这两个解决方案中的任何一个,我最终完全放弃了 graphql-annotations 并开发了我自己的带有自定义注释的解决方案,这些注释允许我自动构建模式(赢得抱歉,请公开发布)。

我不推荐使用 graphql-annotations 直到他们有所有用例的适当文档。

关于java - GraphQL + Java - 如何过滤子字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40256331/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com