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.
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.