我是GraphQL的新手,我正在努力找出Schema设计背后的最佳实践。为GraphQL设计模式和连接感觉有点像设计关系数据库,但我不确定所有最佳实践。在GraphQL中,您将如何实现以下解决方案:
1. Honda Accord
2. Ford Focus
3. Ducati Monster
4. Cessna 152
5. Santa Cruz Nomad
1. Honda Accord (car)
2. Ford Focus (car)
3. Ducati Monster (motorcycle)
4. Cessna 152 (airplane)
5. Santa Cruz Nomad (bicycle)
对于实现相同界面(id,make,model)的每种类型的车辆,已经存在架构和解析器:
Car {
id,
make
model
...
}
MotorCycle {
id
make
model
...
}
AirPlane {
id,
make
model
...
}
Bicycle {
id
make
model
...
}
创建车库类型的最佳方法是什么,可以用来访问车库中可能存在的不同车辆类型列表,即使每辆车都有自己的架构?
我不确定这是否可行,或者如何正确实现,但我想做的是:
{
garage(id: "cGVvcGx10jE=") {
location
vehicleConnection {
edges {
node {
make
model
}
}
}
}
}
{
"data": {
"garage": {
"location": "Sunnyvale",
"vehicleConection": {
"edges": [
{
"node": {
"make": "Honda",
"model": "Accord"
}
},
{
"node": {
"make": "Ford",
"model": "Focus"
}
}, {
"node": {
"make": "Ducati",
"model": "Monster"
}
},
{
"node": {
"make": "Cessna",
"model": "152"
}
},
{
"node": {
"make": "Santa Cruz",
"model": "Nomad"
}
}
]
}
}
答案 0 :(得分:0)
好吧,你可以使用inheratence。
interface Vehicle {
id: ID!,
make: String,
model: String
}
type Car implements Vehicle {
...
}
type MotoCycle implements Vehicle {
...
}
// and now you can connect Garage to Vehicle
请参阅http://graphql.org/learn/schema/#interfaces
其他可能性:联盟类型http://graphql.org/learn/schema/#union-types
它们大致相同,但由于union不期望对象之间存在某些共同属性,因此保存的结构更大。