首页 > 解决方案 > 嵌套查询在 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);

标签: mongodbmongoosegraphqlnext.js

解决方案


推荐阅读