1
votes

Graphql query returns id null (in frontend) after mongodb aggregation, but if I print a console.log in the resolvers aggregation, _id is not null. I don't know why in the backend I have a id and in front is null. Anybody can help me? Thanks

Client Schema

const ClientSchema = mongoose.Schema({
  surname: {
    type: String,
    require: true,
    trim: true
  },
  company: {
    type: String,
    require: true,
    trim: true
  },
  salesman: {
    type: mongoose.Schema.Types.ObjectId, 
    require: true,
    ref: 'User'
  }
})
module.exports = mongoose.model('Client', ClientSchema)

Order Schema

const OrderSchema = mongoose.Schema({
  order: {
    type: Array,
    require: true,
  },
  total: {
    type: Number,
    require: true,
  },
  client: {
    type: mongoose.Schema.Types.ObjectId, 
    require: true,
    ref: 'Client'
  },
  salesman: {
    type: mongoose.Schema.Types.ObjectId, 
    require: true,
    ref: 'User'
  },
  stage: {
    type: String,
    default: "PENDENT"
  },
 })
module.exports = mongoose.model('Order', OrderSchema)

Resolver

getOrdersBySalesman: async (_, { }, ctx) => {
      const mongoObjectId = mongoose.Types.ObjectId(ctx.user.id);
      try {
        const orders = await Order.aggregate([
          { $match: { salesman: mongoObjectId }},
          { $lookup: {
              localField: "client",
              foreignField: "_id",
              from: "clients",
              as: "client"
          }},
          { $unwind: '$client' },
          { $sort: { 'client.company': 1 }}
        ]);
        console.log(orders);
        return orders;
      } catch (error) {
        console.log(error);
      }
    }

Graphql query

const GET_ORDERS = gql`
query getOrdersBySalesman {
  getOrdersBySalesman {
    id
    order{
      name
      quantity
    }
    total
    client {
      id
      surname
      company
    }
    stage
  }
}`

Result console.log resolver _id is fine

 { _id: 5ee0da703b683e071c0adfe2,
    order: [ [Object] ],
    stage: 'PENDENT',
    total: 6166.65,
    client:
     { _id: 5ea813b3085b4417b8627557,
       surname: 'NOU2',
       company: 'NN',
       salesman: 5ea568905d47ed2760e8d11c,
       __v: 0 },
    salesman: 5ea568905d47ed2760e8d11c,
    __v: 0 } ]

Result in frontend after graphql query id: null

 {  id: null,
    order: [{…}]
    stage: 'PENDENT',
    total: 6166.65,
    client:
     { id: null,
       surname: 'NOU2',
       company: 'NN',
     }
  }  
1
id and _id are different fields? - D. SM
id and _id are the same fields. id is in the frontend (graphql query) and _id is in backend (mongodb). There are 2 id, one is order's id and the second is client's id - ccnat
You seem to assume that one is automatically translated to the other. I suggest verifying that this is the case and updating your question with some sort of documentation references demonstrating this expectation. - D. SM
medium.com/the-ideal-system/… - originally it is even an object, not a string - xadm

1 Answers

0
votes

Use type ID for your id in GraphQL Schema as long as you are returning ObjectID and it should work just fine.

type Order {
  id: ID
  ...
}