【发布时间】:2019-12-18 03:31:00
【问题描述】:
这是我第一次使用 GraphQL,我为一个包含用户、歌曲和提示的应用程序创建了一个模式。请注意,听众和艺术家都表示为用户,并且任何用户都可以通过创建提示来“提示”歌曲
我无法正确定义架构中的一对多和多对多关系。我也很困惑如何编写突变以创建正确指向彼此的 Song/User/Tip 对象。
我知道我需要在我的架构定义中使用@connection 指令,并且我已经尝试遵循this example,但我仍然对如何将此设计转换为我的用例感到困惑。
这是我在指定对象之间的关系时所采取的尝试:
type Song @model{
id: ID!
title: String!
artist: String!
artistArray: [User]! @connection(name: "SongArtists")
tips: [Tip]! @connection(name: "SongTips")
totalAmountReceived: Float!
}
type Tip @model{
id: ID!
from: User! @connection(name: "UserTipsSent")
to: User! @connection(name: "UserTipsReceived")
song: Song! @connection(name: "SongTips")
amount: Float!
createdAt: String!
hash: String!
}
type User @model{
id: ID!
name: String!
walletAddress: String!
totalAmountDonated: Float!
totalAmountReceived: Float!
songs: [Song]! @connection(name: "SongArtists")
tipsSent: [Tip]! @connection(name: "UserTipsSent")
tipsReceived: [Tip]! @connection(name: "UserTipsReceived")
}
我的架构没有指定任何连接
type Song @model {
id: ID!
title: String!
artist: String!
artistArray: [User]!
tips: [Tip]!
totalAmountReceived: Float!
}
type Tip @model {
id: ID!
from: User!
to: User!
song: Song!
amount: Float!
createdAt: String!
hash: String!
}
type User @model {
id: ID!
name: String!
walletAddress: String!
totalAmountDonated: Float!
totalAmountReceived: Float!
songs: [Song]!
tipsSent: [Tip]!
tipsReceived: [Tip]!
}
我要实现以下关系
- 提示必须与一首歌曲、“来自”用户和“至”用户相关联
- 一首歌曲必须至少有一位艺术家(用户)
- 一个用户可能有很多歌曲
- 用户可能已发送和/或收到许多提示
我也不知道如何编写一个突变(例如)创建一首歌曲(指向正确的艺术家/用户),同时确保艺术家(用户对象中的歌曲数组)引用我刚刚创作的歌曲。
值得注意的是,我在 AWS AppySync DynamoDB 中创建了 3 个表(歌曲、用户、提示),并且希望能够检索(例如)用户、他们制作的所有歌曲,以及他们在一次查询中收到的所有提示。
【问题讨论】:
标签: graphql aws-appsync