mongodb - 嵌套查询在 GraphQL 中返回空值 - Next JS
问题描述
所以我是第一次尝试 GraphQL,我并没有真正的经验。我有一个产品模型,它有一个图像字段,它是一个包含字符串的对象数组。
我想我可以简单地创建一个图像输入并将其嵌入到产品输入中
但是,每当我查询产品时,图像字段为空。
我还尝试在数据库中手动创建产品文档,查询它也将图像显示为空,这让我确定问题出在我的解析器中。
我非常感谢您的帮助,因为已经有几个小时没有解决方案了。
架构
import { gql } from "apollo-server-micro";
export const typeDefs = gql`
type Image {
title: String
alt: String
url: String!
}
input ImageInput {
title: String
alt: String
url: String!
}
# Products
type Product {
id: ID
name: String!
slug: String!
price: Float!
stockCount: Int
shortDescription: String!
longDescription: String
images: [Image!]!
}
input ProductInput {
id: ID
name: String
slug: String
price: Float
stockCount: Int
shortDescription: String
longDescription: String
images: [ImageInput!]!
}
type Query {
getProducts: [Product]
getProduct(id: ID!): Product
}
type Mutation {
#Products
newProduct(input: ProductInput): Product
updateProduct(id: ID!, input: ProductInput): Product
deleteProduct(id: ID!): String
}
`;
解析器
const Product = require("../models/product");
export const resolvers = {
Query: {
// products
getProducts: async () => {
try {
const products = await Product.find({});
return products;
} catch (err) {
console.log(err);
}
},
getProduct: async (_, { id }) => {
const product = await Product.findById(id);
if (!product) {
throw new Error("Product not found");
}
return product;
},
},
Mutation: {
// products
newProduct: async (_, { input }) => {
try {
const product = new Product(input);
const result = await product.save();
return result;
} catch (err) {
console.log(err);
}
},
updateProduct: async (_, { id, input }) => {
let product = await Product.findById(id);
if (!product) {
throw new Error("Product not found");
}
product = await Product.findOneAndUpdate({ _id: id }, input, {
new: true,
});
return product;
},
deleteProduct: async (_, { id }) => {
const product = await Product.findById(id);
if (!product) {
throw new Error("Product not found");
}
await Product.findOneAndDelete({ _id: id });
return "Product deleted";
},
},
};
产品型号
import mongoose from "mongoose";
const { Schema } = mongoose;
mongoose.Promise = global.Promise;
const ProductSchema = new Schema(
{
name: {
type: String,
required: true,
trim: true,
},
slug: {
type: String,
required: true,
trim: true,
},
stockCount: {
type: Number,
required: true,
trim: true,
},
price: {
type: Number,
required: true,
trim: true,
},
shortDescription: {
type: String,
required: true,
trim: true,
},
longDescription: {
type: String,
required: false,
trim: true,
},
images: [
{
url: {
type: String,
required: true,
},
alt: {
type: String,
required: false,
},
title: {
type: String,
required: false,
},
},
],
},
{
timestamps: true,
}
);
module.exports =
mongoose.models.Product || mongoose.model("Product", ProductSchema);
解决方案
推荐阅读
- spring-boot - springboot中的Java stomp websocket客户端
- c# - 在初始 Twiml 提示完成之前调用的 Twilio asyncAMD 回调 url
- amazon-redshift - 如何在 Dbeaver 中隐藏不允许的导航器模式?
- c# - 如何保存与另一个轨道混合的 OffsetSampleProvider 的 wav 文件
- python - pandas - 根据多列获取最大组
- python - Django:拥有超过 60 个字段的模型好不好?以及开发项目的最佳方式
- python - 获取列表的所有连续段的最快方法
- node.js - 如果其他用户最近删除了它的父级,如何防止创建数据?
- python - PyQT 退出应用程序事件
- python - 是否有一个函数可以根据 Python 和 Flask 中输入值的变化更新过滤字典?