I'm using ReactJS and aws-amplify
to execute graphql operations.
CODE:
import { API, graphqlOperation } from 'aws-amplify'; import { UpdateInput } from './mutations.js'; // Call mutation const input = { /* some values */ }; API.graphql(graphqlOperation(UpdateInput, input)).then(...);
GraphQL mutation definition:
export const UpdateInput = `mutation UpdateInput($input: Input!) { updateInput(input: $input) { id, name } }`
GraphQL Schema:
input Input { id: ID! name: String } type Mutation { updateInput(input: Input!): String }
However, I get an error:
[Log] Variable 'input' has coerced Null value for NonNull type 'Input!'
Using AWS console my mutation works and input
is NonNull (using a debugger)
Any ideas what's causing the error?
But first, the promised cheatsheet... A GraphQL query can have fields and inputs, with or without variables. Fields are always optional, whether nullable or not. In inputs, such as field arguments, nullable types are always optional and non‐null types are always required.
In addition, a variable of a nullable type cannot be provided to a non‐null argument. If you get a null value for a nullable field from the server, there are four possibilities: The value is actually null or absent, e.g. user doesn't exist The value is hidden, e.g. user has blocked you (permission error)
A GraphQL query can have fields and inputs, with or without variables. Fields are always optional, whether nullable or not. In inputs, such as field arguments, nullable types are always optional and non‐null types are always required.
To top it off, any fields with errors have the value null. All of this to say: all types in GraphQL are nullable by default in case of an error. GraphQL doesn't let a small hiccup get in the way of our data fetching: when there's an error with a field, we still get partial data.
The key was input
in the updateInput
mutation.
updateInput(input: Input!): String // ^^^^^ input key
Thus, need to specify correct key in the passed variable.
const variables = { input: someData, // key is "input" based on the mutation above }; API.graphql(graphqlOperation(UpdateInput, variables)).then(...);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With