iamshaunjp / graphql-playlist

All course files for the GraphQL tutorial playlist on The Net Ninja YouTube channel.
754 stars 627 forks source link

Lesson 18 mongoose graphql error #26

Open Human-bio opened 3 years ago

Human-bio commented 3 years ago

I clonned the repo attached my mongo database but this error showed up when I requested a mutation"message": "(Unauthorized) not authorized on admin to execute command { insert: \"authors\", mongoose schema code

const graphql = require('graphql');
const Book = require('../models/book');
const Author = require('../models/Author');
const _ = require('lodash');

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

const BookType = new GraphQLObjectType({
    name: 'Book',
    fields: ( ) => ({
        id: { type: GraphQLID },
        name: { type: GraphQLString },
        genre: { type: GraphQLString },
        author: {
            type: AuthorType,
            resolve(parent, args){
                //return _.find(authors, { id: parent.authorId });
            }
        }
    })
});

const AuthorType = new GraphQLObjectType({
    name: 'Author',
    fields: ( ) => ({
        id: { type: GraphQLID },
        name: { type: GraphQLString },
        age: { type: GraphQLInt },
        books: {
            type: new GraphQLList(BookType),
            resolve(parent, args){
                //return _.filter(books, { authorId: parent.id });
            }
        }
    })
});

const RootQuery = new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
        book: {
            type: BookType,
            args: { id: { type: GraphQLID } },
            resolve(parent, args){
                //return _.find(books, { id: args.id });
            }
        },
        author: {
            type: AuthorType,
            args: { id: { type: GraphQLID } },
            resolve(parent, args){
                //return _.find(authors, { id: args.id });
            }
        },
        books: {
            type: new GraphQLList(BookType),
            resolve(parent, args){
                //return books;
            }
        },
        authors: {
            type: new GraphQLList(AuthorType),
            resolve(parent, args){
                //return authors;
            }
        }
    }
});

const Mutation = new GraphQLObjectType({
    name: 'Mutation',
    fields: {
        addAuthor: {
            type: AuthorType,
            args: {
                name: { type: GraphQLString },
                age: { type: GraphQLInt }
            },
            resolve(parent, args){
                let author = new Author({
                    name: args.name,
                    age: args.age
                });
                return author.save();
            }
        }
    }
});

module.exports = new GraphQLSchema({
    query: RootQuery,
    mutation: Mutation
});
`
Human-bio commented 3 years ago

node:31482) MongoError: (Unauthorized) not authorized on admin to execute command { insert: "authors", documents: [[{name gyfdgyiszukjfheusdzyih} {age 88} {_id ObjectID("60af9c682215ea7afad86f4c")} {__v 0}]], ordered: false, writeConcern: { w: "majority" }, txnNumber: 4.000000, $clusterTime: { clusterTime: {1622121571 1}, signature: { hash: {0 [134 224 34 24 72 83 84 21 31 152 195 189 83 14 136 196 101 10 239 180]}, keyId: 6954772510130831360.000000 } }, lsid: { id: {4 [115 137 219 18 191 230 79 106 130 121 29 168 108 239 195 227]} } }

jessewriter commented 1 week ago

i think the dependencies of this project are so out of date that a lot of things are not working as expected. needs a refresh using MongoDb and modern react functional components. wondering if anyone has refactored this yet as I feel like I have to start from scratch at this point.