简体   繁体   English

Graphql嵌套变异

[英]Graphql nested Mutation

I'm building a Graphql schema to mutate/query restaurants and trying to store the opening times in a nested object. 我正在建立一个Graphql模式来对餐厅进行突变/查询,并尝试将开放时间存储在一个嵌套对象中。

The structure should look as follows: 结构应如下所示:

        [
            { 
                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 }

            },
        ]

The restaurant variable contains the values of the opening hours that I', formatting before sending them to the API and storing them. restaurant变量包含I'的营业时间值,在将其发送到API并存储之前进行格式化。

My Graphql schema looks as follows: 我的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
}

When I hit that API endpoint I get the following error message: 当我到达该API端点时,出现以下错误消息:

Expected type [DayInput], found "[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]".

I'm not sure whether my call is incorrectly formatted or whether I'm making a mistake in the schema itself. 我不确定我的通话格式是否错误,或者我在模式本身上是否出错。 I'm still rather new to Graphql and stuck on this. 我对Graphql还是很陌生,并坚持了下来。

You're seeing that error because the structure of your input doesn't match your schema. 您正在看到该错误,因为输入的结构与架构不匹配。 The square brackets ( [] ) in your schema indicate a List -- if you wrap an input type in square brackets, then the corresponding input should be an array. 模式中的方括号( [] )表示列表-如果将输入类型包装在方括号中,则对应的输入应为数组。 To have a schema that matches the array you've shown, you should remove the brackets from around TimeInput : 要具有与显示的数组匹配的模式,应从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
}

However, you could also consider simplifying this structure altogether: 但是,您也可以考虑完全简化此结构:

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
}

You can then just send a plain object, without using an array: 然后,您可以直接发送一个普通对象,而无需使用数组:

{ 
  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 },
},

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

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