我正在构建一个Graphql模式来对餐厅进行突变/查询,并尝试将开放时间存储在一个嵌套对象中。
结构应如下所示:
[
{
monday: { open: restaurant.Open_Monday, close: restaurant.Close_Monday }
},
{
tuesday: { open: restaurant.Open_Tuesday, close: restaurant.Close_Tuesday }
},
{
wednesday: { open: restaurant.Open_Wednesday, close: restaurant.Close_Wednesday }
},
{
thursday: { open: restaurant.Open_Thursday, close: restaurant.Close_Thursday }
},
{
friday: { open: restaurant.Open_Friday, close: restaurant.Close_Friday }
},
{
saturday: { open: restaurant.Open_Saturday, close: restaurant.Close_Saturday }
},
{
sunday: { open: restaurant.Open_Sunday, close: restaurant.Close_Sunday }
},
]
restaurant变量包含我的营业时间值,将其格式化后再发送到API并进行存储。
我的Graphql模式如下:
input RestaurantInput {
key: Int!
name: String!
image: String!
telNumber: String!
bookingNumber: String
address1: String!
address2: String
suburb: String!
province: String!
postalCode: String
days: [DayInput]
cuisine: String
exclusions: String
restrictions: String
breakfast: String
lunch: String
supper: String
longitude: String
latitude: String
}
input DayInput {
monday: [TimeInput]
tuesday: [TimeInput]
wednesday: [TimeInput]
tursday: [TimeInput]
friday: [TimeInput]
saturday: [TimeInput]
sunday: [TimeInput]
}
input TimeInput {
open: String
close: String
}
当我点击该API端点时,出现以下错误消息:
Expected type [DayInput], found "[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]".
我不确定我的通话格式是否错误,或者我在模式本身上是否出错。我对Graphql还是很陌生,并坚持了下来。
答案 0 :(得分:1)
您看到该错误,因为输入的结构与架构不匹配。模式中的方括号([]
)表示一个列表-如果将输入类型包装在方括号中,则对应的输入应为数组。要拥有与您显示的数组匹配的架构,您应该从TimeInput
周围删除括号:
input RestaurantInput {
days: [DayInput]
# other fields
}
input DayInput {
monday: TimeInput
tuesday: TimeInput
wednesday: TimeInput
thursday: TimeInput
friday: TimeInput
saturday: TimeInput
sunday: TimeInput
}
input TimeInput {
open: String
close: String
}
但是,您也可以考虑完全简化此结构:
input RestaurantInput {
days: DayInput # <---- remove the List here
# other fields
}
input DayInput {
monday: TimeInput
tuesday: TimeInput
wednesday: TimeInput
thursday: TimeInput
friday: TimeInput
saturday: TimeInput
sunday: TimeInput
}
input TimeInput {
open: String
close: String
}
然后,您可以直接发送一个普通对象,而无需使用数组:
{
monday: { open: restaurant.Open_Monday, close: restaurant.Close_Monday },
tuesday: { open: restaurant.Open_Tuesday, close: restaurant.Close_Tuesday },
wednesday: { open: restaurant.Open_Wednesday, close: restaurant.Close_Wednesday },
thursday: { open: restaurant.Open_Thursday, close: restaurant.Close_Thursday },
friday: { open: restaurant.Open_Friday, close: restaurant.Close_Friday },
saturday: { open: restaurant.Open_Saturday, close: restaurant.Close_Saturday },
sunday: { open: restaurant.Open_Sunday, close: restaurant.Close_Sunday },
},