Apollo client- [Unhandled promise rejection: Error: Response not successful: Received status code 400]

Trying to run mutation… does work from apollo “/graphql” interface link

my mutation:


mutation sendChatMessage($data: SendMessageInput!) {
  sendMessage(data: $data)
}

Query Vaiable:
{"data":{"ChatMessageInput":[{"_id":"307abdf5-5ca9-41c5-b019-837c51de5068","createdAt":"2021-08-12T23:41:41.594Z","text":"Aaa","user":{"_id":"123"}}],"to":"456"}}

however doing same mutation query from expo code … i am getting this error:

[GraphQL error]: Message: Variable "$data" of required type "SendMessageInput!" was not provided., Location: [object Object], Path: undefined
[Network error]: ServerError: Response not successful: Received status code 400

[Unhandled promise rejection: Error: Response not successful: Received status code 400]

my expo client code file:

import React, {useEffect} from 'react'
import { StyleSheet, Text, View } from 'react-native'

import { gql, useMutation } from "@apollo/client";




const POSTMESSAGE = gql`
  mutation sendChatMessage($data: SendMessageInput!) {
    sendMessage(data: $data)
  }
`;


const ComponentA = () => {

  const [sendToMessage, { loading, error, data }] = useMutation(POSTMESSAGE);

  useEffect(() => {
    sendToMessage({"data":{"ChatMessageInput":[{"_id":"307abdf5-5ca9-41c5-b019-837c51de5068","createdAt":"2021-08-12T23:41:41.594Z","text":"Aaa","user":{"_id":"123"}}],"to":"456"}})
  }, [])


  if (loading) return <Text>Loading...</Text>;
  if (error) return <Text>Error :(</Text>;


  return (
    <View>
      <Text>Hello world</Text>
    </View>
  )
}

export default ComponentA

@rosnk try prefixing your sendToMessage parameter with variables, like:

useEffect(() => {
  sendToMessage({ 
    variables: { 
      data: {
        ...
      }
    }
  })
}, [])
1 Like

@hwillson worked… thanks!!
was missing variables in the mutation syntax.

1 Like

Solved.