I am trying to get started with AWS AppSync and AWS Amplify.
So far, I managed to follow the AWS documentation (especially here and here) to successfully create a sample TODO app (third code snippet) and enable the AppSync GraphQL API like so:
$ amplify add api
? Please select from one of the below mentioned services GraphQL
? Provide API name: MySampleTodoAPI
? Choose an authorization type for the API API key
? Do you have an annotated GraphQL schema? No
? Do you want a guided schema creation? true
? What best describes your project: Single object with fields (e.g., “Todo” with ID, name, description)
This generates this "trivial" schema.graphql
(i.e. containing only a single object):
type Todo @model {
id: ID!
name: String!
description: String
}
amplify push
generates a much more involved, secondary schema.graphql
from the file above, creates JavaScript code with objects for mutations, queries, etc. and also sets up AWS resources (i.e. DynamoDB table, S3 buckets, etc.). The app seems to have bugs, but essentially works - including adding data entered in the UI to the DynamoDB table.
I have created a second sample Blog app in the same way as above, only this time choosing Single object with fields (e.g., “Todo” with ID, name, description)
instead of Single object with fields ...
.
This generates this "complex" schema.graphql
(i.e. containing multiple, connected objects):
type Blog @model {
id: ID!
name: String!
posts: [Post] @connection(name: "BlogPosts")
}
type Post @model {
id: ID!
title: String!
blog: Blog @connection(name: "BlogPosts")
comments: [Comment] @connection(name: "PostComments")
}
type Comment @model {
id: ID!
content: String
post: Post @connection(name: "PostComments")
}
Question: How do I deal with "complex" objects in a React application when talking to the AWS AppSync GraphQL backend ?
As a (contrived) example, assuming I want to add a new Blog
object with one Post
and one Comment
object, can I somehow pass all objects to a single mutation in a single Connect
React component ? Or do I have to first trigger a Blog
mutation, followed by the other two ? Or do I have to look into customizing the (secondary) schema.graphql
and JavaScript files that Amplify generates for me ?
Unfortunately, the AWS sample code only deals with "trivial" schemas, not "complex" ones - and Amplify seems to be so fresh out of the box that all the third party posts and sample projects use other technologies...
Thank you very much for your consideration! :-)