I have read the many to many relationship doc here Prisma Relations and Redwood's Generators | RedwoodJS Docs
I have the following Schema
datasource db {
provider = "postgres"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
binaryTargets = "native"
}
model User {
id Int @id @default(autoincrement())
uuid String @unique
email String @unique
name String?
userRoles UserRole[]
quests Quest[]
ambassadorProfile AmbassadorProfile?
}
model UserRole {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
name String
user User? @relation(fields: [userId], references: [id])
userId Int?
password String?
@@unique([name, userId])
}
model Quest {
id Int @id @default(autoincrement())
name String?
userId Int
submittedByuser User @relation(fields: [userId], references: [id])
heros Hero[]
ambassadors AmbassadorProfile[]
}
model AmbassadorProfile {
id Int @id @default(autoincrement())
name String?
userId Int @unique
questId Int
user User @relation(fields: [userId], references: [id])
quests Quest[]
twitter String
profileImage String
}
model Hero {
id Int @id @default(autoincrement())
name String?
questId Int
quests Quest[]
twitter String
}
I am primarily concerned right now with a Quest model have many Heros.
When I save a new Quest I also create a new Hero with the questId so I thought that would link the two, but it does not it just adds the id
const onSave = async (input) => {
const castInput = {
name: input.name,
userId: currentUser.user.id,
}
const quest = await createQuest({ variables: { input: castInput } })
const castInputHero = {
name: input.heroName,
questId: quest.data.createQuest.id,
twitter: input.twitter,
}
await createHero({ variables: { input: castInputHero } })
}
I have a relation table called _HeroToQuest
and it has nothing in it. So not sure how to get it to create the relation so I can then query the heros on a Quest
<>
{quests.map((quest) => (
<div key={quest.id}>
<h3>{truncate(quest.name)}</h3>
{quest.heros.map((hero) => (
<div key={hero.id}>
<h4>{hero.name}</h4>
<p>{hero.twitter}</p>
</div>
))}
</div>
))}
</>
quests.sdl.ts
export const schema = gql`
type Quest {
id: Int!
name: String
userId: Int!
submittedByuser: User!
heros: [Hero]!
ambassadors: [AmbassadorProfile]!
}
type Query {
quests: [Quest!]! @skipAuth
quest(id: Int!): Quest @skipAuth
}
input CreateQuestInput {
name: String
userId: Int!
}
input UpdateQuestInput {
name: String
userId: Int
}
type Mutation {
createQuest(input: CreateQuestInput!): Quest! @requireAuth
updateQuest(id: Int!, input: UpdateQuestInput!): Quest! @requireAuth
deleteQuest(id: Int!): Quest! @requireAuth
}
`
quests.ts
import { db } from 'src/lib/db'
import type {
QueryResolvers,
MutationResolvers,
QuestResolvers,
} from 'types/graphql'
export const quests: QueryResolvers['quests'] = () => {
return db.quest.findMany()
}
export const quest: QueryResolvers['quest'] = ({ id }) => {
return db.quest.findUnique({
where: { id },
})
}
export const createQuest: MutationResolvers['createQuest'] = ({ input }) => {
return db.quest.create({
data: input,
})
}
export const updateQuest: MutationResolvers['updateQuest'] = ({
id,
input,
}) => {
return db.quest.update({
data: input,
where: { id },
})
}
export const deleteQuest: MutationResolvers['deleteQuest'] = ({ id }) => {
return db.quest.delete({
where: { id },
})
}
export const Quest: QuestResolvers = {
submittedByuser: (_obj, { root }) =>
db.quest.findUnique({ where: { id: root.id } }).submittedByuser(),
heros: (_obj, { root }) =>
db.quest.findUnique({ where: { id: root.id } }).heros(),
ambassadors: (_obj, { root }) =>
db.quest.findUnique({ where: { id: root.id } }).ambassadors(),
}
I have both hero’s and quests but there are not related to eachother in the common relation table. Also Redwood has been a real joy to work with since 1.0. I find I can execute my ideas so much quicker and play around beyond the odd stumbling block like so.