AppSync DynamoDB types not working for generated create function

82 Views Asked by At

I have been working on a simple API and decided to use AWS CDK , AppSync, Amplify and DynamoDB to get what I needed working, all has been going well, until I've hit a brick wall with a typing issue.

my schema is as follows

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!
}

my table is set as follows:

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,
    },
  });
};

I have generated all the types using amplify codegen so in my resolver code I have the following operation

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;
};

I can see that the given type does not contain a reference for __typename but as this is my sk is there a way to get this to work?

the error I get for reference is

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

There are 1 best solutions below

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

this was my solution