【问题标题】:graphql/graphcool: How to write a mutation that links both a one:one and a one:many relationshipgraphql/graphcool:如何编写一个连接一对一和一对多关系的突变
【发布时间】:2018-07-13 19:51:45
【问题描述】:

我的架构如下:

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. Artist ID 数组
  2. Venue 的 ID
  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 和适当的 VenueArtist ID 之间创建关系?

【问题讨论】:

    标签: graphql graphcool


    【解决方案1】:

    我必须构建 JSON,以便在我编写 Show 时可以访问 Artists 和 Venue 的列表。

    然后我必须将每个 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]
            }
    
        }
    

    首先运行查询以确保在 graphcool 中不存在代表特定日期和 Venue 联合的 Show,然后运行突变以添加 Show

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

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

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

    【讨论】:

      猜你喜欢
      • 2018-06-20
      • 2022-11-13
      • 2021-02-24
      • 1970-01-01
      • 2018-10-03
      • 2018-09-27
      • 1970-01-01
      • 2018-12-31
      • 2014-05-26
      相关资源
      最近更新 更多