typescript AppSync DynamoDB类型不适用于生成的创建函数

ruoxqz4g  于 7个月前  发布在  TypeScript
关注(0)|答案(1)|浏览(77)

我一直在开发一个简单的API,并决定使用AWS CDK、AppSync、Amplify和DynamoDB来实现我需要的功能,一切都很顺利,直到我遇到了一个打字问题。
我的模式如下

type Client {
  id: ID!
  name: String!
  customers: [Customer]!
}

type Customer {
  id: ID!
  clientId: String!
  name: String!
}

input CreateClientInput {
  id: ID
  name: String!
  customers: [CreateCustomerInput]
}

input CreateCustomerInput {
  id: ID
  clientId: String!
  name: String!
}

字符串
我的表如下:

import { AttributeType, BillingMode, Table } from "aws-cdk-lib/aws-dynamodb";
import { Construct } from "constructs";

type TableProps = {
  scope: Construct;
  tableName: string;
};

export const createClientsTable = (props: TableProps) => {
  const { scope, tableName } = props;
  return new Table(scope, tableName, {
    billingMode: BillingMode.PAY_PER_REQUEST,
    partitionKey: {
      name: "id",
      type: AttributeType.STRING,
    },
    sortKey: {
      name: "__typename",
      type: AttributeType.STRING,
    },
  });
};


我已经使用amplify codegen生成了所有类型,因此在解析器代码中,我有以下操作

import * as ddb from "@aws-appsync/utils/dynamodb";
import { Context, util } from "@aws-appsync/utils";
import { Client, CreateClientMutationVariables } from "../src/generatedTypes";

export const request = (ctx: Context<CreateClientMutationVariables>) => {
  return ddb.put({
    key: { __typname: "Client", id: util.autoId() },
    item: ctx.args.input,
  });
};

export const response = (ctx: Context) => {
  return ctx.result as Client;
};


我可以看到给定的类型不包含对__typename的引用,但这是我的sk,有没有办法让它工作?
我得到可供参考的错误是

Type '{ __typname: string; id: string; }' is not assignable to type '{ name?: string | undefined; }'.
  Object literal may only specify known properties, and '__typname' does not exist in type '{ name?: string | undefined; }'.

pvcm50d1

pvcm50d11#

export const request = (ctx: Context<CreateClientMutationVariables>) => {
  return ddb.put({
    key: { __typename: "Client", id: util.autoId() } as ddb.DynamoDBKey<Client>,
    item: ctx.args.input,
  });
};

字符串
这是我的解决方案

相关问题