Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL error: Cannot query field 'mutation_name' on type 'Mutation'

I am trying to mutate a mutation. The mutation does exist and works fine on my graphql playground. but as I implement it in my react component, I get error. Queries work fine though. By the way, I need client in my code so I definitely have to use ApolloConsumer.

I tried using client.mutate like https://github.com/apollographql/apollo-client/issues/2762

export const LOGIN = gql`
  mutation LOGIN($email: String!, $password: String!) {
    login(email: $email, password: $password) {
      email
    }
  }
`;
class LoginComponent extends Component{
  render(){
    return(
      <ApolloConsumer>
        {client=>{
          return(
            <Button onClick={()=>{
              client
                .mutate({
                  mutation: LOGIN,
                  variables: {
                    email: "[email protected]",
                    password: "test"
                    }
                })
                .then(result => {
                  console.log('result', result)
                })
                .catch(err => {
                  console.log("err", err);
                  alert(err.toString());
                });
            }}> 
              OK
            </Button>
          )
        }}
      </ApolloConsumer>
    )  
  }
}

I expect to get success but I get Error: GraphQL error: Cannot query field 'login' on type 'Mutation'. (line 2, column 3): login(email: $email, password: $password) { ^

like image 299
Anita Avatar asked Sep 11 '25 11:09

Anita


1 Answers

My mistake was trivial: I was using type-graphql and apollo-server in the backend but added the datasource instead of the correct resolver in the resolvers field:


const schema = await buildSchema({
    resolvers: [
      FirstResolver,
      SecondResolver,
      // added datasource instead of resolver
      ThirdDataSource,
      FourthResolver,
]})

Replacing datasource with the resolver solved the issue.

Package versions:

    "apollo-datasource": "^3.0.3",
    "type-graphql": "^1.1.1",
    "apollo-server-core": "^3.6.2",
like image 103
exaucae Avatar answered Sep 16 '25 10:09

exaucae