解析器必须是对象或函数Graphql 关系

问题描述

我正在尝试在类别和产品之间建立一对多的关系,但是在我放置解析器后出现错误,这是我的参考代码

错误显示

enter image description here

产品解析器:

 async getProductByCategory(mainCategory,{}) {
      try {
        const product = Product.find({ mainCategory: mainCategory._id });
        if (product) {
          return product;
        } else {
          throw new Error("Product Not Found");
        }
      } catch (err) {
        throw new Error(err);
      }
    },

MainCategory 解析器:

async getCategoryByProduct(product,args) {
      try {
        const mainCategory = MainCategory.findById(product._id);
        if (mainCategory) {
          return mainCategory;
        } else {
          throw new Error("Main Category Not Found");
        }
      } catch (err) {
        throw new Error(err);
      }
    },

主解析器:

const productsResolvers = require("./products");
const mainCategoriesResolvers = require("./mainCategories");


module.exports = {
    Product: {
      mainCategory: productsResolvers.getProductByCategory,},MainCategory: {
      products: mainCategoriesResolvers.getCategoryByProduct,Query: {
      ...productsResolvers.Query,...mainCategoriesResolvers.Query,Mutation: {
      ...productsResolvers.Mutation,...mainCategoriesResolvers.Mutation,};

typeDefs:

type Product {
    id: ID! 
    mainCategory: MainCategory
  }

 type MainCategory {
    id: ID!
    products: [Product]
  }

如果您需要更多代码参考,请告诉我,以便我可以给您。提前致谢

解决方法

大家好,我已经为那些想了解 graphql 中一对多关系的人解决了这个问题,这是我做的代码。

产品型号:

const { model,Schema } = require("mongoose");

const { ObjectId } = Schema.Types;

const productSchema = new Schema({
  name: String,mainCategory: {
    type: ObjectId,ref: "MainCategory",},});

module.exports = model("Product",productSchema);

我们只想让 mainCategory 字段或数据成为一个对象,因为我们只希望每个产品都有一个类别,您在那里看到的 ref 应该与您在此处命名的模型完全相同:

module.exports = model("MainCategory",mainCategorySchema);

对于 MainCategory 模型:

const { model,Schema } = require("mongoose");

const { ObjectId } = Schema.Types;

const mainCategorySchema = new Schema({
  products: [
    {
      type: ObjectId,ref: "Product",],});

module.exports = model("MainCategory",mainCategorySchema);

既然你分类对了吗? category 可以有多个产品,因此我们将其设为一个数组,然后像 Product 一样,我们应该使 ref 与我们命名模型时相似。

现在是 TypeDefs:

type Product {
    id: ID!
    mainCategory: MainCategory!
  }

我们仅将其设为 MainCategory!因为我们只想返回一个对象。

type MainCategory {
    id: ID!
    name: String!
    products: [Product!]
  }

对于 MainCategory 类型,我们像这样 [Product!] 因为我们只想返回一个数组。

现在用于 typedef 中的查询:

type Query {
 getProducts: [Product]
 getMainCategories: [MainCategory]
}

这将获得产品和主类别的所有数据

因为我们需要改变数据或换句话说创建它,我们可以这样做:

type Mutation {
    createProduct(
      name:String!
      mainCategory: String!
    ): Product!
     createMainCategory(
      name: String!
    ): MainCategory!
}

正如你现在问的那样?为什么 mainCategory 是一个字符串?为什么?因为我们要发送一个对象 id 或类别的 id。

正如您所注意到的,我们没有产品 createMainCategory,因为我们将在创建产品时获取该 ID,稍后我将对此进行解释。

然后我们转到这里的解析器:

产品解析器:

const Product = require("../../models/Product");

module.exports = {
  Query: {
    async getProducts() {
      try {
        const products = await Product.find().sort({ createdAt: -1 });
        return products;
      } catch (err) {
        throw new Error(err);
      }
    },Mutation: {
    async createProduct(_,{ mainCategory },context) {
      const newProduct = new Product({
        mainCategory,});

      const product = await newProduct.save();

      return product;
    },};

MainCategory 解析器:

const MainCategory = require("../../models/MainCategories");

module.exports = {
  Query: {
    async getMainCategories() {
      try {
        const mainCategories = await MainCategory.find().sort({
          createdAt: -1,});
        return mainCategories;
      } catch (err) {
        throw new Error(err);
      }
    },Mutation: {
    async createMainCategory(_,{ name },context) {
      const newMainCategory = new MainCategory({
        name,});

      const mainCategory = await newMainCategory.save();

      return mainCategory;
    },};

最后是 index.js:

Product: {
    mainCategory: (parent) => 
    MainCategory.findById(parent.mainCategory),MainCategory: {
    products: (parent) => Product.find({ mainCategory: parent._id 
}),

我们在这里定义我们使用了我使用 parent 的原因所以我不会混淆知道它是产品还是 maincategory 所以在产品中你必须定义在这种情况下我们使用 mainCategory 字段,我们使用 MainCategory模型注意,我们使用作为产品的父级来获取作为对象 id 的 mainCategory 字段,它将返回给我们具有相同 id 的类别

为了让 mainCategory 获得产品数组,我们使用 Product 模型注意并找到相同的 mainCategory 和父 id,即 mainCategory Id,它将返回产品数组>

这是你的结果: 我们先查询Products

enter image description here

结果:

enter image description here

同样适用于 mainCategory 我们首先查询 getMainCategories

enter image description here

那么结果:

enter image description here

请注意,我建立了一对多关系 如果您对此有任何错误,请告诉我,我经常在 stackoverflow 上很活跃,但我可能会迟到回复,但我会在看到通知后立即回复。