Expandable types are complex to implement and prone to making mistakes during usage.
For now, the refactor the client make multiple queries for fetching related data.
Otherwise, populate the data every time irrespective of request, to favor development speed.
If request based population is wanted, this is an example. The caveat of this compared to expandable types is that this does populate recursively but I feel its not required.
type User {
id: ID!
name: String!
email: String!
posts: [Post!]!
}
type Post {
id: ID!
title: String!
content: String!
author: User!
}
import graphqlFields from 'graphql-fields' // this import may be slightly incorrect, do verify when implementing
Query: {
users: async (parent, args, context, info) => {
const fields = graphqlFields(info);
const userFields = fields.User;
// Only populate posts if it was requested in the query.
if (userFields.posts) {
return await User.find().populate('posts');
} else {
return await User.find();
}
}
Expandable types are complex to implement and prone to making mistakes during usage. For now, the refactor the client make multiple queries for fetching related data. Otherwise, populate the data every time irrespective of request, to favor development speed.
If request based population is wanted, this is an example. The caveat of this compared to expandable types is that this does populate recursively but I feel its not required.