GraphQL:Rootquery返回null

时间:2018-10-27 10:56:00

标签: javascript node.js graphql

我想要一个root查询,该查询返回表中的每一行。但是目前,当我调用该rootquery时,我只会收到null。但是我的rootquery之一只返回具有特定ID的行确实可以工作。所以我要去哪里错了?

Rootquery的:

这个作品

aanvraag:{
        type: AanvraagType, 
        args:{id: {type: GraphQLID}},
        resolve(parentValue, args){
            const query = `SELECT * FROM aanvraag where id=${args.id}`;
            return db.conn.one(query)
                .then(data => {
                    return data;
                })
                .catch(err => {
                    return 'Error is: ', err
                });
        }
    },

这不起作用

   aanvragen:{
        type: AanvraagType,
        resolve(parentValue, args){
            const query = 'SELECT * from aanvraag';
            return db.conn.many(query)
                .then(data => {
                    return data;
                })
                .catch(err => {
                    return 'Error is: ', err
                });
        }
    }

如果有人需要,这是整个文件:

const graphql = require('graphql');
const pgp = require('pg-promise')();
const axios = require('axios');
const db = {}
var cn = {
    host: 'localhost', // server name or IP address;
    port: 5432,
    database: 'admin',
    user: 'admin',
    password: 'admin123'
};

db.conn = pgp(cn);

const {
    GraphQLObjectType,
    GraphQLID,
    GraphQLString,
    GraphQLSchema
} = graphql;


const TeamlidType = new GraphQLObjectType({
    name: 'Teamlid',
    fields: {
        id: { type: GraphQLID },
        email: { type: GraphQLString },
        naam: { type: GraphQLString }
    }
})

const ProjectType = new GraphQLObjectType({
    name: 'Project',
    fields:{
        id: {type: GraphQLID},
        naam: { type: GraphQLString },
        type: { type: GraphQLString },
        lead_naam: { type: GraphQLString },
        lead_email: { type: GraphQLString },
        teamlid:{
            type: TeamlidType,
            resolve(parentValue, args){
                console.log(parentValue.id);
                const query = `SELECT * FROM teamlid WHERE project_id=${parentValue.id}`;
                return db.conn.many(query)
                .then(data => {
                   return data;
                })
                .catch(err => {
                   return 'The error is', err;
                });
            }
        }
    }
})

const AanvraagType = new GraphQLObjectType({
    name: 'Aanvraag', 
    fields:{
        id: {type: GraphQLID},
        naam: { type: GraphQLString },
        email: { type: GraphQLString },
        divisie: { type: GraphQLString },
        afdeling: { type: GraphQLString },
        team: { type: GraphQLString },
        project:{
            type: ProjectType,
            resolve(parentValue, args){
                const query = `SELECT * FROM project WHERE aanvraag_id=${parentValue.id}`;
                return db.conn.one(query)
                .then(data => {
                   return data;
                })
                .catch(err => {
                   return 'The error is', err;
                });
            }
        }
    }
})

const RootQuery = new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
        teamlid: {
            type: TeamlidType,
            args: { id: { type: GraphQLID } },
            resolve(parentValue, args) {
                const query = `SELECT * FROM teamlid WHERE id=${args.id}`;
                return db.conn.one(query)
                    .then(data => {
                        return data;
                    })
                    .catch(err => {
                        return 'The error is', err;
                    });
            }
        },
        aanvraag:{
            type: AanvraagType, 
            args:{id: {type: GraphQLID}},
            resolve(parentValue, args){
                const query = `SELECT * FROM aanvraag where id=${args.id}`;
                return db.conn.one(query)
                    .then(data => {
                        return data;
                    })
                    .catch(err => {
                        return 'Error is: ', err
                    });
            }
        },
        project:{
            type: ProjectType, 
            args:{id: {type: GraphQLID}},
            resolve(parentValue, args){
                const query = `SELECT * FROM project where id=${args.id}`;
                return db.conn.one(query)
                    .then(data => {
                        return data;
                    })
                    .catch(err => {
                        return 'Error is: ', err
                    });
            }
        },
        aanvragen:{
            type: AanvraagType,
            resolve(parentValue, args){
                const query = 'SELECT * from aanvraag';
                return db.conn.many(query)
                    .then(data => {
                        return data;
                    })
                    .catch(err => {
                        return 'Error is: ', err
                    });
            }
        }
    }
})

module.exports = new GraphQLSchema({
    query: RootQuery,
    mutation
})

1 个答案:

答案 0 :(得分:0)

所以我已经找到了答案。我必须将“ aanvraag”包装在GraphQLList中。这样就可以了:

   aanvragen:{
        type: GraphQLList(AanvraagType),
        resolve(){
            const query = 'SELECT * from aanvraag';
            return db.conn.many(query)
                .then(data => {
                    return data;
                })
                .catch(err => {
                    return 'Error is: ', err
                });
        }
    }

我是GrapQL的新手,但我希望这会在将来对某人有所帮助。