Js doee
Js doee

Reputation: 333

users not a constructor in graphql

am building an app with graphQl using node js, but am getting am error saying users is not a constructor, here is my code

const graphql = require('graphql')
const Users = require('../models/user')

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

const UserType = new GraphQLObjectType({
    name: 'Users',
    fields: () => ({
        id: {
            type: GraphQLID
        },
        username: {
            type: GraphQLString
        },
        email: {
            type: GraphQLString
        }
    })
})

const RootQuery = new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
        user: {
            type: UserType,
            args: {
                id: {
                    type: GraphQLID
                }
            },
            resolve(parent, args) {
                return Users.findById(args.id)
            }
        }
    }
})

const Mutation = new GraphQLObjectType({
    name: 'Mutation',
    fields: {
        register: {
            type: UserType,
            args: {
                username: {
                    type: new GraphQLNonNull(GraphQLString)
                },
                email: {
                    type: new GraphQLNonNull(GraphQLString)
                }
            },
            resolve(parent, args) {
                let hUsers = new Users({
                    username: args.username,
                    email: args.email
                })
                return hUsers.save()
                //.then((data) => {
                // here is where i dont know how to proceed
                // })
            }
        }
    }
})

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

What i want is to create a registration page where users will register from. i tried testing it with Grahica but i keep getting that error "Users is not a constructor". I dont know what am doing wrong or how to solve this problem.

Upvotes: 2

Views: 3210

Answers (2)

Sina Abedi
Sina Abedi

Reputation: 2401

you have to create a model, then export it as below:

const UserModel = mongoose.model("User", userSchema);
module.exports = UserModel;

or just:

module.exports = mongoose.model("User", userSchema);

I've tried many things, but this works better than anything else :)

Upvotes: 2

Ali
Ali

Reputation: 41

In User Model, you should export like this.

module.exports = mongoose.model('User', userSchema);

Upvotes: 2

Related Questions