55 Cancri
55 Cancri

Reputation: 1195

While using aws cdk with appsync, how to add mutation to graphql schema and prevent deployment from failing?

When my schema looks like this, aws cdk deploys fine (notice the mutations are commented out):

schema {
  query: Query
  # mutation: Mutation
}

type AppUser {
  userId: String
  fullName: String
}

type Query {
  getUser(id: ID!): AppUser
  getUsers: [AppUser]
}

# type Mutation {
#   addUser(id: ID!, newUser: AppUser!): AppUser!
# }

But when I uncomment the Mutation portions:

schema {
  query: Query
  mutation: Mutation
}

type AppUser {
  userId: String
  fullName: String
}

type Query {
  getUser(id: ID!): AppUser
  getUsers: [AppUser]
}

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

The cdk fails at at the AWS::AppSync::GraphQLSchema with the following message: Schema Creation Status is FAILED with details: Internal Failure while saving the schema. Help?

Upvotes: 2

Views: 1068

Answers (1)

55 Cancri
55 Cancri

Reputation: 1195

The error message from aws cdk was quite vague. The issue was with my graphql schema. The type AppUser cannot be used as a type for the input of the addUser mutation. My solution was to create a new type with an identical data structure as the AppUser and start it with input instead of type. The new schema looks like this:

schema {
  query: Query
  mutation: Mutation
}

type AppUser {
  userId: String
  fullName: String
}

input NewUser {
  userId: String
  fullName: String
}

type Query {
  getUser(id: ID!): AppUser
  getUsers: [AppUser]
}

type Mutation {
  addUser(newUser: NewUser!): AppUser!
}

Upvotes: 3

Related Questions