我是GraphQL的新手,我想知道是否有人能帮我弄清楚GraphQL模式中的以下JSON的等价物:
[
{
"id": "1",
"name": "A",
"fldDef": [
{
"name": "f1",
"type": "str",
"opt": false
},
{
"name": "f2",
"type": "bool"
}]
},
{
"id": "2",
"name": "B",
"fldDef": [
{
"name": "f3",
"type": "str",
"opt": true
},
{
"name": "f4",
"type": "str",
"opt": true
}]
}
]到目前为止,我设法将上面的响应映射到下面的对象:
public class FldDef {
private String name, type;
private boolean opt;
// getters & setters
}
public class Product {
private String id, name;
private Map<String, FldDef> fldDef;
// getters & setters
}然后我的模式如下所示,但我遇到的问题是作为Product对象的一部分,我有一个Map,我希望为它获得正确的模式,但我很难获得正确的模式!
type FldDef {
name: String!
type: String!
opt: Boolean!
}
type Product {
id: String!
name: String!
fldDef: FldDef! // now here I don't know what is the syntax for representing MAP, do you know how to achieve this?
}我得到以下异常:
Causedby:com.coxautodev.graphql.tools.TypeClassMatcher$RawClassRequiredForGraphQLMappingException: Type java.util.Map<java.lang.String, com.grapql.research.domain.FldDef> cannot be mapped to a GraphQL type! Since GraphQL-Java deals with erased types at runtime, only non-parameterized classes can represent a GraphQL type. This allows for reverse-lookup by java class in interfaces and union types.
注意:我正在使用Java生态系统(graphql-java)
发布于 2017-12-07 16:55:21
从JSON中给出模式是不可能的,因为模式包含的信息远不止数据的形状。我认为对你来说最好的方法就是学习GraphQL的基础知识,这样设计简单的模式就会变得非常简单和有趣!也许可以从graphql.org上的学习部分开始。他们有一个关于schema的部分。基本上,您可以从标量(又称原语)和对象类型构建您的模式。所有类型都可以另外包装在不可空类型和/或列表类型中。GraphQL是为客户端设计的。理解GraphQL的最简单方法是对现有的API进行一些查询。Launchpad有很多示例可供您使用(当您知道一些JavaScript时,可以对其进行修改)。
发布于 2017-12-08 01:10:37
您可以尝试如下所示的更改:
模式定义:
type FldDef {
name: String!
type: String!
opt: Boolean!
}
type Product {
id: String!
name: String!
fldDef: [FldDef]! // make it as Collection of FldDef
}Java类:
public class FldDef {
private String name;
private String type;
private boolean opt;
// getters & setters
}
public class Product {
private String id;
private String name;
private List<FldDef> fldDef; // Change to List of FldDef
// getters & setters
}希望能有所帮助。
https://stackoverflow.com/questions/47677140
复制相似问题