0

我有一个架构如下:

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 数据,创建Artists 和Venues 并将它们返回给我。

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

  1. 一组ArtistID
  2. 一个IDVenue
  3. 填充其余Show数据所需的所有信息(在名为 的对象中showInfo

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

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

如何编辑它,以便在Show我正在创建的和适当的VenueArtistID 之间创建关系?

4

1 回答 1

0

Artist我必须构造 JSON ,以便Venue在编写Show.

然后,我必须将每个ArtistVenue(或验证这些是否已经写入)都写到 graphcool 并取回相应的 ID。

然后我执行了如下函数:

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

    }

它首先运行查询以确保 aShow表示特定日期的联合并且Venue在 graphcool 中尚不存在,然后运行突变以添加 a Show

请注意,这种突变是专门为现有对象VenueArtist对象(后者以列表形式)获取 ID 并将它们作为变量附加到行中的:

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

使用该graphql-request库,它允许您将变量散列传递到对 graphcool 的请求中。

于 2018-02-05T03:18:38.263 回答