简体   繁体   中英

Graphql nested Mutation

I'm building a Graphql schema to mutate/query restaurants and trying to store the opening times in a nested object.

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.

My Graphql schema looks as follows:

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:

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.

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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