0

I'm trying to learn GraphQL (& node.js & MongoDB etc.). I cant get this simple nested query to return results :

query getLocationByPerson {
   People {
        firstName
        lastName
        service {
        location
      }
   }
}

I only get the first level fields, like:

{
  "data": {
    "People": [
      {
        "firstName": "xxx",
        "lastName": "XXXXX",
        "service": null
      }
}

Here's my schema:

type Query {
  People: [PeopleObject]!
  PeopleByName(lastName: String!): [PeopleObject]
  PeopleByID(id:ID!): [PeopleObject]
  Service: [ServiceObject]
  ServiceByID(id:ID!): [ServiceObject]
}
type PeopleObject {
    id: ID!
    Xid: String!
    firstName: String!
    lastName: String!
    email: String!
    serviceId: String
    apps: [String]
    service: [ServiceObject]
}
type ServiceObject {
    id: ID!
    name: String!
    location: String!
}

And my resolver:

const queries = {
    People: () => People.find({}),
    PeopleByName: (root,args,context,info) => People.find({lastName: args.lastName}),
    PeopleByID: (root,args,context,info) => People.find({_id: args.id}),
    Service: () => Service.find({}),
    ServiceByID: (root,args,context,info) => Service.find({_id: args.id})
  };

Any idea what am I doing wrong?

2 Answers 2

0

You need to pass the reference for the location object in service which is same as you mongoose model(i assume you are using mongoose). After passing the reference you can add populate(refernce).exec() in your service query Refer this

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, but as a total noob I would ask : can you please post a code example?
0

Your syntax are dame good for getting any data from graphql

type Query {
  People: [PeopleObject]!
}
type PeopleObject {
    id: ID!
    Xid: String!
    firstName: String!
    lastName: String!
    email: String!
    serviceId: String
    apps: [String]
    service: [ServiceObject]
}
type ServiceObject {
    id: ID!
    name: String!
    location: String!
}

for Simplicity

type Query {
  People: [{
    id: ID!
    Xid: String!
    firstName: String!
    lastName: String!
    email: String!
    serviceId: String
    apps: [String]
    service: [{
        id: ID!
        name: String!
        location: String!
    }]
}]!
}

But you have return only people

People: () => People.find({}),

You should return service array object to get that return object with People

People: () => return something Like below result.

[{
    id: '5c5ac87e5e4d85ae77de9c50'
    Xid: 'xx'
    firstName: 'yyy'
    lastName: 'zzz'
    email: '[email protected]'
    serviceId: '5c5ac87e5e4d85ae77de9c51'
    apps: ['testing', 'google']
    service: [{
        id: 5c5ac78d5e4d85ae77de9c4f,
        name: 'Tester',
        location: 'US America',
    }, {}]
}, {}]

If you using mongoose use aggregation to get data from mongoBD.

10 Comments

thanks for your answer. Can you help me to implement aggregation, I dont get it.
I'm so sorry, what do you mean by "modal of person and service"?
people.js: const mongoose = require('mongoose'); const Schema = mongoose.Schema; const peopleSchema = new Schema({ Xid: { type: String }, firstName: { type: String }, lastName: { type: String }, email: { type: String }, apps: { type: Array }, serviceId: { type: String } },{ versionKey: false }) module.exports = mongoose.model('people', peopleSchema);
service.js const mongoose = require('mongoose'); const Schema = mongoose.Schema; const serviceSchema = new Schema({ name: { type: String }, location: { type: String } },{ versionKey: false }) module.exports = mongoose.model('service', serviceSchema);
For agregation or populate you must have reference to make relationship serviceId: { type: Schema.Types.ObjectId, ref: 'service' },
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.