You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AppSync生成的Create函数与DynamoDB类型不兼容问题求助

AppSync生成的Create函数与DynamoDB类型不兼容问题求助

Hey there! Let's break down what's going on and fix this type issue you're facing.

First, I spot a tiny but critical typo in your resolver code: you wrote __typname instead of __typename in the key object. That's why TypeScript is throwing an error about that property not existing—since it's a misspelling, it doesn't match your table's sort key name.

Beyond the typo, the core issue is that your generated GraphQL types don't include __typename (it's not part of your schema), but your DynamoDB table uses it as the sort key. So we need to adjust both the resolver logic and the types to account for this.

Here's how to fix everything step by step:

1. Correct the Typo & Fix the Key/Item Structure

Your resolver needs to include __typename in both the key and the item (DynamoDB requires all key attributes to be present in the item). Also, make sure the id in the key matches the one in the item:

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

// Extend the generated Client type to include our sort key __typename
type ClientWithTypename = Client & { __typename: "Client" };

export const request = (ctx: Context<CreateClientMutationVariables>) => {
  // Use the provided id or auto-generate one if missing
  const id = ctx.args.input.id || util.autoId();

  // Build the complete item with __typename and consistent id
  const item: ClientWithTypename = {
    ...ctx.args.input,
    __typename: "Client",
    id,
  } as ClientWithTypename;

  return ddb.put({
    key: { id, __typename: "Client" }, // Correct key with both partition and sort keys
    item,
  });
};

export const response = (ctx: Context) => {
  // Cast the result to our extended type to include __typename
  return ctx.result as ClientWithTypename;
};

2. Why This Works

  • Typo Fix: __typename now matches your table's sort key name, so TypeScript stops complaining about an unknown property in the key.
  • Extended Type: By creating ClientWithTypename, we're telling TypeScript that our Client items in DynamoDB include the __typename field, aligning with our table schema while keeping type safety.
  • Item Completeness: We added __typename to the item because DynamoDB requires all key attributes (partition and sort) to be present when writing to the table.

Additional Notes

  • If you prefer a quicker (less type-safe) fix, you could use as any for the key and item, but the extended type approach is better for long-term maintainability.
  • When fetching items later, remember that __typename will be present in DynamoDB responses, so you can extend your query types similarly if needed.

That should resolve the type error and get your CreateClient mutation working correctly with your single-table DynamoDB setup.

备注:内容来源于stack exchange,提问作者Deviland

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 09:04:48