GraphQL AppSync 的自动生成的变更输入中缺少对象字段
Object Fields Missing in Autogenerated Mutation Input for GraphQL AppSync
我有一个自定义 GraphQL 架构,用于我的 ReactJS 应用程序的博客应用程序。主要模型类型为Post.
Post 包含许多具有自定义类型的属性,例如图库:[MediaObject]。大多数具有自定义类型的属性在 AWS Amplify CLI 自动生成的 CreatePostInput 中缺失。这意味着我无法创建具有这些自定义类型属性的 Post 对象。
AppSync 控制台中的查询下显然缺少这些自定义类型属性。在资源管理器中,转到 Mutations,单击 CreatePost mutation,您将看到 Post 的 21 个属性中有 6 个丢失。缺少的属性是标签、属性、seo、featuredImage、gallery 和 createdBy。
它们在 api > build > schema.graphql 中自动生成的 graphql 模式中也丢失了。
我从头开始做了多个clean项目,问题重现。我已经将 amplify cli 更新到 4.51.2,但仍然没有成功。
我希望 CreatePostInput 具有所有字段的关联输入,而不仅仅是 comments
(请参阅下面自动生成的 CreatePostInput)例如:attribute: AttributeInput
,但这些也不会生成。这将允许我创建一个填充了这些自定义字段的 Post 对象。但是,只生成了 CommentInput
。我哪里做错了,或者我误解了什么?
谢谢!
下面是我的 gql 架构以及自动生成的 CreatePostInput 和 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
}
自动生成(为简洁起见,仅包含 CreatePostInput 和 CommentInput),可在 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
}
至少对于您引用的示例,Post 和 MediaObject 具有一对多关系。也就是说,单个 Post 具有多个 MediaObject。您不会看到在父对象上为突变生成的一对多的许多关系。
创建具有子对象的父对象时,您需要分两步完成此操作。
- 创建父对象,return ID。
- 使用parentId,创建N个子对象。
以下是 Amplify 提供的有关如何执行此操作的一些文档:https://docs.amplify.aws/cli/graphql-transformer/connection#has-many
对于注意到这种奇怪行为的任何其他人:CommentInput
包含在 CreatePostInput
中而其他自定义类型不包含的原因是因为 Comment
是唯一的我忘记用@model 指令标记的类型。我假设这意味着 AppSync 然后将 comments
字段上的此类型视为一组嵌套字段,而不是具有自己的 DynamoDB table 的对象。我已经问过 AWS Amplify 团队,当我从他们那里听到更多信息时,我会更新这个答案。
我有一个自定义 GraphQL 架构,用于我的 ReactJS 应用程序的博客应用程序。主要模型类型为Post.
Post 包含许多具有自定义类型的属性,例如图库:[MediaObject]。大多数具有自定义类型的属性在 AWS Amplify CLI 自动生成的 CreatePostInput 中缺失。这意味着我无法创建具有这些自定义类型属性的 Post 对象。
AppSync 控制台中的查询下显然缺少这些自定义类型属性。在资源管理器中,转到 Mutations,单击 CreatePost mutation,您将看到 Post 的 21 个属性中有 6 个丢失。缺少的属性是标签、属性、seo、featuredImage、gallery 和 createdBy。
它们在 api > build > schema.graphql 中自动生成的 graphql 模式中也丢失了。
我从头开始做了多个clean项目,问题重现。我已经将 amplify cli 更新到 4.51.2,但仍然没有成功。
我希望 CreatePostInput 具有所有字段的关联输入,而不仅仅是 comments
(请参阅下面自动生成的 CreatePostInput)例如:attribute: AttributeInput
,但这些也不会生成。这将允许我创建一个填充了这些自定义字段的 Post 对象。但是,只生成了 CommentInput
。我哪里做错了,或者我误解了什么?
谢谢!
下面是我的 gql 架构以及自动生成的 CreatePostInput 和 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
}
自动生成(为简洁起见,仅包含 CreatePostInput 和 CommentInput),可在 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
}
至少对于您引用的示例,Post 和 MediaObject 具有一对多关系。也就是说,单个 Post 具有多个 MediaObject。您不会看到在父对象上为突变生成的一对多的许多关系。
创建具有子对象的父对象时,您需要分两步完成此操作。
- 创建父对象,return ID。
- 使用parentId,创建N个子对象。
以下是 Amplify 提供的有关如何执行此操作的一些文档:https://docs.amplify.aws/cli/graphql-transformer/connection#has-many
对于注意到这种奇怪行为的任何其他人:CommentInput
包含在 CreatePostInput
中而其他自定义类型不包含的原因是因为 Comment
是唯一的我忘记用@model 指令标记的类型。我假设这意味着 AppSync 然后将 comments
字段上的此类型视为一组嵌套字段,而不是具有自己的 DynamoDB table 的对象。我已经问过 AWS Amplify 团队,当我从他们那里听到更多信息时,我会更新这个答案。