graphql/graphcool:如何编写同时链接一对一和一对多关系的变异 [英] graphql/graphcool: How to write a mutation that links both a one:one and a one:many relationship

查看:50
本文介绍了graphql/graphcool:如何编写同时链接一对一和一对多关系的变异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的架构:

type Artist @model {
  id: ID! @isUnique
  createdAt: DateTime!
  updatedAt: DateTime!
  name: String! @isUnique
  songkickId: String
  shows: [Show!]! @relation(name: "ArtistShow")
}

type Show @model {
  id: ID! @isUnique
  createdAt: DateTime!
  updatedAt: DateTime!
  name: String
  songkickId: String
  date: DateTime! 
  soldOut: Boolean!
  pit: Boolean!
  ages: String!
  price: String!
  multiDay: Boolean!
  artists: [Artist!]! @relation(name: "ArtistShow")
  venue: Venue! @relation(name: "ShowVenue")
}

type Venue @model {
  id: ID! @isUnique
  createdAt: DateTime!
  updatedAt: DateTime!
  name: String! @isUnique
  address: String
  latitude: Float
  longitude: Float
  metro: String
  songkickId: String @isUnique
  shows: [Show!]! @relation(name: "ShowVenue")
}

我写了一些变体,根据JSON数据,创建了 Artist s和 Venue s并将它们返回给我.

I have written mutations that, given JSON data, create Artists and Venues and return those to me.

当我想创建一个 Show 时,我有:

At the time that I want to create a Show, I have:

  1. 一组 Artist ID
  2. 场地
  3. 的ID
  4. 所有必需的信息,以填充其余的 Show 数据(在名为 showInfo 的对象中)
  1. An array of Artist IDs
  2. An ID for a Venue
  3. All required info to populate the rest of the Show data (in an object called showInfo)

我有一个看起来像这样的突变:

I have a mutation that looks like this:

        mutation: gql`
            mutation {
                createShow(
                    date: "${showInfo.date}"
                    soldOut: ${showInfo.soldOut}
                    pit: ${showInfo.pit}
                    ages: "${showInfo.ages}"
                    price: "${showInfo.price}"
                    multiDay: ${showInfo.multiDay}
                ) {
                    id
                }
            }
        `,

如何进行编辑,以便在创建的 Show 与相应的 Venue Artist ID之间建立关系?

How do I edit this so that I also create relations between a Show that I am creating and appropriate Venue and Artist IDs?

推荐答案

我必须构造JSON,以便可以访问 Artist s和 Venue 在我编写 Show 时.

I had to structure the JSON so that I had access to a list of Artists and a Venue at the time that I was writing a Show.

然后,我不得不将每个 Artist Venue (或验证是否已经编写)写入graphcool并取回各自的ID.

I then had to write each Artist and Venue (or validate whether these had already been written) to graphcool and get a respective ID back.

然后我执行了如下函数:

I then executed a function like the following:

async findShowOrCreate (showInfo, artists, venue) {
        const tempDate = new Date(showInfo.date);
        this.logger.info(`BEGIN : write show to graphcool (${showInfo.venue} on ${tempDate.toDateString()})`);

        const showQuery =   `
                                query ($venueId: ID!, $date: DateTime) {
                                    allShows(filter: {
                                            venue: {
                                                id: $venueId
                                            }
                                            date: $date
                                    })  {
                                            id
                                        }
                                }
                            `

        const existentialShowTest = await this.client.request(showQuery, 
            {venueId: venue, date: showInfo.date})

        if (existentialShowTest.allShows.length < 1){

            const addShowQuery =    `
                                        mutation createShow($artistsIds:[ID!], $venueId:ID  ) {
                                            createShow (
                                                artistsIds: $artistsIds
                                                venueId: $venueId
                                                date: "${showInfo.date}"
                                                soldOut: ${showInfo.soldOut}
                                                pit: ${showInfo.pit}
                                                ages: "${showInfo.ages}"
                                                price: "${showInfo.price}"
                                                multiDay: ${showInfo.multiDay}
                                            ) {
                                                id
                                            }
                                        }
                                    `
            const finalResult = await this.client.request(addShowQuery, 
                {venueId: venue, artistsIds: artists})
                .then(data => {
                 const result = data.createShow.id
                 this.logger.info(`FINISH: write show to graphcool as ${result}`)
                 return result
                })
                .catch(error => this.logger.error(error));

            return finalResult

        } else {
            this.logger.info(`FINISH: found show in graphcool`)
            return existentialShowTest.allShows[0]
        }

    }

首先运行查询以确保在graphcool中不存在表示特定日期和 Venue 的并集的 Show 添加 Show .

which first ran a query to make sure that a Show representing the union of a specific date and Venue didn't already exist in graphcool, then ran a mutation to add a Show.

请注意,此突变的结构特别适合于为现有的 Venue Artist 对象(后者以列表形式)获取ID,并将它们作为变量附加到行中:

Note that this mutation was specifically structured to take in Ids for existing Venue and Artist objects (the latter in list form) and to attach these as variables in the line:

 client.request(addShowQuery, 
            {venueId: venue, artistsIds: artists})

使用 graphql-request 库,该库允许您将变量的哈希传递到对graphcool的请求中.

using the graphql-request library, which allows you to pass a hash of variables into requests to graphcool.

这篇关于graphql/graphcool:如何编写同时链接一对一和一对多关系的变异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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