Nick
03/30/2021, 3:55 AMmodel User {
id Int @id @default(autoincrement())
name String
email String @unique
games UserGame[]
friends User[] @relation("Friends")
friendOf User[] @relation("Friends")
}
type User {
id: Int!
name: String!
email: String!
games: [UserGame]!
friends: [User]
friendOf: [User]
}
type Query {
user(email: String): User
users: [User!]!
}
input CreateUserInput {
name: String!
email: String!
}
input UpdateUserInput {
name: String
email: String
}
input FriendInput {
id: Int!
friendId: Int!
}
type Mutation {
createUser(input: CreateUserInput!): User
addFriend(input: FriendInput!): User
removeFriend(input: FriendInput!): User
}
export const user = ({ email }) => {
return db.user.findUnique({
where: { email },
include: { friends: true, friendOf: true },
})
}
export const users = () => {
return db.user.findMany({ include: { friends: true, friendOf: true } })
}
export const User = {
games: (_obj, { root }) =>
db.user.findUnique({ where: { id: root.id } }).games(),
}
export const addFriend = ({ input }) => {
return db.$transaction([
db.user.update({
where: {
id: input.id,
},
data: {
friends: {
connect: {
id: input.friendId,
},
},
},
}),
db.user.update({
where: {
id: input.friendId,
},
data: {
friends: {
connect: {
id: input.id,
},
},
},
}),
])
}
export const removeFriend = ({ input }) => {
return db.$transaction([
db.user.update({
where: {
id: input.id,
},
data: {
friends: {
disconnect: {
id: input.friendId,
},
},
},
}),
db.user.update({
where: {
id: input.friendId,
},
data: {
friends: {
disconnect: {
id: input.id,
},
},
},
}),
])
}
mutation {
addFriend(input: { id: 4, friendId: 3 }) {
id
}
}
mutation {
removeFriend(input: { id: 4, friendId: 3 }) {
id
}
}
query {
users {
id,
name,
email
friends {
id
name
}
friendOf {
id,
name
}
}
}
Ryan
08/12/2021, 6:39 AM