Ismail Arafa
09/23/2019, 1:51 PMtype User {
id: ID! @id
username: String! @unique
email: String! @unique
phoneNum: String @unique
firstName: String!
lastName: String!
password: String!
profilePicture: String
status: Status! @default(value: ACTIVE)
followers: [User!]!
following: [User!]!
createdAt: DateTime! @createdAt
updatedAt: DateTime! @updatedAt
}
type Following {
id: ID! @id
follower: User!
followed: User!
blocked: Boolean! @default(value: false)
createdAt: DateTime! @createdAt
updatedAt: DateTime! @updatedAt
}
When I run prisma deploy
, I get an error saying that the relations followers
, following
(on User
), follower
, followed
(on Following
) don't have names. If anyone could help me with figuring this issue out, I would be forever grateful as I have been struggling with this for a couple of weeks now. My current workaround is that I deleted the followers
and following
fields on User
and instead wrote resolvers for them as computed fields and named the followed
and follower
relations on `Following`:
// resolvers/User.ts
t.list.field("followers", {
type: "User",
resolve: async ({ id }, args, ctx) => {
const fragment = `
fragment FollowerUserInfo on User {
follower {
username
email
firstName
lastName
profilePicture
id
}
}
`;
const rows: User[] = await ctx.prisma.followings({ where: { AND: [{ followed: { id } }, { blocked: false } ] } }).$fragment(fragment);
const followers = rows.map((elm: any) => elm.follower);
return followers;
}
});
// datamodel.prisma
type Following {
id: ID! @id
follower: User! @relation(name: "Follower")
followed: User! @relation(name: "Followed")
blocked: Boolean! @default(value: false)
createdAt: DateTime! @createdAt
updatedAt: DateTime! @updatedAt
}
Moreover, I am using the following versions of Prisma-related dependencies:
"dependencies": {
"apollo-server-express": "^2.9.3",
"express": "^4.17.1",
"graphql": "^14.5.4",
"graphql-middleware": "^4.0.1",
"graphql-shield": "^6.1.0",
"nexus-prisma": "^0.3.8",
"prisma-client-lib": "^1.34.8"
},
"devDependencies": {
"@types/express": "^4.17.1",
"@types/graphql": "^14.5.0",
"@types/jest": "^24.0.18",
"@types/node": "^12.7.4",
"@typescript-eslint/parser": "^2.3.0",
"apollo-server-testing": "^2.9.3",
"jest": "^24.9.0",
"ts-node": "^8.3.0",
"ts-node-dev": "^1.0.0-pre.42",
"typescript": "^3.4.5"
}
Please let me know if this makes sense or not or if there's a more efficient way of doing things. Thanks!Spencer Olsen
09/23/2019, 2:04 PMRichard Ward
09/23/2019, 2:07 PMname
param. From the linked page:
author: User @relation(name: "StoriesByUser")
Ismail Arafa
09/23/2019, 2:58 PMRichard Ward
09/23/2019, 3:01 PMRichard Ward
09/23/2019, 3:01 PMIsmail Arafa
09/23/2019, 3:08 PMtype User {
id: ID! @id
username: String! @unique
email: String! @unique
phoneNum: String @unique
firstName: String!
lastName: String!
password: String!
profilePicture: String
status: Status! @default(value: ACTIVE)
followers: [User!]! @relation(name: "Following")
following: [User!]! @relation(name: "Following")
createdAt: DateTime! @createdAt
updatedAt: DateTime! @updatedAt
}
type Following {
id: ID! @id
follower: User! @relation(name: "Follower")
followed: User! @relation(name: "Followed")
blocked: Boolean! @default(value: false)
createdAt: DateTime! @createdAt
updatedAt: DateTime! @updatedAt
}
When I query a User to look up its followers
or following
field, the array is always empty 😞Josef Henryson
09/23/2019, 3:17 PMfollowers: [User!]! @relation(name: "Following")
following: [User!]! @relation(name: "Following")
Josef Henryson
09/23/2019, 3:20 PMfollowers: [Following!]! @relation(name: "Follower")
following: [Following!]! @relation(name: "Followed")
Ismail Arafa
09/23/2019, 3:24 PMJosef Henryson
09/23/2019, 3:25 PMJosef Henryson
09/23/2019, 3:26 PMIsmail Arafa
09/23/2019, 3:29 PMFollowing
type as well to have the createdAt
field.Josef Henryson
09/24/2019, 6:21 AM