繁体   English   中英

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

[英]Resolver must be an object or function (Graphql Relationship)

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

错误显示: 在此处输入图片说明

产品解析器:

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

类型定义:

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!] 因为我们只想返回一个数组。

现在对于typedefs中的查询:

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 模型注意,我们使用父类是获取 mainCategory 字段的产品,它是一个对象 ID,它将返回给我们具有相同 ID 的类别

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

这是您的结果:我们首先查询产品

在此处输入图片说明

结果:

在此处输入图片说明

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

在此处输入图片说明

那么结果:

在此处输入图片说明

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM