Error when defining module.exports for schema when working with MERN stack

Viewed 22

I'm following along with Traversy Media's GraphQl MERN course in the section where the GraphQl schema is being defined. I'm getting errors regarding the definition of the module.exports statement, though I've checked my code against the instructor's code many times and can't find a difference.

I've looked through my code to see if there were any logical or syntax-related errors but seems to be fine for the most part.

Here is my schema.js:

    //Mongoose models
const Project = require('../models/Project');
const Client = require('../models/Client');



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

 // Project Type 
 const ProjectType = new GraphQLObjectType({
      name: 'Project',
      fields: () => ({
         id: { type: GraphQLID},
         name: {type: GraphQLString},
         description: {type: GraphQLString},
         status: {type: GraphQLString}, 
         client: {
            type: ClientType,
            resolve(parent, args)
            {
                return clients.findById(parent.clientId);
            }
         }
      })
 });

  // Client Type 
  const ClientType = new GraphQLObjectType({
    name: 'Client',
    fields: () => ({
       id: { type: GraphQLID},
       name: {type: GraphQLString},
       email: {type: GraphQLString},
       phone: {type: GraphQLString}, 
    })
});

 const RootQuery = new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
        projects: {
            type: new GraphQLList(ProjectType),
            resolve(parent, args){
                return Project.find();
            }
        },
        project: {
            type: ProjectType,
            args: {id: {type: GraphQLID}},
            resolve(parent, args) {
                return Project.findById(args.id);            }
        },
        clients: {
            type: new GraphQLList(ClientType),
            resolve(parent, args){
                return Client.find();
            }
        },
        client: {
            type: ClientType,
            args: {id: {type: GraphQLID}},
            resolve(parent, args) {
                return Client.findById(args.id);            }
        }
    }
 });
//Mutations
const mutation = new GraphQLObjectType({
    name: 'Mutation',
    flelds: {
        addClient: {
            type: ClientType,
            args: {
                name: {
                    type: GraphQLNonNull(GraphQLString)
                },
                email: {
                    type: GraphQLNonNull(GraphQLString)
                }, 
                phone: {
                    type: GraphQLNonNull(GraphQLString)
                },
            },
            resolve(parent, args) {
                const client = new Client({
                    name: args.name,
                    email: args.email,
                    phone: args.phone,
                });
                return client.save(); 
            },
        },
    },
});
module.exports = new GraphQLSchema({
    query: RootQuery,
    mutation,
});

index.js:

const express = require('express');
const colors = require('colors');
require('dotenv').config();
const { graphqlHTTP } = require('express-graphql');
const schema = require('./schema/schema');
const connectDB = require('./config/db');
const port = process.env.PORT || 5000;
const app = express();
//Connect to database
connectDB();
app.use('/graphql', graphqlHTTP({
   schema, graphiql: process.env.NODE_ENV === 'development',
}));
app.listen(port, console.log(`Server running on port ${port}`));

and db.js:

const mongoose = require('mongoose');

const connectDB = async() => {
    const conn = await mongoose.connect(process.env.MONGO_URI);
    console.log(`MongoDB Connected: ${conn.connection.host}`.cyan.underline.bold);
};
module.exports = connectDB;

My expected result is to get a presentation of the GraphiQl interface but I receive an error stating "Mutation fields must be an object with field names as keys or a function which returns such an object."

0 Answers
Related