0

我是 GraphQL 的新手,想知道是否有人可以帮我弄清楚以下 JSON 到 GraphQL 模式中的等价物:

[
 {
   "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)

4

2 回答 2

0

从您的 JSON 中为您提供一个模式实际上是不可能的,因为模式包含的信息不仅仅是数据的形状。我认为最适合你的是学习 GraphQL 的基础知识,这样设计简单的模式就非常容易和有趣!也许从graphql.org上的学习部分开始。他们有一个关于schema的部分。基本上,您从标量(又名原语)和对象类型构建您的模式。所有类型都可以另外包装在不可空类型和/或列表类型中。GraphQL 是为客户设计的。理解 GraphQL 的最简单方法是对现有 API 进行一些查询。Launchpad有很多示例可供您使用(并且在您了解一些 JavaScript 时进行修改)。

于 2017-12-07T08:55:21.453 回答
0

您可以尝试以下一些更改:

架构定义:

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
}

希望它可以提供帮助。

于 2017-12-07T17:10:36.613 回答