GraphQL apollo playground - How to add array of strings?

230 Views Asked by At

I have always had trouble with the syntax in graphql apollo playground.

For example, I have a simple model in MongoDB

const playerSchema = new Schema({
playerName: {
   type: String,
   required: true,
},
awards: [{
   type: String,
 }]
})

const Player = model('Player', playerSchema)
module.exports = Player;

See above that 'awards' is an array. I want to be able to add an array of strings for awards a player has won.

Below, here is my attempts to add an array of strings in the GraphQL playground in the browser...

mutation addPlayer($playerName: String!, $awards:[String]){
addPlayer(playerName:$playerName, awards:$awards){
    playerName
    awards
  }
}

and here are the query variables with the data

{
    "playerName": "Michael Jordan",
    "awards": ["most valuable player", "rookie of the year", "scoring champion"]
}

If I run a query in GraphQL to see the results of this in the database, here is what it looks like. Why is the 'awards' array empty???

{
    "data": {
        "players": [
            {
              "playerName": "Michael Jordan",
              "awards": [],       //EMPTY ARRAY HERE, WHY? 
            }
        ]
    }
}
0

There are 0 best solutions below