Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

graphql - Schema from nested JSON list

I have a JSON list which captures one to many relationships.

For example, School can have multiple Class objects and Class can have multiple Student objects, but Student only belongs to one Class and one School:

{
  "School": [ {
    "id": 1,
    "name": "Grad School",
    "Class": [ {
         "name": 101,
         "Student": [ {
              "name": 501,
              "propertyA": "test"
         }]
     }]
  }]
}

I am trying to convert this JSON example into an appropriate schema but the nesting is causing issues. Apollo appears to be able to help but the example below isn't very descriptive: https://launchpad.graphql.com/4nqqqmr19

I'm looking for suggestions on how to handle this situation, whether that be through a JSON schema converter (which handles nested situations) or other.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you issue is not really the schema, which to me looks straightforward:

You have these types (everything dummy code as you have not specified in what language/framework you want to provide the GraphQL-Api):

SchoolType
  id ID
  name String
  classes [Class]
  students [Students]

ClassType
  id ID
  name String
  school School
  students [Student]

StudentType
  id ID
  name String
  class Class
  school School

Then we need an entry point

classQueryType
  name "school"
  argument :id, ID
  resolve do
    schools.where(id: argument["id"])

So we have the schema. The bigger work is probably to get the different types to access the JSON Schema in a way that the types above work.

So let's say, we read the JSON data somehow, with the structure you have.

 const DATA = JSON.parse("your-example.json")

We need to convert this into different collections of objects, so we can query them dynamically:

 schools = []
 classes =  []
 people = []

  def build_schools(data)
    data.schools.for_each do |school|
       schools.push(
         name: school.name, 
         id: school.id, 
         classes: build_classes(school)
       )
    end
 end

 def build_classes(school)
   ids = []
   school.classes.for_each do  |class|
     ids.push(class.id)
     classes.push(
       id: class.id
       name: class.name
       school_id: school.id # you create your own references, to associate these objects
       students: build_students(class)
     )
   end
   return ids
 end

 ...

But then you still need to hook this up, with your type system. Which means to write your resolvers:

For example on the StudentType

StudentType
 id ID
 name String
 class Class
 school School
   resolve(object) ->
     school_id = students.where(id: object.id).class_id.school_id
     schools.where(id: school_id)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...