Implementing the resolvers 🧑💻
Build out the resolvers for our schema and wrap up the completion of our back-end.
When defining our resolvers, they're just a one-to-one mapping between our schema and our data.
So for example, if we have a User type, we can just return a User object from our resolver.
const resolvers = {
Query: {
user: async (parent, args, context, info) => {
return await context.prisma.user.findUnique({ where: { id: args.id } })
},
},
}These are similar to controllers in a traditional REST API. They'll contain all the business logic for our API.
We can also define relationships between entities. For example we can fetch all of the jobs a user has applied for, which would be a User -> Job relationship.
const resolvers = {
User: {
jobs: async (parent, args, context, info) => {
return await context.prisma.job.findMany({ where: { userId: parent.id } })
},
},
}As with our schema, we only define the direct relationships between entities. So inside the User resolvers, the only top-level keys we would define are Query, Mutation, Subscription and User.