我一直在开发一个简单的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; }'.
型
1条答案
按热度按时间pvcm50d11#
字符串
这是我的解决方案