Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataStore - TypeScript - Type inconsistency #4809

Closed
mlecoq opened this issue Jan 30, 2020 · 4 comments
Closed

DataStore - TypeScript - Type inconsistency #4809

mlecoq opened this issue Jan 30, 2020 · 4 comments
Labels
DataStore Related to DataStore category to-be-reproduced Used in order for Amplify to reproduce said issue

Comments

@mlecoq
Copy link

mlecoq commented Jan 30, 2020

Hi

Here is my types

type Project @model @auth(rules: [{ allow: owner }]) {
  id: ID!
  """
  Title of the project.
  """
  title: String!
  """
  Description of the project
  """
  description: String
  """
  Reports of the project
  """
  reports: [Report] @connection(name: "ProjectReports")
}

type Report @model @auth(rules: [{ allow: owner }]) {
  id: ID!
  """
  Number of the report.
  """
  number: String
  """
  Title of the report
  """
  title: String
  """
  Project
  """
  project: Project @connection(name: "ProjectReports")
}

I am using CRA with typescript template on client side.

I observe Report in order to add it to my list when I create a new one:

  const [project, setProject] = useState<Project>();
  const [reports, setReports] = useState<Report[]>();

  useEffect(() => {
    (async () => {
      if (projectId) {
        const project = await DataStore.query(Project, projectId);

        const reports = (await DataStore.query(Report)).filter(
          r => r?.project?.id === projectId
        );

        setProject(project);
        setReports(reports);
      }
    })();
  }, [projectId]);

  useEffect(() => {
    const subscription = DataStore.observe(Report).subscribe(async msg => {
      if (msg.element.project?.id === projectId && projectId) {
        const project = await DataStore.query(Project, projectId);
        setProject(project);
      }
    });

    return () => {
      subscription.unsubscribe();
    };
  }, [projectId]);

Generated models are

export declare class Project {
  readonly id: string;
  readonly title: string;
  readonly description?: string;
  readonly reports?: Report[];
  constructor(init: ModelInit<Project>);
  static copyOf(source: Project, mutator: (draft: MutableModel<Project>) => MutableModel<Project> | void): Project;
}

export declare class Report {
  readonly id: string;
  readonly number?: string;
  readonly title?: string;
  readonly project?: Project;
  constructor(init: ModelInit<Report>);
  static copyOf(source: Report, mutator: (draft: MutableModel<Report>) => MutableModel<Report> | void): Report;
}

It does not work because msg.element is not a Report with a project key. When I log the element received in msg, I have

{
  title: "test",
  id: "39925af0-4422-4268-b76a-2f3f65212645",
  _version: undefined,
  _lastChangedAt: undefined,
  _deleted: undefined,
  reportProjectId: "ae48642f-da66-412c-9a44-dc3dcbbd1925"
}

I can cast the type to read reportProjectId but it would be more convenient to have correct type.

Thanks

@mlecoq mlecoq added the to-be-reproduced Used in order for Amplify to reproduce said issue label Jan 30, 2020
@elorzafe elorzafe added the DataStore Related to DataStore category label Jan 30, 2020
@sammartinez
Copy link
Contributor

@mlecoq I do see that you have an issue open for another TypeScript callout. Do you want to merge these two issues together? I believe it'll be the same callout as in the other issue that are current TypeScript support is 3.6.4

@mlecoq
Copy link
Author

mlecoq commented Feb 19, 2020

With 3.6.4, I have still this issue (like in #4827)

@sammartinez
Copy link
Contributor

This issue was resolved with the above Pull Request being merged in. Resolving issue

@github-actions
Copy link

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
DataStore Related to DataStore category to-be-reproduced Used in order for Amplify to reproduce said issue
Projects
None yet
Development

No branches or pull requests

3 participants