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

10,144

Solution 1

We had this problem. We were looking to query a service object in the database that had a price on it.

Expected Result:

service: {
  price: 9999
}

However, we accidentally queried “services” (instead of “service”) which gave us an array of prices (with only one price) like so:

[ [Object: null prototype] { price: 9.99 } ]

This was caused by a bad query.

Once we changed the query to “service” (instead of “services”) the data came back as expected without the null prototype.

We use Prisma as our ORM though but perhaps you are querying for recipes when you should be querying for recipe.

Solution 2

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' }

Solution 3

Normally, when passing InputTypes as an argument, I solve it using destructuring, like this:

addRecipe: async (root, { ...args }, { req }, info) => {
   // args.category = '5c28c79af62fad2514ccc788'
   // args.ingredient = '5c28c8deb99a9d263462a086'
   // console.log(args.map(x => x))
   return Recipe.create(args)
}

Share:
10,144
FIrman
Author by

FIrman

Updated on July 01, 2022

Comments

  • FIrman
    FIrman almost 2 years

    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']
    }
    
  • amanb
    amanb over 5 years
    Please include some explanation for your answer.
  • AlxVallejo
    AlxVallejo about 5 years
    I get the null prototype when I run a create mutation and return the results as well, so not sure if it's a naming thing.
  • F2BEAR
    F2BEAR over 3 years
    @amanb the reason why this works it's because you first use JSON.strigify(args) transforming the value of args from [[Object: null prototype] { id: '5c28c79af62fad2514ccc788' }] to [{ id: '5c28c79af62fad2514ccc788' }] and then with the JSON.parse you transform it to a valid JS value. In my case I prefered to do JSON.strigify(args[0]) to achieve the needed result of { name: 'test', type: 'A' }