繁体   English   中英

相当于这个Json的GraphQL模式?

[英]GraphQL schema equivalent of this Json?

我是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)

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

您可以尝试以下更改:

模式定义:

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
}

希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM