查询SQL Server表时,Apollo GraphQL返回null

时间:2017-03-24 22:46:45

标签: graphql graphql-js

我在SQL Server数据库中有这个表:表名是 small_customer

customerid| first_name | last_name | starting_date |
--------- | -----------| --------- | --------------|

这是 CustomerModel.js 中的代码,我连接到SQL Server数据库和Sequelize类型:

const CustomerModel= Conn.define('small_customer',{
    customerid: {type: Sequelize.INTEGER},
    first_name: {type: Sequelize.STRING},
    last_name: {type: Sequelize.STRING},
    starting_date: {type:Sequelize.DATEONLY},
});
const Customer= Conn.models.small_customers;
export default Customer;

这是 Query.js

import { CustomerModel, CustomerType, CustomerArgs} from './models/customer';
export default new GraphQLObjectType({
  name: 'Query',
  fields: () => {
    return {
      customer : {
        type: CustomerType,
        args: CustomerArgs,
        resolve(root,args){
          return new Promise((resolve, reject) => {
           return resolve(Customer.find({ where: args }));
            }); } }}}});

我已定义 CustomerType.js

export default new GraphQLObjectType({
  name: 'Customer',
  fields: () => {
    return {
      customerid: {type:GraphQLInt},
      first_name: {type: GraphQLString},
      last_name : {type: GraphQLString},
      starting_date: {type: GraphQLDate},
    }}});

当我编写查询以便根据customerid和firstname选择客户数据时,我有这个错误

  

它无法读取未定义的属性'find'

这是我的查询示例和GraphiQL中的结果:

enter image description here

1 个答案:

答案 0 :(得分:1)

  

Cannot read property 'find' of undefined

表示客户具有undefined类型,并且您正在find上执行undefined操作Customer.find({ where: args })

CustomerModel

中导入Query.js
import { Customer } from './models/CustomerModel';

// i haven't used babel so require, if only schema is defined in CustomerModel
// const Customer = require('./models/CustomerModel');

import { CustomerModel, CustomerType, CustomerArgs } from './models/customer';

export default new GraphQLObjectType({
    name: 'Query',
    fields: () => {
        return {
            customer: {
                type: CustomerType,
                args: CustomerArgs,
                resolve(root, args) {
                    return new Promise((resolve, reject) => {
                        return resolve(Customer.find({ where: args }));
                    });
                }
            }
        }
    }
});