MySQL日期在GraphQL查询中返回Null

时间:2019-05-29 23:54:01

标签: mysql node.js express graphql

我正在使用以下代码创建GraphQL API。在这种情况下,对于MySQL Date,我会得到null。我正在为此使用graphql-iso-date库。

我认为这是由于graphql-iso-date和MySQL Date的某些格式不匹配。下面是我从RowDataPacket打印到达日期到控制台的格式。

arival_date: 2019-02-28T18:30:00.000Z,

我得到的输出是:

{
  "data": {
    "tours": [
      {
        "id": "1",
        "name": "NEJC_20190301_SVN_000001",
        "pax_count": 12,
        "arrival_date": null
      }
    ]
  }
}

查询文件:

tours: {
        type: new GraphQLList(type),
        args: {
            name: {
                type: GraphQLString
            },
            pax_count: {
                type: GraphQLInt
            },            
            arrival_date: {
                type: GraphQLDate
            },
            arrival_time: {
                type: GraphQLTime
            }
        },
        resolve: Tour.findMatching.bind(Tour)
    },
    tour: {
        type,
        args: {
            id: {
                type: GraphQLID
            }
        },
        resolve: Tour.getByID.bind(Tour)
    }

类型文件:

let {
    GraphQLID,
    GraphQLString,
    GraphQLInt,
    GraphQLFloat,
    GraphQLObjectType,
    GraphQLNonNull,
    GraphQLList
} = require('graphql')

const { GraphQLDate, GraphQLTime } = require('graphql-iso-date')


// Defines the type
module.exports = new GraphQLObjectType({
    name: 'Tour',
    description: 'A Tour',
    fields: {
        id: {
            type: new GraphQLNonNull(GraphQLID)
        },
        name: {
            type: new GraphQLNonNull(GraphQLString)
        },
        pax_count: {
            type: GraphQLInt
        },
        arrival_date: {
            type: GraphQLDate
        },
        arrival_time: {
            type: GraphQLTime
        }

    }
})

DAO文件:

static get TABLE_NAME() {
        return 'tours'
    }

    /**
     * Returns a tour by its ID
     */
    static async getByID(_, {id}) {
        return await this.find(id)
    }

    /**
     * Returns a list of tours matching the passed fields
     * @param {*} fields - Fields to be matched
     */
    static async findMatching(_, fields) {
        // Returns early with all tours if no criteria was passed
        if (Object.keys(fields).length === 0) return this.findAll()

        // Find matching tours
        return this.findByFields({
            fields
        })
    }

任何人都可以提供帮助。 ?

谢谢

0 个答案:

没有答案