Node.js Graphql mongodb无法返回\u id

Node.js Graphql mongodb无法返回\u id,node.js,mongodb,graphql,Node.js,Mongodb,Graphql,大家好,我正在用graphql和mongodb创建一个api。我正在使用mongodb聚合过滤我的文档 export const findAllVariants = async (_, { orderby, filter, skip, sort, perPage }, context) => { await jwtAuthentication.verifyTokenMiddleware(context); try { const aggregate = Variant.a

大家好,我正在用graphql和mongodb创建一个api。我正在使用mongodb聚合过滤我的文档

export const findAllVariants = async (_, { orderby, filter, skip, sort, perPage }, context) => {
  await jwtAuthentication.verifyTokenMiddleware(context);

  try {
    const aggregate = Variant.aggregate();
    const match = { $and: [] };

    if (filter) {
      await filter.split(' ').forEach((f) => {
        match.$and.push({
          $or: [
            {
              color: {
                $regex: new RegExp(transliterate(f), 'i')
              }
            },
            {
              size: {
                $regex: new RegExp(transliterate(f), 'i')
              }
            },
            {
              sku: {
                $regex: new RegExp(transliterate(f), 'i')
              }
            },
            {
              barcode: {
                $regex: new RegExp(transliterate(f), 'i')
              }
            }
          ]
        });
      });
      aggregate.match(match);
    }

    const options = {
      allowDiskUse: true,
      limit: perPage,
      page: skip,
      sortBy: { [orderby]: sort }
    };

    return Variant.aggregatePaginate(aggregate, options, (err, results, pageCount, count) => {
      if (err) {
        return new ApolloError(`There was an error ${err}`);
      }
      console.log(results);
      return {
        count,
        variants: results,
        pageCount,
        skip
      };
    });
  } catch (err) {
    return new ApolloError(`There was an error ${err}`);
  }
};
我的图形定义是这样的

export default gql`
  type VariantsResult {
    variants: [Variant]
    count: Int
    pageCount: Int
    skip: Int
  }

  type Variant {
    id: String!
    color: String
    size: String
    quantity: Int
    sku: String
    barcode: String
    price: Price
    images: [String]
  }

  input VariantInfo {
    id: ID!
    color: String!
    size: String!
    quantity: Int
    sku: String!
    barcode: String!
    price: InputPrice!
    images: [ImageInfo]!
  }

  extend type Query {
    findAllVariants(orderby: Int, filter: String, skip: Int, sort: Int, perPage: Int): VariantsResult
  }

  extend type Mutation {
    createVariant(variantInfo: VariantInfo): Variant!
    removeVariantFromProduct(variantInfo: VariantInfo, productInfo: ProductInfo): Product!
    addVariantToProduct(variantInfo: VariantInfo, productInfo: ProductInfo): Product!
    editVariantFromProduct(variantInfo: VariantInfo): Variant!
  }
`;
现在在阿波罗游乐场,当我提供所需的数据并返回id值时,它会显示以下消息“对于不可为null的字段Variant.id,不能返回null。”

这只发生在id字段中,其他所有字段都可以正常工作。 请帮忙

错误: 身份证!表示该字段不可为空():

在您的情况下,
Variant.id
为空(抛出错误)

在GraphQL中使用可空性

可能的解决办法: Mongodb使用
\u id
而不是
id

类型可以是:

type Player{
  _id: String
  name: String
  salary: String
}
按id获取数据可能如下所示():

或在:

Sum:从简单的id查询开始(在进入聚合之前),检查返回值是否为null(或否)。然后在_idfeild解析器中使用正确的查询

相关问题:

相关教程:

type Player{
  _id: String
  name: String
  salary: String
}
db.collection('baseballPlayers').findOne({ _id: ObjectID("5faeda2c7e348a5e142827ae") });