【问题标题】:Relationships with AwsCdk, DynamoDB and AppSync - Typescript and lambda functions与 AwsCdk、DynamoDB 和 AppSync 的关系 - Typescript 和 lambda 函数
【发布时间】:2021-06-16 07:28:42
【问题描述】:

我们目前正在研究堆栈:cdk、appsync 和 amplify 以迁移我们的应用程序。

在我们最初的测试中,我们能够上传一个只有 appsync 和 wit 关系的 graphql api,它非常流畅、漂亮和快速。

在测试使用 cdk 构建时,我们很难创建关系。

这是我的代码:

架构

type Person {
  id: ID!
  name: String!
}
input PersonInput {
  id: ID!
  name: String!
}
input UpdatePersonInput {
  id: ID!
  name: String
}
type Client {
  id: ID!
  type: String!
  personId: String
  # Person: PersonConnection 
  Person: Person @connection(fields: ["personId"])
}
input ClientInput {
  id: ID!
  type: String!
  personId: String!
}
input UpdateClientInput {
  id: ID!
  type: String
  personId: String
}

我的功能

const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();
async function list() {
    const params = {
        TableName: process.env.CLIENT_TABLE,
    }
    try {
        const data = await docClient.scan(params).promise()
        return data.Items
    } catch (err) {
        console.log('DynamoDB error: ', err)
        return null
    }
}
export default list;

我的桌子

const clientTable = new dynamodb.Table(scope, 'ClientTable', {
        billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
        partitionKey: {
            name: 'id',
            type: dynamodb.AttributeType.STRING,
        },
    });
    clientTable.addGlobalSecondaryIndex({
        indexName: 'client-by-person-id',
        partitionKey: {
        name: 'personId',
        type: dynamodb.AttributeType.STRING
        },
        sortKey: {
        name: 'createdAt',
        type: dynamodb.AttributeType.STRING
        }
    })

我的查询

query MyQuery {
  listClients {
    id
    personId
    type
    Person {
      name
    }
  }
}

但是,我返回的 Person 连接是 null

"listClients": [
      {
        "id": "1",
        "personId": "1",
        "type": "PJ",
        "Person": null
      }
    ]

如果能指出我们的错误,我将不胜感激

【问题讨论】:

    标签: graphql aws-appsync aws-cdk


    【解决方案1】:

    根据 Thorsten 的响应解决问题。

    1. 首先,在客户端的Person字段中添加resolver

      export const clientResolvers = [{ typeName: "Client", fieldName: "Person" },...]

      clientResolvers.map(((resolver: clientTypeResolver) => dataSource2.createResolver(resolver)))

    2. 将函数映射到其 lambda 函数中的 Person 字段

      type AppSyncEvent = {
          ...
          source: {personId: string,}
          ...
      }
      
      exports.handler = async (event:AppSyncEvent) => {
          switch (event.info.fieldName) {
              ...
              case "Person":
              return await getPerson(event.source.personId);
          }
      }```
      
      
    3. 解决person字段的函数

    async function getPerson(personId: string) {
        console.log("CONTEXT\n" + JSON.stringify(personId, null, 2))
        // console.log(context.source)
        const params = {
            TableName: process.env.PERSON_TABLE,
            Key: { id: personId }
        }
        try {
            const { Item } = await docClient.get(params).promise()
            console.log("DATA\n" + JSON.stringify(Item, null, 2))
            return Item
        } catch (err) {
            console.log('DynamoDB error: ', err)
        }
    

    【讨论】:

      猜你喜欢
      • 2019-07-27
      • 1970-01-01
      • 2020-08-30
      • 2019-10-19
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      相关资源
      最近更新 更多