graphql/graphcool:如何编写同时链接一对一和一对多关系的变异 [英] graphql/graphcool: How to write a mutation that links both a one:one and a one:many relationship
问题描述
我有一个如下的架构:
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 Artist
s and Venue
s and return those to me.
当我想创建一个 Show
时,我有:
At the time that I want to create a Show
, I have:
- 一组
Artist
ID - 场地
- 所有必需的信息,以填充其余的
Show
数据(在名为showInfo
的对象中)
- An array of
Artist
IDs - An ID for a
Venue
- All required info to populate the rest of the
Show
data (in an object calledshowInfo
)
我有一个看起来像这样的突变:
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 Artist
s 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屋!