简体   繁体   English

嵌套JSON列表中的架构

[英]Schema from nested JSON list

I have a JSON list which captures one to many relationships. 我有一个JSON列表,它捕获了一对多的关系。

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. 我正在尝试将此JSON示例转换为适当的架构,但是嵌套引起了问题。 Apollo appears to be able to help but the example below isn't very descriptive: https://launchpad.graphql.com/4nqqqmr19 阿波罗(Apollo)似乎可以提供帮助,但以下示例的描述性很差: 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. 我正在寻找有关如何处理这种情况的建议,无论是通过JSON模式转换器(处理嵌套情况)还是其他方式。

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): 您有以下几种类型(您没有以要提供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. 更大的工作可能是以上面的类型起作用的方式来获取不同的类型来访问JSON Schema。

So let's say, we read the JSON data somehow, with the structure you have. 假设我们以您拥有的结构以某种方式读取JSON数据。

 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上

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)

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

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