具有嵌套突变的 Graphql? [英] Graphql with nested mutations?

查看:29
本文介绍了具有嵌套突变的 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆