Json到GraphQLArgumetn对象转换在graphql-spqr中失败。
尝试向上述抽象类添加GraphQLInterface(具有自动发现true和scanpackage) 和GraphQLtype键入所有具体类。
我的图形查询:
query contactsQuery($searchQuery : QueryInput) { contacts(searchQuery:$searchQuery){id}}
variables:{"searchQuery":{"bool":{"conditions":[{"must":{"matches":[{"singleFieldMatch":{"boost":null,"field":"firstname","value":"siddiq"}}],"bool":null}}]}})
Java代码:
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME,include=JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonSubTypes({@type(value = Must.class, name="must"),@type(value = MustNot.class, name="mustNot")})
public abstract class Condition
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME,include=JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonSubTypes({@type(value = SingleFieldMatch.class, name="singleFieldMatch"),@type(value = MultiFieldMatch.class, name="multiFieldMatch")})
public abstract class Match
@GraphQLQuery(name = "contacts")
public List getContacts(@GraphQLArgument(name ="searchQuery") Query query)
仍然会引发错误,未知字段错误等。不确定是否缺少哪个配置。 使用AnnotatedResolvedBuilder构建GraphQLSchema,配置基本包配置JacksonValueMappperFactory和单例服务。
答案 0 :(得分:0)
嗨,这可能与我最终遇到的问题类似。
最初我有以下
@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "type")
@GraphQLInterface(name = "AbstractClass", implementationAutoDiscovery = true)
public abstract class AbstractClass{
,以下查询称为
addNewObject(object: {name: "soft2", id: "asdas"})
要使转换功能正常运行,我需要做的是以下更改
@JsonTypeInfo(use = Id.NAME, include = As.EXISTING_PROPERTY, property = "type")
@GraphQLInterface(name = "AbstractClass", implementationAutoDiscovery = true)
public abstract class AbstractClass{
private String type = this.getClass().getSimpleName();
/**
* @return the type
*/
@GraphQLQuery(name = "type", description = "The concrete type of the node. This should match the initialised class. E.g. \"Concrete\", \"DecafCoffee\"")
public String getType() {
return type;
}
查询现在为
addNewConcreteObject(concrete: {name: "soft2", id: "asdas", type: "Concrete"})
为什么这样做有效(我认为):
使用Jackson转换器(ObjectMapper)从JSON转换为我代码中的对象时。之前我已经注意到JSON需要了解要转换为哪个类的知识。因此,@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "type")
的最初使用是在将其写入字符串时在JSON中放置一个type
属性。
SPJSON可能会选择@JSON标记,然后似乎使用Jackson转换器尝试将查询转换为所需对象。
如果我是对的,这就是问题所在。
由于查询不包含type
,因此无法正确转换查询。此外,由于type
属性不是对象的成员变量,而是仅由ObjectMapper添加的,因此SPQR不会选择它,因此它不是对象架构的一部分。因此,为了解决这个问题,我添加了type
作为始终等于实际类的成员变量,然后更改了JsonTypeInfo
以查找现有属性。
我很高兴这不是您问题的直接答案(而且绝对不是一个很漂亮的答案),但希望它能帮助您找到解决方案。