GraphQL schema equivalent of this Json?

流过昼夜 提交于 2019-12-11 15:12:30

问题


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:

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

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)


回答1:


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. I think the best for you would be to learn the basics of GraphQL, designing simple schemas is then very easy and fun! Maybe start with the learning section on 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. The easiest way of understanding GraphQL is making some queries against an existing API. Launchpad has a lot of examples that you can use (and modify when you know some JavaScript).




回答2:


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:

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.



来源:https://stackoverflow.com/questions/47677140/graphql-schema-equivalent-of-this-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!