mark
01/24/2019, 2:30 PMtype Dashboard {
id: ID! @unique
name: String!
widgets: [Widget!]! @relation(name: "DashboardToWidgets", onDelete: SET_NULL)
user: User! @relation(name: "UserToDashboard", onDelete: SET_NULL)
createdAt: DateTime!
updatedAt: DateTime!
}
type Widget {
id: ID! @unique
name: String!
createdAt: DateTime!
updatedAt: DateTime!
}
type Portfolio {
id: ID! @unique
name: String!
widget: Widget!
assets: [Asset!]! @relation(name: "PortfolioToAsset", onDelete: CASCADE)
createdAt: DateTime!
updatedAt: DateTime!
}Ecker
01/24/2019, 2:49 PMextend, to let Portfolio extend Widget. But I'm still a beginner at GraphQL myself so it's just a thought.mark
01/24/2019, 3:11 PMEcker
01/24/2019, 3:14 PMunion, so your Dashboard can have an array of multiple different widget types.
https://stackoverflow.com/a/52102073mark
01/24/2019, 3:15 PMmark
01/24/2019, 3:16 PMmark
01/24/2019, 3:16 PMEcker
01/24/2019, 3:16 PMmark
01/24/2019, 3:20 PMDashboard
β The field `widgets` has the type `[Widget!]!` but there's no type or enum declaration with that name.mark
01/24/2019, 3:20 PMunion Widget = Newsfeed | Portfoliomark
01/24/2019, 3:22 PMmark
01/24/2019, 3:25 PMmark
01/24/2019, 3:25 PMEcker
01/24/2019, 3:53 PMEcker
01/24/2019, 3:54 PMdata: Json type a bit, which can be any data when you need to set some flags etc. But think that type has to be converted to a String in some cases when Json is not supported.mark
01/24/2019, 4:23 PMmark
01/24/2019, 8:20 PMmark
01/24/2019, 8:21 PMmark
01/24/2019, 8:21 PMmark
01/24/2019, 8:22 PMmark
01/24/2019, 8:23 PMmark
01/24/2019, 8:23 PMinterface Widget {
id: ID!
name: String!
}
type Portfolio implements Widget {
id: ID!
name: String!
assets: [Asset!]!
createdAt: String!
updatedAt: String!
}
type Newsfeed implements Widget {
id: ID!
name: String!
feed: String!
}mark
01/24/2019, 8:24 PMEcker
01/24/2019, 8:35 PMtype Dashboard { β¦ widgets: [Widget!]! β¦ } or do you have to do it in a different way?mark
01/24/2019, 8:57 PMtype Dashboard {
id: ID!
name: String!
panels: [Panel!]!
user: User!
createdAt: String!
updatedAt: String!
}
type Panel {
id: ID!
name: String!
widgets: [Widget!]!
}
interface Widget {
id: ID!
name: String!
}
type Portfolio implements Widget {
id: ID!
name: String!
assets: [Asset!]!
createdAt: String!
updatedAt: String!
}
type Newsfeed implements Widget {
id: ID!
name: String!
feed: String!
}mark
01/24/2019, 8:58 PMEcker
01/24/2019, 9:10 PMPortfolio and Newsfeed as a return value of type Panel { β¦ widgets: [Widget!]! }?mark
01/24/2019, 9:56 PMmark
01/24/2019, 9:59 PMmark
01/24/2019, 9:59 PMmark
01/24/2019, 9:59 PMmark
01/24/2019, 10:17 PMmark
01/24/2019, 10:19 PMtype Panel {
id: ID! @unique
name: String!
dashboard: Dashboard! @relation(name: "DashboardToPanel", onDelete: SET_NULL)
}
type Portfolio {
id: ID! @unique
name: String!
assets: [Asset!]! @relation(name: "PortfolioToAsset", onDelete: CASCADE)
# not sure about relation below
panel: Panel! @relation(name: "PortfolioToPanel", onDelete: SET_NULL)
createdAt: DateTime!
updatedAt: DateTime!
}
type Newsfeed {
id: ID! @unique
name: String!
feed: String!
# not sure about relation below
panel: Panel! @relation(name: "NewsfeedToPanel", onDelete: SET_NULL)
createdAt: DateTime!
updatedAt: DateTime!
}
in your graphqlserver schema:
type Query {
widgets: [Widget!]!
}
type Panel {
id: ID!
name: String!
dashboard: Dashboard!
widgets: [Widget!]!
}
interface Widget {
id: ID!
name: String!
}
type Portfolio implements Widget {
id: ID!
name: String!
assets: [Asset!]!
createdAt: String!
updatedAt: String!
}
type Newsfeed implements Widget {
id: ID!
name: String!
feed: String!
}
Your query resolver:
widgets: async (parent, args, { prisma }, info) => {
return [
...(await prisma.query.portfolios(null, info)),
...(await prisma.query.newsfeeds(null, info)),
]
}
Your Widget resolver:
export const Widget = {
__resolveType(book, context, info) {
if (book.assets) {
return 'Portfolio'
}
if (book.feed) {
return 'Newsfeed'
}
return null
},
}
Your query
query {
widgets {
id
name
... on Portfolio {
id
name
assets {
id
}
}
... on Newsfeed {
id
name
feed
}
}
}
Response
{
"data": {
"widgets": [
{
"id": "cjrb6ckus00610795cpx3y0gi",
"name": "m",
"assets": []
},
{
"id": "cjrb6gb7j006i0795wswrw42h",
"name": "m",
"feed": "hola"
}
]
}
}
πππ
You should also override the Panel resolver as such
export const Panel = {
widgets: async (parent, args, { prisma }, info) => {
return [
...(await prisma.query.portfolios(null, info)),
...(await prisma.query.newsfeeds(null, info)),
]
},
}
And add it to your resolvers of coursemark
01/24/2019, 10:26 PMmark
01/25/2019, 1:24 AMEcker
01/25/2019, 5:37 AMmark
01/25/2019, 11:18 AMmark
01/25/2019, 11:18 AMEcker
01/25/2019, 12:40 PMmark
01/25/2019, 12:47 PMmark
01/25/2019, 12:48 PMtype Panel {
portfolios: [Portfolios!]!
newsfeeds: [Newsfeeds!]!
}mark
01/25/2019, 12:48 PMmark
01/25/2019, 12:49 PMEcker
01/25/2019, 3:29 PMmark
01/25/2019, 4:46 PMmark
01/25/2019, 4:46 PMEcker
03/24/2019, 9:30 PMtype Panel {
portfolios: [Portfolios!]!
newsfeeds: [Newsfeeds!]!
}
OR did you hack a union solution together in the end? I see Prisma still doesn't support union.Ecker
03/29/2019, 8:08 PMmark
03/30/2019, 1:29 AMmark
03/30/2019, 1:30 AMtype Panel {
portfolios: [Portfolios!]!
newsfeeds: [Newsfeeds!]!
}
type PanelSetting {
id: !ID
# common panel related settings here
}
type Portfolio {
panel: [Panel!]
panelSetting: PanelSetting! #add this in prima schema "@relation(onDelete: CASCADE)"
}
type Newsfeed {
panel: [Panel!]
panelSetting: PanelSetting! #add this in prima schema "@relation(onDelete: CASCADE)"
}Ecker
03/30/2019, 6:45 AM