Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS GraphQL: Variable 'input' has coerced Null value for NonNull type 'Input!'

Tags:

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?

like image 829
Joseph D. Avatar asked Aug 24 '18 07:08

Joseph D.


People also ask

Can a GraphQL query have a nullable field?

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.

Can a variable of a nullable type be provided to non‐Null Arguments?

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)

What are fields and inputs in a GraphQL query?

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.

What happens when there is an error with a GraphQL field?

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.


1 Answers

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(...); 
like image 176
Joseph D. Avatar answered Sep 19 '22 23:09

Joseph D.