Javascript Graphql变异返回null,但数据库已更新

Javascript Graphql变异返回null,但数据库已更新,javascript,node.js,graphql,Javascript,Node.js,Graphql,我正在尝试添加一个变体,以允许客户端将文档添加到LineItem架构中。我在下面编写的代码允许我在使用GraphiQL测试它时这样做,但得到的响应为null。如何修复代码,使响应成为新文档 addLineItem: { type: LineItemType, args: { orderId: {type: new GraphQLNonNull(GraphQLID)}, productId: {type: new GraphQLNonNull(GraphQL

我正在尝试添加一个变体,以允许客户端将文档添加到LineItem架构中。我在下面编写的代码允许我在使用GraphiQL测试它时这样做,但得到的响应为null。如何修复代码,使响应成为新文档

addLineItem: {
    type: LineItemType,
    args: {
      orderId: {type: new GraphQLNonNull(GraphQLID)},
      productId: {type: new GraphQLNonNull(GraphQLID)},
      quantity: {type: new GraphQLNonNull(GraphQLInt)}
    },
    resolve(parent, args) {
      Product.findById(args.productId, (err, result) => {
        let price = result.price;
        let subtotal = price*args.quantity;
        let lineitem = new LineItem({
          orderId : args.orderId,
          productId : args.productId,
          quantity : args.quantity,
          subtotal: subtotal
        });
        return lineitem.save();
    }}
  }
},

实际上,你的代码没有问题

您需要将返回值指定为类型
(例如布尔值、字符串等)
。类型可以为null,例如:值可以为null,事实上,它们在默认情况下可以为null,除非您使用


因此,空返回值没有问题。

实际上,您的代码没有问题

您需要将返回值指定为类型
(例如布尔值、字符串等)
。类型可以为null,例如:值可以为null,事实上,它们在默认情况下可以为null,除非您使用


因此,null返回值没有问题。

问题是在
resolve
函数的
lineitem.save()中没有返回任何值
返回
回调中的值

使resolve function
async
,删除
findById
回调并等待结果,然后实现逻辑并返回值,如下所示:

async resolve(parent, args) {
  const result = await Product.findById(args.productId);

  let price = result.price;
  let subtotal = price*args.quantity;
  let lineitem = new LineItem({
    orderId : args.orderId,
    productId : args.productId,
    quantity : args.quantity,
    subtotal: subtotal
  });

  return lineitem.save();
}

问题在于,
line item.save()函数的
resolve
中没有返回任何值
返回
回调中的值

使resolve function
async
,删除
findById
回调并等待结果,然后实现逻辑并返回值,如下所示:

async resolve(parent, args) {
  const result = await Product.findById(args.productId);

  let price = result.price;
  let subtotal = price*args.quantity;
  let lineitem = new LineItem({
    orderId : args.orderId,
    productId : args.productId,
    quantity : args.quantity,
    subtotal: subtotal
  });

  return lineitem.save();
}