Graphql express nested schema returning all data. Help needed

Will

Published · May 11

I am currently building mern + graphql app. I am new to graphql and I have two different models a User Model and Character model. The Character Model is nested in the User. When I create a user in the Graphiql interface I want it to display its data and the character that is associated with the specific user but instead it is displaying every character that has been created in the database and adding them to every user created instead of the specific user. Trying to figure out how to fix this. Link to Github Repo

const mongoose = require('../db/connection') const Schema = mongoose.Schema const User = new Schema({ id: String, name: String, totalwins: Number, description: String, charactersID: [ { type: Schema.Types.ObjectId, ref: "Character" } ] }) module.exports = mongoose.model('User', User)

Character Mongoose Model
const mongoose = require('mongoose') const Schema = mongoose.Schema const Character = new Schema({ name: String, wins: Number, losses: Number, percentage: Number, image: String }) module.exports = mongoose.model('Character', Character)

Types
// User Schema const userType = new GraphQLObjectType({ name: 'User', fields : () => ({ _id: {type: GraphQLID}, name : { type: GraphQLString}, totalwins : { type: GraphQLInt }, description : { type: GraphQLString }, charactersID : { type : new GraphQLList(characterType), resolve(parent, args){ return Character.find({charactersID: parent.id}) } } }) }) // Character Schema const characterType = new GraphQLObjectType({ name: 'Character', fields : () => ({ id: {type: GraphQLID}, name : { type: GraphQLString}, wins : { type: GraphQLInt }, losses : { type: GraphQLInt }, percentage: {type: (GraphQLInt)}, image: { type: GraphQLString }, }) })

Query
//query the graph to grab the data const Query = new GraphQLObjectType({ name: 'Query', fields: { user : { type: userType, // arguments passed by the user while making the query args: {id : {type : GraphQLID}}, resolve(parent, args){ //finding a single user by id return User.findById(args.id) } }, users : { type: new GraphQLList(userType), resolve(parent, args) { return User.find({}) } }, character : { type: characterType, args: {id : {type : GraphQLID}}, resolve(parent, args){ return Character.findById(args.id) } }, characters : { type: new GraphQLList(characterType), resolve(parent, args) { return Character.find({}) } } } })

Mutations
//allows user to add, update and delete to mondodb through graphql const Mutation = new GraphQLObjectType({ name: 'Mutation', fields: { addUser: { type: userType, args: { name: {type: GraphQLNonNull(GraphQLString)}, totalwins: {type: (GraphQLInt)}, description: {type: (GraphQLString)}, charactersID: { type: new GraphQLNonNull(GraphQLID)} }, resolve(parent, args){ let user = new User({ name: args.name, totalwins: args.totalwins, description: args.description, charactersID: args.charactersID }) return user.save() } }, addCharacter: { type: characterType, args: { name: {type: GraphQLNonNull(GraphQLString)}, wins: {type: (GraphQLInt)}, losses: {type: (GraphQLInt)}, percentage: {type: (GraphQLInt)}, image: {type: (GraphQLString)}, }, resolve(parent, args){ let character = new Character({ name: args.name, wins: args.wins, losses: args.losses, percentage: args.percentage, image: args.image }) return character.save() } }, deleteUser: { type: userType, args: { id: {type: new GraphQLNonNull(GraphQLString)} }, resolve(parent, args){ const remUser = User.findByIdAndRemove(args.id) if(!remUser){ throw new Error('No character found') } return remUser } }, deleteCharacter: { type: characterType, args: { id: {type: new GraphQLNonNull(GraphQLString)} }, resolve(parent, args){ const remCharacter = Character.findByIdAndRemove(args.id) if(!remCharacter){ throw new Error('No character found') } return remCharacter } }, } }) module.exports = new GraphQLSchema({ query : Query, mutation : Mutation})
Results from query


Write your answer...

Never miss a post from Will, when you sign up for Ednsquare.