I have a custom GraphQL schema for a blogging application for my ReactJS app. The main model type is Post.
Post contains a number of properties that have a custom type eg Gallery: [MediaObject]. The majority of these properties with custom types are missing from the automatically generated CreatePostInput by AWS Amplify CLI. This means that I cannot create a Post object that has these properties with custom type.
The lack of these custom type properties is evident in the AppSync console, under Queries. In the Explorer, go to Mutations, click on CreatePost mutation, and you will see that 6 of the 21 properties for Post are missing. The missing properties are tags, attributes, seo, featuredImage, gallery and createdBy.
They are also missing in the autogenerated graphql schema in api > build > schema.graphql in the react app.
I have made multiple clean projects from scratch, and the problem is reproducible. I have updated the amplify cli to 4.51.2 and still no luck.
I would expect the CreatePostInput to have the associated inputs for all of the fields, not just comments
(see the autogenerated CreatePostInput below) eg: attribute: AttributeInput
, but these are not generated either. This would allow me to create a Post object that has these custom fields populated. However, only the CommentInput
has been generated. What am I doing wrong here, or have I misunderstood something?
Thanks!
Below is my gql schema and both the autogenerated CreatePostInput and CommentInput:
type Post @model {
id: ID
createdBy: User
createdAt: AWSDateTime
updatedAt:AWSDateTime
type: PostType!
title: String!
status: Status
content: String!
excerpt: String
slug: AWSURL
wordpressId: Int
wordpressImageURLs: [AWSURL]
author: String
likes: Int
tags: [Tag]
attributes: Attribute
seo: Seo
featuredImage: MediaObject
gallery: [MediaObject]
comments: [Comment]
numberOfComments: Int
}
type User @model {
id: ID
username: String!
firstName: String
lastName: String
email: AWSEmail!
avatar: MediaObject
location: String
createdAt: AWSDateTime
updatedAt: AWSDateTime
Posts: [Post]
}
type Tag @model {
id: ID
postIds: [ID]
name: String!
}
type Attribute @model {
id: ID
postId: ID
link: String
imgUrl: AWSURL
ogImage: AWSURL
ogDescription: String
canonicalLink: AWSURL
}
type Seo @model {
id: ID
postId: ID
metaRobotsNoIndex: String
metaRobotsNoFollow: String
canonical: AWSURL
metaDesc: String
opengraphDescription: String
opengraphModifiedTime: AWSDateTime
opengraphPublishedTime: AWSDateTime
opengraphTitle: String
opengraphUrl: AWSURL
opengraphImage: AWSURL
twitterDescription: String
twitterImage: String
twitterTitle: String
schemaSeo: String
}
type Comment
{
id: ID
postId: ID!
createdBy: ID
author: String
createdAt: AWSDateTime
text: String!
likes: Int
}
type MediaObject @model {
id: ID
linkedId: ID
createdBy: ID
mediaItemUrl: AWSURL
srcSet: String
medium: AWSURL
thumb: AWSURL
sourceURL: AWSURL
description: String
bucket: String
region: String
key: String
type: MediaObjectType
}
type Like @model {
id: ID!
objectVotedOnID: ID!
createdBy: ID
createdAt: AWSDateTime
likeOn: LikeOnType
}
enum PostType {
TOOLS
BLOGS
INSPIRATIONS
NEWS
POSTS
NEWSLETTERS
}
enum LikeOnType {
POST
COMMENT
}
enum Status {
PUBLISH
DRAFT
}
enum MediaObjectType {
IMAGE
VIDEO
AUDIO
}
Autogenerated (only CreatePostInput and CommentInput have been included for brevity), found in amplify > backend > api > build > schema.graphql:
input CreatePostInput {
id: ID
createdAt: AWSDateTime
updatedAt: AWSDateTime
type: PostType!
title: String!
status: Status
content: String!
excerpt: String
slug: AWSURL
wordpressId: Int
wordpressImageURLs: [AWSURL]
author: String
likes: Int
comments: [CommentInput]
numberOfComments: Int
}
input CommentInput {
id: ID
postId: ID!
createdBy: ID
author: String
createdAt: AWSDateTime
text: String!
likes: Int
}
2
Answers
To anyone else that notices this strange behaviour: The reason why
CommentInput
is included in theCreatePostInput
, whilst the other custom types are not, is becauseComment
was the only type that I had forgot to label with an @model directive. I assume that this means that AppSync then views this type on thecomments
field as a set of nested fields, rather than an object with its own DynamoDB table. I have asked the AWS Amplify team and will update this answer when I hear more from them.At least for the example, you cited, Post and MediaObject have a one-to-many relationship. That is, a single Post has many MediaObjects. You won’t see the many relations of a one-to-many generated on the parent object for mutations.
When creating a parent object that has children, you’ll need to do this in 2 steps.
Here is some documentation from Amplify on how to do this: https://docs.amplify.aws/cli/graphql-transformer/connection#has-many