3

How to query User object with UserType using graphql with mongoose?

I have looked in Ahmad Ferdous answer but I could not figure out how to do with different collection..

export const UserType = mongoose.model('User-Type', new mongoose.Schema({
    typeName: { type: String }
}));

export const User = mongoose.model('User', new mongoose.Schema({
    name: { type: String },
    type: { type: mongoose.Schema.Types.ObjectId, ref: 'User-Type' },
  })
);

const Users = new GraphQLObjectType({
  name: 'Users',
  fields: () => ({
    id: { type: GraphQLID },
    name: { type: GraphQLString },
    type: { type: /* HOW TO CONNECT TO USER TYPE? */ },
  }),
});

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    users: {
      type: new GraphQLList(Users),
      async resolve(parent, args) {
        return await User.find({});
      },
    },
});

export const Schema = new GraphQLSchema({
  query: RootQuery,
});

In mongoose to resolve User with UserType I does:

 const users = await User.find({})
    .populate({ path: 'type', model: 'User-Type' })

UPDATE

This question is not duplicate. Why? I want to query those fields id, name from User. this is working well by this code:

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    users: {
      type: new GraphQLList(Users),
      async resolve(parent, args) {
        return await User.find({});
      },
    },
});

But when query with type field then I should do:

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    users: {
      type: new GraphQLList(Users),
      async resolve(parent, args) {
        return await User.find({}).populate({ path: 'type', model: 'User-Type' });
      },
    },
});

This is seem so much wrong with graphql because if I just want id, name the mongoose also ask for type ( return await User.find({}).**populate**({ path: 'type', model: 'User-Type' });) that I did not ask!

So I think I should do:

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    onlyUsers: {
      type: new GraphQLList(Users),
      async resolve(parent, args) {
        return await User.find({});
      },
    },
    UsersWithType: {
      type: new GraphQLList(Users),
      async resolve(parent, args) {
        return await User.find({}).populate({ path: 'type', model: 'User-Type' });
      },
    },
});

This is how do things in graphql??

I want to query User and get from the db only the fields I ask for.

If graphql doesn't do that that is power of using it? I can also use lodash for get the fields I want from the object.. _.get(obj, 'somefield.anothersomefield')

Jon Sud
  • 10,211
  • 17
  • 76
  • 174
  • 1
    That's actually `.populate('type')` because that's what the field is called. There's nothing "graphql specific" about this as it's still using the mongoose methods. – Neil Lunn Mar 24 '19 at 07:47
  • corrent. I update my question. the question still remain – Jon Sud Mar 24 '19 at 07:48
  • 1
    How? Have you tried it? I just told you it's **exactly the same**. – Neil Lunn Mar 24 '19 at 07:52
  • So, instead of `return await User.find({});` I need to change it to `return await User.find({}).populate('type')`? – Jon Sud Mar 24 '19 at 07:54
  • 1
    That would be how you use mongoose populate. There are only so many ways I can keep saying **it's the same**. Your problem was you used the wrong argument initially. `populate()` accepts the "path to populate" as a single string argument. Optionally an object like `{ path: 'type', model: 'User-Type' }`, but only when you really need those options. Bottom line is that `.populate("UserType")` as you originally asked the question was the incorrect argument. – Neil Lunn Mar 24 '19 at 07:56
  • The problem with that is when I want to get the `id, name` from the User object then mongodb does query to bring not only `id, name` but also `type` that I did not ask for! Sometimes I want type and sometimes I don't. – Jon Sud Mar 24 '19 at 07:59
  • Based on your edit and comments, it sounds like your question really is "how do I know which fields have been requested", which has been asked and answered [here](https://stackoverflow.com/questions/48004805/is-there-a-tool-to-parse-graphql-query-to-get-requested-fields) and [here](https://stackoverflow.com/questions/54984035/how-to-know-which-child-resolvers-are-needed-in-apollo-graphql-query/54988767#54988767). – Daniel Rearden Mar 24 '19 at 13:47

0 Answers0