amplify-category-api icon indicating copy to clipboard operation
amplify-category-api copied to clipboard

I want the props type of the data CRUD function.

Open rnrnstar2 opened this issue 1 year ago • 3 comments
trafficstars

Environment information

System:
  OS: macOS 14.0
  CPU: (10) arm64 Apple M2 Pro
  Memory: 157.41 MB / 16.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 20.5.0 - /usr/local/bin/node
  Yarn: 1.22.19 - /usr/local/bin/yarn
  npm: 9.8.0 - /usr/local/bin/npm
  pnpm: 8.15.5 - ~/Library/pnpm/pnpm
NPM Packages:
  @aws-amplify/backend: Not Found
  @aws-amplify/backend-cli: Not Found
  aws-amplify: Not Found
  aws-cdk: Not Found
  aws-cdk-lib: Not Found
  typescript: Not Found
AWS environment variables:
  AWS_PROFILE = cloudteam
  AWS_DEFAULT_PROFILE = cloudteam
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Description

export const updateTeam = async (input: any) => {
  const { data: updatedTeam, errors } = await client.models.Team.update(input);
  if (errors) console.error(errors);
  return updatedTeam;
};

A function file is created externally. At that time, it is not possible to specify the type of props, so I would like to be able to do so.

rnrnstar2 avatar Apr 10 '24 10:04 rnrnstar2

Hi - we're working on this as we speak. Our current thought is to update the clientschema type to include more types in general:

type CreateTodoInput = Schema["Todo"]["createArgs"]
type UpdateTodoInput = Schema["Todo"]["updateArgs"]
type DeleteTodoInput = Schema["Todo"]["deleteArgs"]

renebrandel avatar Apr 12 '24 15:04 renebrandel

Thank you for working on this issue. The type of setup you guys are thinking of is very nice. I think having those type definitions will greatly improve the development experience.

rnrnstar2 avatar Apr 15 '24 02:04 rnrnstar2

import { useEffect, useState } from "react";
import { generateClient } from "aws-amplify/data";
import { type Schema } from "@shared-backend/amplify/data/resource";
type Team = Schema["Team"];

const client = generateClient<Schema>();

// GET
export const useTeam = (id: string) => {
  // state
  const [team, setTeam] = useState<Team>();
  useEffect(() => {
    const func = async () => {
      if (!id) return;
      const res = await client.models.Team.get({ id });
      const team = res.data as Team;
      const errors = res.errors;
      if (errors.length > 0) {
        console.error("errors: ", errors);
        return;
      } else {
        console.log(team);
        setTeam(team);
      }
    };
    func();
  }, [id]);

  return team;
};

I would like to see the development of the type of data when obtained using get. If the type is not defined, the following error will occur.

An inferred type of 'useTeam' cannot be named without a reference to '../../../node_modules/@aws-amplify/data-schema/lib-esm/src/Authorization' you can't. This may not be portable. Requires type annotation. ts(2742)
An inferred type of 'useTeam' cannot be named without a reference to '../../../node_modules/@aws-amplify/data-schema/lib-esm/src/RefType' you can't. This may not be portable. Requires type annotation. ts(2742)
Exported variable 'useTeam' has or uses name '__auth' in external module "/Users/rnrnstar/github/Oripa/node_modules/@aws-amplify/data-schema/lib-esm/src/RefType" However, you cannot specify the name. ts(4023)

rnrnstar2 avatar Apr 15 '24 02:04 rnrnstar2