Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ApolloError: GraphQL error: Validation error of type FieldUndefined: Field '<my query name>' in type 'Mutation' is undefined @ '<my query name'

I'm trying to run a simple mutation using AppSync with aws-sdk in a NodeJS environment. I'm making an error but I can't figure it out. This query works in my AppSync console:

mutation MyMutation {
  createApiKey(input: {key: "testconsole2"}) {
    id
    key
  }
}

Then when I try it in NodeJS I can't get it work (despite all of my other similar queries working):

 async function createApiKey({ encryptedKey }: { encryptedKey: string }) {
    const createApiKeyQueryString = `mutation createApiKey(
      $key: String!,
    ){
      createApiKey(
        input: {
          key: $key
        }
      ){
        key
      }
    }`;
    try {
      const runMutation = await appSyncClient.mutate({
        mutation: gql(createApiKeyQueryString),
        variables: {
          key: encryptedKey,
        },
      });

      console.log({
        response: runMutation // Throws error before we reach here
      });
      return (runMutation as { data }).data;
    } catch (error) {
      console.log(error); //  ApolloError: GraphQL error: Validation error of type FieldUndefined: Field 'createApiKey' in type 'Mutation' is undefined @ 'createApiKey
    }
  }

I'm making a silly error, but I can't figure out what I'm doing wrong, or what the error message means?

like image 602
JimmyTheCode Avatar asked Dec 06 '25 04:12

JimmyTheCode


1 Answers

I was using the wrong AppSync URL (eg, "https://.appsync-api.eu-west-2.amazonaws.com/graphql").

This meant that the schema name for my ApiKey table didn't exist, which is what the following was trying to tell me:

ApolloError: GraphQL error: Validation error of type FieldUndefined: 
Field 'createApiKey' in type 'Mutation' is undefined @ 'createApiKey

Basically, because I hadn't deployed my ApiKey schema from my amplify.graphql file, Amplify hadn't generated the relevant AppSync calls (ie, 'createApiKey'). So when I tried to invoke the call with my NodeJS code, AppSync told me that it was undefined.

like image 89
JimmyTheCode Avatar answered Dec 08 '25 19:12

JimmyTheCode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!