I am figuring my way around creating a GraphQL API using MongoDB and I am trying to get my head around interfaces.
I get the idea behind it and watched a bunch of videos on talks and howto's, but the one thing I am seeing is how to make the query resolver.
It is always displayed like this: groups: () => { ... }
groups
would be listed in the query type, so it would need the mongodb query.
This is the resolver that I need to find the answer for:
What goes inside of the {...}
?
Query: {
groups: () => { ... },
families: () => Family.find(),
members: () => Member.find(),
},
I think the area I am stuck in when it comes to the query is that: "What would the query be since groups
is not a mongodb document?"
MORE INFORMATION:
Here is the full typedef
export const typeDefs = gql`
interface Group {
id: ID!
name: String!
}
type Family implements Group {
id: ID!
name: String! # persons family name
numberOfPeople: Int
}
type Member implements Group {
id: ID!
name: String! # persons first name
age: Int!
}
type Query {
groups: [Group]
families: [Family]
members: [Member]
}
}
Here is the resolver
export const resolvers = {
Group: {
__resolveType(group, context, info){
if(group.family){
return 'Family';
}
if(group.member){
return 'Member';
}
return null;
},
},
Query: {
groups: () => { ... },
families: () => Family.find(),
members: () => Member.find(),
}
}
The idea is that Family
and Member
are separate documents which hold the data and that Group
is the interface to create a query that combines them.