I have been trying to run a mutation to create relations to two separate Types with not much success.
** SCHEMA **
(I have used "Create Resources" to create tables in DynamoDB)
type Comment {
eventId: ID!
commentId: String!
content: String
}
type CommentConnection {
items: [Comment]
nextToken: String
}
input CreateCommentInput {
eventId: ID!
commentId: String!
content: String
}
input CreateEventInput {
id: ID!
name: String
where: String
when: String
description: String
}
input DeleteCommentInput {
eventId: ID!
}
input DeleteEventInput {
id: ID!
}
type Event {
id: ID!
name: String
where: String
when: String
description: String
comments(limit: Int, nextToken: String): CommentConnection
}
type EventConnection {
items: [Event]
nextToken: String
}
type Mutation {
createEvent(input: CreateEventInput!): Event
updateEvent(input: UpdateEventInput!): Event
deleteEvent(input: DeleteEventInput!): Event
createComment(input: CreateCommentInput!): Comment
updateComment(input: UpdateCommentInput!): Comment
deleteComment(input: DeleteCommentInput!): Comment
commentOnEvent(input: commentOnEventInput!): Comment
}
type Query {
fetchEvent(id: ID!): Event
getEvent(id: ID!): Event
listEvents(first: Int, after: String): EventConnection
getComment(eventId: ID!): Comment
listComments(first: Int, after: String): CommentConnection
}
type Subscription {
onCreateEvent(
id: ID,
name: String,
where: String,
when: String,
description: String
): Event
@aws_subscribe(mutations: ["createEvent"])
onUpdateEvent(
id: ID,
name: String,
where: String,
when: String,
description: String
): Event
@aws_subscribe(mutations: ["updateEvent"])
onDeleteEvent(
id: ID,
name: String,
where: String,
when: String,
description: String
): Event
@aws_subscribe(mutations: ["deleteEvent"])
onCreateComment(eventId: ID, commentId: String, content: String): Comment
@aws_subscribe(mutations: ["createComment"])
onUpdateComment(eventId: ID, commentId: String, content: String): Comment
@aws_subscribe(mutations: ["updateComment"])
onDeleteComment(eventId: ID, commentId: String, content: String): Comment
@aws_subscribe(mutations: ["deleteComment"])
}
input UpdateCommentInput {
eventId: ID!
commentId: String
content: String
}
input UpdateEventInput {
id: ID!
name: String
where: String
when: String
description: String
}
input commentOnEventInput {
eventId: ID!
content: String
}
schema {
query: Query
mutation: Mutation
subscription: Subscription
}
** MUTATIONS **
mutation #1:
mutation {
createEvent(input: {
id: "id8888"
name: "some event"
where: "Tokyo"
when: "tomorrow"
description: "desc for event"
})
{
id
name
}
}
mutation #1 gives:
{
"data": {
"createEvent": {
"id": "id8888",
"name": "some event"
}
}
}
mutation #2:
mutation {
commentOnEvent(input : {
eventId: "id8888"
commentId: "id2222"
content: "some content"
})
{
commentId
content
}
}
mutation #2 gives:
{
"data": {
"commentOnEvent": null
}
}
In the React sample created by AWS AppSync creates commentId automatically but I can't recreate that in manually created schema and resource.
I would like to know how I can establish relations on separate types and query them. Has anybody successfully done this??
Starting from the console's “Create resources” functionality lets talk through how this works. Assume we have this schema.
This is what the schema should look like after running Create Resources on the Event and Comment types. When going through the "Create Resources" flow with the Comment type you should choose the eventId as the table's hash key and the commentId as the sort key. For the Event type you can leave "id" as the single hash key. So what did that do for us?
First it created 2 DynamoDB tables to hold our objects of type Event and Comment. It then imported those tables as AppSync data sources and generated new schema parts including input objects, objects, and query and mutation fields and saved them to the schema. It also wired up resolvers specific to the new table you just defined and attached them to the newly generated query and mutation fields that implement common CRUD patterns. Unfortunately this does not yet understand relations so we have to add those ourselves. To do that let's first make the mutation to create relations as you have asked about and for completeness we will do a query as well.
As you have already done, you are going to need to add something like this to your schema
Save the schema and then click "Attach" on the Mutation.commentOnEvent field to add a resolver. Select the CommentTable data source we created earlier and from the mapping template put this:
and for the response mapping template
Click save. Now you should be able to run a query like this:
Let's now add away to read data via a relation. E.G. I want to be able to run a query like this:
To do this lets first add the following parts to the schema.
Click save then click "Attach" on the Event.comments field. Select the CommentTable data source again and then provide the following for the request mapping template.
Notice the $ctx.source.id. Since we are resolving the Event.comments field, the $ctx.source is the instance of the Event type that we are resolving comments for. In effect this makes it so that anywhere we include
{ comments { ... }
in a selection set on the Event type, only comments for the parent event will be fetched. And then you can return the paginated result object.This should do the trick. Now you can run both these queries and see the results.
Hope this helps.