按照您的示例,让我们将您的 Object 文字分解为它所代表的实体。在高级细节中,您正在查看
- 代表约会的实体
- 与所述约会关联的用户
- 与某个 AppointmentTime 关联的约会列表
- 给定日期的约会时间列表
- 日程表形式的日期列表
对我来说,这似乎与您所拥有的示例中的数据非常匹配(但不完全匹配)。基于此,我在下面定义了一个架构,其中一些有意的设计决策与您的提议略有不同。
让我们定义一些类型,并将它们组合在一起,从 User 类型开始:
type User {
id: ID!
name: String!
email: String!
# Depending on your requirements, a user may not have to provide a phone number.
phoneNumber: String
# Depending on your requirements, a user may not have an Avatar.
avatarUrl: String
}
type Appointment {
id: ID!
user: User!
}
type AppointmentTime {
time: String!
appointments: [Appointment!]!
}
type Day {
# The Day ID could be the actual day itself, i.e. 2020-12-29
id: ID!
open: Boolean!
hours: Int!
appointmentTimes: AppointmentTime!
}
type Schedule {
days: [Day!]!
}
这将允许您编写如下查询(假设您有一个 getSchedule 查询 - 或类似效果):
getSchedule {
days {
id
open
hours
appointmentTimes {
time
appointments {
id
user {
name
email
phoneNumber
avatarUrl
}
}
}
}
}
{
days: [
{
id: "2020-12-29",
open: true,
hours: 2,
appointmentTimes: [
{
time: "09:00-am",
appointments: [
{
id: "5223a4ef-a3cf-4e2f-b761-3e06193e2e21",
user: {
name: "John Smith",
email: "john@smith.com",
phoneNumber: "123...",
avatarUrl: "...",
}
},
...
]
},
...
]
},
...
]
}
请注意,这最终会产生与您发布的输出略有不同的输出。为什么?
嗯,我做出了以下设计选择,我鼓励你也去研究一下:
-
用户应该是一个单独的字段。在您的示例中,用户和约会信息都在同一个键下——09:00-am——在这里,我们想利用 GraphQL 的类型系统通过定义我们可以附加到约会的 User 类型来规范化模式.也更适合自省。
-
您的appointments 键指向另一个对象 作为其值,而不是列表。由于您要在一天结束时返回约会列表,因此您应该将其建模为 GraphQL List
-
添加了与约会列表关联的AppointmentTime 类型。这使您可以同时进行多个约会。 (未来证明)
-
每天都有一个AppointmentTime 列表——这是最佳选择,因为您现在不再依赖键(在您的情况下为09:00-am)来定义与每个约会时间关联的数据。 (未来证明)
如果您确实希望对象字面量与 graphql 输出匹配完全,您可以将我选择提取的一些字段内联到其他类型,但实际上,您应该为此利用列表那种东西。