【问题标题】:GraphQL nested query returning null fieldGraphQL 嵌套查询返回空字段
【发布时间】:2019-07-08 08:44:48
【问题描述】:

我正在尝试学习 GraphQL(& node.js & MongoDB 等)。我无法让这个简单的嵌套查询返回结果:

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

我只得到一级字段,例如:

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

这是我的架构:

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!
}

还有我的解析器:

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})
  };

知道我做错了什么吗?

【问题讨论】:

    标签: graphql


    【解决方案1】:

    您的语法非常适合从 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!
    }
    

    为了简单

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

    但你只有回报的人

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

    您应该返回服务数组对象以使用 People 获取该返回对象

    People: () => 返回类似下面的结果。

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

    如果您使用 mongoose,请使用 aggregation 从 mongoBD 获取数据。

    【讨论】:

    • 感谢您的回答。你能帮我实现聚合吗,我不明白。
    • 非常抱歉,您所说的“人和服务的模态”是什么意思?
    • 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);
    • 对于聚合或填充,您必须参考建立关系 serviceId: { type: Schema.Types.ObjectId, ref: 'service' },
    【解决方案2】:

    您需要在服务中传递与您的猫鼬模型相同的位置对象的引用(我假设您使用的是猫鼬)。传递引用后,您可以在服务查询Refer this中添加 populate(refernce).exec()

    【讨论】:

    • 谢谢,但作为一个菜鸟,我会问:你能发布一个代码示例吗?
    猜你喜欢
    • 2019-07-18
    • 2020-01-15
    • 2020-02-25
    • 2019-12-20
    • 2019-08-12
    • 1970-01-01
    • 2017-10-01
    • 2021-01-29
    相关资源
    最近更新 更多