Dan Borstelmann
11/06/2020, 1:35 AMexport const User = objectType({
name: 'User',
definition(t) {
t.model.id();
t.model.email();
t.model.handle();
},
});
export const Follow = objectType({
name: 'Follow',
definition(t) {
t.model.id();
t.model.createdAt();
t.model.follower(); // This is a user
t.model.following(); // This is a user
},
});
export const FollowQuery = extendType({
type: 'Query',
definition(t) {
t.list.field('followers', {
type: 'Follows',
resolve: async (_parent, args, context) => {
const authedId = getUserId(context);
let followers;
try {
followers = await context.prisma.follow.findMany({
where: { following: { id: authedId } },
select: {
id: true,
follower: {
// THIS SELECT IS IGNORED. <-------------
// User model is populated with all fields (including say the email we don't want to expose)
select: {
handle: true
},
},
},
});
} catch {
throw new Error();
}
return followers;
},
});
},
});
query followers {
followers {
id
follower {
handle
# if I ask for email here it will return the email even though it wasn't selected
}
}
}
Ryan
11/06/2020, 7:19 AMDan Borstelmann
11/06/2020, 7:59 AMquery followers {
followers {
id
createdAt
}
}
and this is a custom query like the above, if you don't select createdAt from prisma yourself from the follows table (and return from the query resolver that data) the api call will fail with Follow.createdAt is null or whatever. So it's not using the default model resolver for this, it expects you to do it (even though the return type is a prisma model). however the behavior is different on related fields, this is where it does automatically get the related model and fetch. I am curious why the discrepancy, and if there is any info on why/how this is implemented and how to work with itDan Borstelmann
11/06/2020, 8:00 AMiki
11/06/2020, 8:22 AMiki
11/06/2020, 8:27 AMfollower
iki
11/06/2020, 8:30 AMDan Borstelmann
11/06/2020, 8:34 AMiki
11/06/2020, 8:45 AM