简体   繁体   English

相当于这个Json的GraphQL模式?

[英]GraphQL schema equivalent of this Json?

I'm newbie to GraphQL, and was wondering if someone could help me figure out what is the equivalent of below JSON into GraphQL schema: 我是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
     }]
  }
]

So far I managed to map above response to below object: 到目前为止,我设法将上面的响应映射到下面的对象:

  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

 }

Then my schema looks like below, but the problem I'm having is as a part of Product object, I've a Map which I would like to get the schema right for it, but I'm having difficulty getting the schema right! 然后我的模式如下所示,但是我遇到的问题是作为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?
 }

I get below exception: 我得到以下异常:

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.

Note: I'm working with Java eco-system (graphql-java) 注意:我正在使用Java生态系统(graphql-java)

giving you a schema from your JSON is not really possible because a schema contains much more information than just simply the shape of your data. 从JSON提供一个模式实际上是不可能的,因为模式不仅包含数据的形状,还包含更多的信息。 I think the best for you would be to learn the basics of GraphQL, designing simple schemas is then very easy and fun! 我认为最适合您的是学习GraphQL的基础知识,然后设计简单的架构就非常容易且有趣! Maybe start with the learning section on graphql.org . 也许从graphql.org的学习部分开始 They have a section about the schema . 他们有关于模式的部分。 Basically you build your schema from scalars (aka primitives) and object types. 基本上,您是根据标量(即原语)和对象类型构建模式的。 All types can additionally be wrapped in the non-nullable type and/or of the list type. 所有类型都可以另外包装为非空类型和/或列表类型。 GraphQL was designed for clients. GraphQL是为客户设计的。 The easiest way of understanding GraphQL is making some queries against an existing API. 了解GraphQL的最简单方法是针对现有API进行一些查询。 Launchpad has a lot of examples that you can use (and modify when you know some JavaScript). Launchpad有许多示例可供您使用(在您了解一些JavaScript时可以进行修改)。

You can try some changes as below: 您可以尝试以下更改:

Schema definition: 模式定义:

type FldDef {
   name: String!
   type: String!
   opt: Boolean!
}

type Product {
   id: String!
   name: String!
   fldDef: [FldDef]! // make it as Collection of FldDef
 }

Java Class: 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
}

Hope it can help. 希望能有所帮助。

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

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