【问题标题】:Get all data from subdocument via graphql通过 graphql 从子文档中获取所有数据
【发布时间】:2021-02-17 21:52:29
【问题描述】:

型号

const mongoose=require('mongoose');

const CustomerGeneralInformation = require('./CustomerGeneralInformation').schema;
const CustomerContact = require('./CustomerContact').schema;
const CustomerFinance=require('./CustomerFinancial').schema;
const CustomerPersonalData=require('./CustomerPersonalData').schema;
const CustomerIdentification=require('./CustomerIdentification').schema;


const Schema=mongoose.Schema;


const CustomerSchema=new Schema({
  

   generalInformation: [CustomerGeneralInformation],
    
    contactData: [CustomerContact],
    financialData:[CustomerFinance],
    personalData:[CustomerPersonalData],
    identificationData:[CustomerIdentification]
});

module.exports=mongoose.model('Customer',CustomerSchema);

Graphql 架构

const RootQuery=new GraphQLObjectType({
    name:'RootQueryType',
    fields:{
        customers:{
            type: new GraphQLList(CustomerType),
            resolve(parent,args){
             
            return customer.find({});
            
            }
        }
    }
});

GraphiQl

{
  customers
  {
    id
   generalInformation{
    purposeOfBusiness
  }
  contactData{
    phone
    email
    countryResidence
    mailbox
    houseNo
    zip
    city
  }
    financialData{
      taxNo
      countryTaxation
      refBankIban
      StringrefBankBic
      refBankIban
    }
   
  }
}

结果

{
  "data": {
    "customers": [
      {
        "id": "5fa0f8ea4e028a2cf8d24c3f",
        "generalInformation": {
          "purposeOfBusiness": null
        },
        "contactData": {
          "phone": null,
          "email": null,
          "countryResidence": null,
          "mailbox": null,
          "houseNo": null,
          "zip": null,
          "city": null
        },
        "financialData": {
          "taxNo": null,
          "countryTaxation": null,
          "refBankIban": null,
          "StringrefBankBic": null
        }
      }
    ]
  }
}

所以我创建了一个客户模型,它有一个 id(由 mongodb 自动生成)和 5 个其他模式类型字段。 我已经使用 graphql 来创建 api。

一切正常,我已经创建了添加数据的突变,所有测试都在 mongo 中进行了数据保存。

但是,如果您检查我的 graphql 输出,我无法查询子文档,它显示为 null。我想要所有包含数据的子文档的列表。

请帮忙

【问题讨论】:

    标签: node.js mongodb graphql


    【解决方案1】:

    我已经尝试过这种方式,并且效果很好,

    首先,当您在新的 Customer 模型中传递整个模型时,我只是将 ID 存储为参考。

        const CustomerSchema = new mongoose.Schema(
      {
        generalInformation: {
          type: mongoose.Schema.Types.ObjectId,
          ref: "GeneralInfo"
        },
        contactData: { type: mongoose.Schema.Types.ObjectId, ref: "ContactData" },
        financialData: {
          type: mongoose.Schema.Types.ObjectId,
          ref: "FinacialData"
        },
        personalData: { type: mongoose.Schema.Types.ObjectId, ref: "PersonalData" }
      },
      {
        timestamps: true
      }
    );
    

    在那之后,对于 CustomerType,我已经这样做了

    const CustomerType = new GraphQLObjectType({
      name: "Customer",
      fields: () => ({
        id: { type: GraphQLID },
        customerContact: {
          type: CustomerContactType,
          resolve(parent, args) {
            return CustomerContact.findById(parent.contactData);
          }
        },
        generalInformation: {
          type: CustomerGeneralType,
          resolve(parent, args) {
            return CustomerGeneral.findById(parent.generalInformation);
          }
        },
        financialData: {
          type: CustomerFinanceType,
          resolve(parent, args) {
            return CustomerFinance.findById(parent.financialData);
          }
        },
        personalData: {
          type: CustomerPersonalType,
          resolve(parent, args) {
            return CustomerPersonal.findById(parent.personalData);
          }
        }
      })
    });
    

    它们用于 RootQuery

    customers: {
          type: new GraphQLList(CustomerType),
          resolve(parent, args) {
            return Customar.find({});
          }
        }
    

    这是我的查询:

    {
      customers{
        id
        generalInformation{
          purposeOfBusiness
        }
        customerContact{
          phone
          email
          countryResidence
        }
        financialData{
          taxNo
        }
      }
    }
    

    然后输出

    {
      "data": {
        "customers": [
          {
            "id": "5fa3fe066f0fda0568e56456",
            "generalInformation": {
              "purposeOfBusiness": "Test Purpose"
            },
            "customerContact": {
              "phone": "123456798",
              "email": "test@demo.com",
              "countryResidence": "Pak"
            },
            "financialData": {
              "taxNo": "120033244"
            }
          }
        ]
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-21
      • 1970-01-01
      • 2018-11-11
      • 2021-11-05
      • 2020-12-11
      • 2020-01-20
      • 2021-01-12
      • 2022-07-16
      • 2021-03-01
      相关资源
      最近更新 更多