Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get rid of [Object: null prototype] in GraphQL

I'm trying to make one-to-many relationship database with Mongoose and GraphQL.

Whenever I insert the data to GraphQL mutation argument, I will get [Object: null prototype] error.

I notice the object will have [Object: null prototype] in front of it when I tried to console.log for debug purpose.

I have tried many ways, tried to map() args or even to use replace() but no luck. All I have been getting is "args.ingredient.map/replace is not a function"

I have test hard coded method by changing the args for example:

args.category = '5c28c79af62fad2514ccc788'
args.ingredient = '5c28c8deb99a9d263462a086'

Surprisingly it works with this method. I assume the input cannot be an object but just an ID.

Refer below for actual results.

Resolvers

Query: {
    recipes: async (root, args, { req }, info) => {
        return Recipe.find({}).populate('ingredient category', 'name createdAt').exec().then(docs => docs.map(x => x))
    },
},
Mutation: {
    addRecipe: async (root, args, { req }, info) => {
      // args.category = '5c28c79af62fad2514ccc788'
      // args.ingredient = '5c28c8deb99a9d263462a086'
      // console.log(args.map(x => x))
      return Recipe.create(args)
    }
}

TypeDef

extend type Mutation {
    addRecipe(name: String!, direction: [String!]!, ingredient: [IngredientInput], category: [CategoryInput]): Recipe
}

type Recipe {
    id: ID!
    name: String!
    direction: [String!]!
    ingredient: [Ingredient!]!
    category: [Category!]!
}

input IngredientInput {
    id: ID!
}

input CategoryInput {
    id: ID!
}

Models

const recipeSchema = new mongoose.Schema({
    name: String,
    direction: [String],
    ingredient: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Ingredient' }],
    category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category' }
}, {
    timestamps: true // createdAt, updateAt
})

const Recipe = mongoose.model('Recipe', recipeSchema)

This is the result I console log the args when inserting the data

{ 
    name: 'Butter Milk Chicken TEST2',
    direction: [ 'Step1', 'Step2', 'Step3' ],
    ingredient:[[Object: null prototype] { id: '5c28c8d6b99a9d263462a085' }],
    category: [[Object: null prototype] { id: '5c28c79af62fad2514ccc788' }]
}

I assume I need to get something like this

{ 
    name: 'Butter Milk Chicken TEST2',
    direction: [ 'Step1', 'Step2', 'Step3' ],
    args.category = ['5c28c79af62fad2514ccc788']
    args.ingredient = ['5c28c8ccb99a9d263462a083', '5c28c8d3b99a9d263462a084', '5c28c8d6b99a9d263462a085']
}
like image 550
FIrman Avatar asked Dec 31 '18 03:12

FIrman


Video Answer


1 Answers

You can do something like below,and [Object: null prototype] would disappear

const a = JSON.parse(JSON.stringify(args));

args.category is

[[Object: null prototype] { id: '5c28c79af62fad2514ccc788' }], 

JSON.parse(JSON.stringify(args.category) would be { id: '5c28c79af62fad2514ccc788' }

like image 138
hzgood Avatar answered Sep 29 '22 14:09

hzgood