嵌套JSON列表中的架构 [英] Schema from nested JSON list
问题描述
我有一个JSON列表,它捕获了一对多的关系.
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"
}]
}]
}]
}
我正在尝试将此JSON示例转换为适当的架构,但是嵌套引起了问题.阿波罗(Apollo)似乎可以提供帮助,但以下示例的描述性很差: https://launchpad.graphql.com/4nqqqmr19
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
我正在寻找有关如何处理这种情况的建议,无论是通过JSON模式转换器(处理嵌套情况)还是其他方式.
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.
推荐答案
我认为您提出的并不是真正的架构,对我而言,它看起来很简单:
I think you issue is not really the schema, which to me looks straightforward:
您具有以下类型(您没有以要提供GraphQL-Api的语言/框架指定的所有伪代码):
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"])
所以我们有了模式.更大的工作可能就是以上面的类型起作用的方式,让不同的类型访问JSON Schema.
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数据.
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:
例如在StudentType上
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)
这篇关于嵌套JSON列表中的架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!