本文介绍了带有嵌套突变的Graphql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出如何使用graphql突变对嵌套对象进行突变.例如,我具有以下架构:

I am trying to figure out how to mutate a nested object with graphql mutations, if possible. For instance I have the following schema:

type Event {
    id: String
    name: String
    description: String
    place: Place
}

type Place {
    id: String
    name: String
    location: Location
}

type Location {
    city: String
    country: String
    zip: String
}

type Query {
    events: [Event]
}

type Mutation {
    updateEvent(id: String, name: String, description: String): Event
}

schema {
    query: Query
    mutation: Mutation
}

如何在updateEvent突变内添加位置信息?

How can I add the place information inside my updateEvent mutation?

推荐答案

如果要将整个对象添加到突变中,则必须定义输入类型的graphql元素.这是一个小的备忘单的链接.

If you want to add a whole object to the mutation you have to define a graphql element of the type input. Here is a link to a small cheatsheet.

在您的情况下,它看起来可能像这样:

In your case it could look like this:

`
type Location {
    city: String
    country: String
    zip: String
}

type Place {
    id: String
    name: String
    location: Location
}

type Event {
    id: String
    name: String
    description: String
    place: Place
}

input LocationInput {
   city: String
   country: String
   zip: String
}

input PlaceInput {
    id: ID!
    name: String!
    location: LocationInput!
}

type Query {
    events: [Event]
}

type Mutation {
    updateEvent(id: String, name: String, description: String, place: PlaceInput!): Event
}

schema {
    query: Query
    mutation: Mutation
}
`

这篇关于带有嵌套突变的Graphql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 12:04