簡體   English   中英

如何建立從用戶池到 aws appsync 模型的數據關系

[英]How to make a relationship of data from user pool to aws appsync model

我是放大和應用同步的新手。 我正在嘗試創建一個 Post 模型,該模型需要與創建 Post 的用戶建立關系。 問題是我的用戶來自 cognito 用戶池。

我想要的是來自 cognito 的用戶 我不想在 dynamo db 上創建新的用戶表,因為 cognito 用戶池已經有它的信息 我只想在查詢帖子時獲取創建該帖子的用戶信息。

我應該如何建立這種關系?

我像這樣創建 amlify api

? Please select from one of the below mentioned services: GraphQL
? Choose the default authorization type for the API API key
? Enter a description for the API key: 
? After how many days from now the API key should expire (1-365): 7
? Do you want to configure advanced settings for the GraphQL API Yes, I want to make some additional changes.
? Configure additional auth types? Yes
? Choose the additional authorization types you want to configure for the API Amazon Cognito User Pool
Cognito UserPool configuration
Use a Cognito user pool configured as a part of this project.
? Configure conflict detection? No

這是我當前的 schema.grapql

type Post
    @model
    @versioned
    @aws_cognito_user_pools
    @auth(rules: [{ allow: owner, queries: null }, { allow: public }]) {
    id: ID!
    title: String!
    content: String
    thumb: String
    slug: String!
    allow_comments: Boolean
    owner: String!
    post_type: String!
    add_to_nav: Boolean!
    version: Int!
    comments: [Comment] @connection(name: "PostComments")
}

type Comment
    @model
    @versioned
    @aws_cognito_user_pools
    @auth(rules: [{ allow: owner, queries: null }]) {
    id: ID!
    content: String
    version: Int!
    post: Post @connection(name: "PostComments")
}

================================================ ==

編輯:添加了我想要的數據結果

這是我要執行的查詢

query ListPost {
  listPosts {
    items {
      title
      content
      owner{
          username
          id
          email
          first_name
          last_name
        }
      }
    }
  }
}

我想要的結果

{
  "data": {
    "listPosts": {
      "items": [
        {
          "title": "title 1"
          "content": "Test long text cotent"
          "owner": {
              "username": "user1"
              "id": "234234234"
              "email": "user1@test.com"
              "first_name": "John"
              "last_name": "Doe"
           }
        },
       {
          "title": "title 1"
          "content": "Test long text cotent"
          "owner": {
              "username": "user1"
              "id": "234234234"
              "email": "user1@test.com"
              "first_name": "John"
              "last_name": "Doe"
           }
        },
      ]
    }
  }
}

我找不到任何文檔如何構建這樣的東西。

這應該對正在考慮這樣做的其他人有所幫助:

https://docs.amplify.aws/cli-legacy/graphql-transformer/function/#usage

滾動至:

示例:從 Amazon Cognito 用戶池獲取登錄用戶

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM