GraphQL查询始终返回NULL

时间:2019-07-07 07:31:15

标签: node.js mongodb mongoose graphql

尽管数据库中有数据,但我的查询始终返回Null!这是代码,下面是我通过MONGODB Atlas查询数据时返回的错误...是模型中的问题还是什么?

'use strict';

const Mongoose = require('mongoose');

Mongoose.connect("mongodb+srv://admin:xxxxxx-XXXdx.mongodb.net/test?retryWrites=true&w=majority");

const {
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLString,
    GraphQLInt,
    GraphQLList,
    GraphQLNonNull,
    GraphQLBoolean
} = require('graphql');

const viewProduct=Mongoose.model("viewProduct",{
    name:String,
    open:Number,
    high:Number,
    low:Number,
    close:Number
})


const productType = new GraphQLObjectType({
    name: 'Product',
    fields: {

        name: { type: new GraphQLNonNull(GraphQLString) },
        close: { type: new GraphQLNonNull(GraphQLInt) },

    }
});


const schema = new GraphQLSchema({
    query: new GraphQLObjectType({
        name: 'Query',
        fields: {
            viewProduct: {
                type: productType,
                resolve: (root, args, context, info) => {
                return viewProduct.findOne({name:"IBM"}).exec();
            }
            },
        }
    }),
});

module.exports = schema;

错误:它说不能将null作为值返回,因为我将变量声明为NonNullable,因此返回的值为Null ...

{
  "statusCode": 200,
  "body": {
    "errors": [
      {
        "message": "Cannot return null for non-nullable field Product.close.",
        "locations": [
          {
            "line": 4,
            "column": 9
          }
        ],
        "path": [
          "viewProduct",
          "close"
        ]
      }
    ],
    "data": {
      "viewProduct": null
    }
  }
}

0 个答案:

没有答案