2

I'm struggling to write a nested GraphQL mutation for a React Native app I'm building with GraphQL client being AWS Amplify API. Here's my type definition file

type Game @model {
  id: ID!
  gameId: String!
  players: [Player!]!
}

type Player @model {
  id: ID!
  username: String!
}

Here's my attempt to create a mutation to create a new Game

import API, { graphqlOperation } from '@aws-amplify/api';

const CreateGame = `
mutation ($gameId: String! $username: String!) {
  createGame(input: {
    gameId: $gameId, 
    players: [{ username: $username }]
  }) {
    id
    gameId
    players
  }
}
`;

const gameObj = {
  gameId: 'example_game_id',
  username: 'example_username'
};
const queryResp = await API.graphql(graphqlOperation(CreateGame, gameObj));
console.log(queryResp.data);

Here's the error I get

"Validation error of type WrongType: argument 'input' with value 'ObjectValue{objectFields=[ObjectField{name='gameId', value=VariableReference{name='gameId'}}, ObjectField{name='players', value=ArrayValue{values=[ObjectValue{objectFields=[ObjectField{name='username', value=VariableReference{name='username'}}]}]}}]}' contains a field not in 'CreateGameInput': 'players' @ 'createGame'"
tlaminator
  • 946
  • 2
  • 9
  • 23
  • pass entire 'input' variable using 'query variables' ... of course test this method in graphiql/playground before writting code – xadm Jun 08 '20 at 13:39
  • @xadm can you elaborate on that? I'm fairly new to graphql so I'm not sure what `query variables` mean here – tlaminator Jun 08 '20 at 16:08

0 Answers0