projecthuman-repo / Coquest-WebApp

https://base-map-workspace.uc.r.appspot.com/
Other
0 stars 5 forks source link

Remove expandable types from both frontend and backend #131

Open pranav-suri-phc opened 2 months ago

pranav-suri-phc commented 2 months ago

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();
      }
    }
pranav-suri-phc commented 1 month ago

Mostly done, needs some cleanup though