【问题标题】:Object Fields Missing in Autogenerated Mutation Input for GraphQL AppSyncGraphQL AppSync 的自动生成的变异输入中缺少对象字段
【发布时间】:2021-08-11 23:25:49
【问题描述】:

我有一个自定义 GraphQL 架构,用于我的 ReactJS 应用程序的博客应用程序。主要模型类型为 Post。

Post 包含许多具有自定义类型的属性,例如 Gallery: [MediaObject]。 AWS Amplify CLI 自动生成的 CreatePostInput 中缺少大多数具有自定义类型的属性。这意味着我无法创建具有自定义类型的这些属性的 Post 对象。

这些自定义类型属性的缺失在 AppSync 控制台的查询下很明显。在资源管理器中,转到 Mutations,单击 CreatePost 突变,您将看到 Post 的 21 个属性中有 6 个丢失。缺少的属性是标签、属性、seo、featuredImage、gallery 和 createdBy。

react 应用程序的 api > build > schema.graphql 中自动生成的 graphql 架构中也缺少它们。

我从头开始做了多个干净的项目,问题是可重现的。我已将放大 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
}

【问题讨论】:

    标签: reactjs amazon-web-services graphql aws-amplify


    【解决方案1】:

    至少对于您引用的示例, Post 和 MediaObject 具有一对多的关系。也就是说,一个 Post 有许多 MediaObject。您不会看到在父对象上为突变生成的一对多关系。

    创建具有子对象的父对象时,您需要分两步完成。

    1. 创建父对象,并返回id。
    2. 使用 parentId,创建 N 个子对象。

    以下是 Amplify 提供的有关如何执行此操作的一些文档:https://docs.amplify.aws/cli/graphql-transformer/connection#has-many

    【讨论】:

    • 好的,非常感谢您告诉我!知道为什么 seoattributesfeaturedImage 不存在吗?
    • 我没有看到你的任何关系被宣布。查看docs.amplify.aws/cli/graphql-transformer/connection 并为您的特定用例连接适当的连接/密钥。
    • 好的,感谢您的指针,但为什么 cmets 数组包含在 CreatePostInput(作为 CommentInput)中,而其他数组却没有?这就是让我困惑的地方:)
    【解决方案2】:

    对于注意到这种奇怪行为的其他人:CommentInput 包含在 CreatePostInput 中而其他自定义类型不包含的原因是因为 Comment 是我忘记标记的唯一类型@model 指令。我假设这意味着 AppSync 然后将 comments 字段上的这种类型视为一组嵌套字段,而不是具有自己的 DynamoDB 表的对象。我已询问 AWS Amplify 团队,并会在收到更多来自他们的消息时更新此答案。

    【讨论】:

      猜你喜欢
      • 2020-06-12
      • 2020-05-02
      • 2019-09-20
      • 2019-01-23
      • 2020-04-25
      • 2022-10-31
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      相关资源
      最近更新 更多