aws cdk appsync Schema Creation Status is FAILED with details: Internal Failure while saving the schema
Asked Answered
P

4

19

Given the following graphql schema

# graphql/schema.graphql
type AppUser {
  userId: String
  fullName: String
}

type Query {
  getUser(userId: String): AppUser
  getUsers(): [AppUser]
}

type Mutation {
  addUser(user: AppUser!): AppUser
}

And the following cdk code defined at lib/mystack.ts:


    const graphql = new appsync.GraphQLApi(this, 'GraphQLApi', {
      name: 'metrolens-graphql-api',
      logConfig: {
        fieldLogLevel: appsync.FieldLogLevel.ALL,
      },
      authorizationConfig: {
        defaultAuthorization: {
          // userPool,
          defaultAction: appsync.UserPoolDefaultAction.ALLOW,
        },
        // additionalAuthorizationModes: [{ apiKeyDesc: 'My API Key' }],
      },
      schemaDefinitionFile: props?.schemaDirectory,
    })

    const lambdaRegister = new nodejs.NodejsFunction(this, 'register', {
      functionName: 'register',
      runtime: lambda.Runtime.NODEJS_12_X,
      timeout: cdk.Duration.seconds(10),
      entry: './lambda/register/register-0.ts',
      handler: 'handler',
      layers: [layer],
      description: 'Register a user.',
    })

    const lambdaLogin = new nodejs.NodejsFunction(this, 'login', {
      functionName: 'login',
      runtime: lambda.Runtime.NODEJS_12_X,
      timeout: cdk.Duration.seconds(10),
      entry: './lambda/login/login-0.ts',
      handler: 'handler',
      layers: [layer],
      description: 'Login a user.',
    })

    const lambdaRegisterDataSource = graphql.addLambdaDataSource(
      'lambdaRegister',
      'Register lambda triggered by appsync',
      lambdaRegister
    )
    lambdaRegisterDataSource.createResolver({
      typeName: 'Mutation',
      fieldName: 'addUser',
      requestMappingTemplate: appsync.MappingTemplate.lambdaRequest(),
      responseMappingTemplate: appsync.MappingTemplate.lambdaResult(),
    })

    const lambdaLoginDataSource = graphql.addLambdaDataSource(
      'lambdaLogin',
      'Login lambda triggered by appsync',
      lambdaLogin
    )

    lambdaLoginDataSource.createResolver({
      typeName: 'Query',
      fieldName: 'getUsers',
      requestMappingTemplate: appsync.MappingTemplate.lambdaRequest(),
      responseMappingTemplate: appsync.MappingTemplate.lambdaResult(),
    })

I get the following error message during the AWS::AppSync::GraphQLSchema deployment phase:

GraphQLApi/Schema (GraphQLApiSchema5937B126) Schema Creation Status is FAILED with details: Internal Failure while saving the schema.

Has anyone encountered this error before? I suspect there is something wrong with my graphql schema but I couldn't tell what it was. Another answer mentioned that the DynamoDB reserved words at https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html could cause conflicts, even if you don't use DynamoDB, so I renamed the User type to AppSync. However, it looks like Query is also a reserved word, but as far as I understand, that is also necessary for a graphql definition. Please help. AWS CDK suck hard.

Pendley answered 2/5, 2020 at 10:2 Comment(0)
P
51

The issue was my schema was incorrect. AppUser is a type, but I used as an input for the addUser mutation. You define the inputs for queries and mutations with the special input keyword. I ended up duplicating AppUser to be:

input AppUserInput {
  userId: String
  fullName: String
}

And that solved the problem.

Pendley answered 18/7, 2020 at 19:59 Comment(3)
Thanks, helped me a lot. That error was not really descriptive.Intimist
Adding a note - use the AppSync schema editor in the console - I had the same error - turns out AppSync doesn't recognize triple quote comments which most validators allow.Courtesan
I was searching the docs and you can see in the generated schema how the inputs were used in the functions: docs.amplify.aws/cli-legacy/graphql-transformer/model/…Decode
O
4

To understand where the issue appears with your graphql schema in this case, you can just go to your AppSync API and manually try to add the schema (copy/paste) and it will automatically throw an error.

enter image description here

Organelle answered 13/6, 2023 at 7:36 Comment(0)
B
2

Update getUsers(): [AppUser] to getUsers: [AppUser]

I came across this same error and found what it was by plugging my schema into a validator.

Entering your one gives

{
  "error": "Syntax error while parsing GraphQL query. Invalid input \"{\\n  getUser(userId: String): AppUser\\n  getUsers()\", expected ImplementsInterfaces, Directives, FieldDefinition or Comments (line 6, column 12):\ntype Query {\n           ^"
}
Blanc answered 26/5, 2020 at 4:41 Comment(1)
Which validator did you use? (and where is it located?). If possible could you please tell us the commands that you used to get that output?Javelin
T
0

I had accidently edited the graphql from

enum Status { PENDING APPROVED }

to

enum String { PENDING APPROVED }

Reverting this change solved the issue.

Tripper answered 8/2 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.