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

generate model type for type interchangeability #3323

Closed
hirochachacha opened this issue Feb 3, 2020 · 1 comment · Fixed by #3324
Closed

generate model type for type interchangeability #3323

hirochachacha opened this issue Feb 3, 2020 · 1 comment · Fixed by #3324
Assignees
Labels
feature-request Request a new feature

Comments

@hirochachacha
Copy link
Contributor

Note: If your feature-request is regarding the AWS Amplify Console service, please log it in the
official AWS Amplify Console forum

Is your feature request related to a problem? Please describe.

type Blog @model {
  id: ID!
  name: String!
  posts: [Post] @connection(name: "BlogPosts")
}
type Post @model {
  id: ID!
  title: String!
  blog: Blog @connection(name: "BlogPosts")
  comments: [Comment] @connection(name: "PostComments")
}
type Comment @model {
  id: ID!
  content: String
  post: Post @connection(name: "PostComments")
}

generates:

src/API.ts in TypeScript.

However generated types aren't interchangeable each other.

src/typeCheck.ts:

import * as API from './API'

type CommentList1 = API.GetCommentQuery['getComment'][]
type CommentList2 = API.ListCommentsQuery['listComments']['items']
type CommentList3 = API.OnCreateCommentSubscription['onCreateComment'][]
type CommentList4 = API.GetPostQuery['getPost']['comments']['items']

let c1: CommentList1
let c2: CommentList2
let c3: CommentList3
let c4: CommentList4

c1 = c2
c1 = c3
c1 = c4

c2 = c1
c2 = c3
c2 = c4

c3 = c1
c3 = c2
c3 = c4

c4 = c1
c4 = c2
c4 = c3

then

$ tsc --noEmit src/typeCheck.ts
src/typeCheck.ts:13:1 - error TS2322: Type '{ __typename: "Comment"; id: string; content: string; post: { __typename: "Post"; id: string; title: string; }; }[]' is not assignable to type '{ __typename: "Comment"; id: string; content: string; post: { __typename: "Post"; id: string; title: string; blog: { __typename: "Blog"; id: string; name: string; }; comments: { __typename: "ModelCommentConnection"; nextToken: string; }; }; }[]'.
  Type '{ __typename: "Comment"; id: string; content: string; post: { __typename: "Post"; id: string; title: string; }; }' is not assignable to type '{ __typename: "Comment"; id: string; content: string; post: { __typename: "Post"; id: string; title: string; blog: { __typename: "Blog"; id: string; name: string; }; comments: { __typename: "ModelCommentConnection"; nextToken: string; }; }; }'.
    Types of property 'post' are incompatible.
      Type '{ __typename: "Post"; id: string; title: string; }' is missing the following properties from type '{ __typename: "Post"; id: string; title: string; blog: { __typename: "Blog"; id: string; name: string; }; comments: { __typename: "ModelCommentConnection"; nextToken: string; }; }': blog, comments

13 c1 = c2
   ~~

src/typeCheck.ts:15:1 - error TS2322: Type '{ __typename: "Comment"; id: string; content: string; }[]' is not assignable to type '{ __typename: "Comment"; id: string; content: string; post: { __typename: "Post"; id: string; title: string; blog: { __typename: "Blog"; id: string; name: string; }; comments: { __typename: "ModelCommentConnection"; nextToken: string; }; }; }[]'.
  Property 'post' is missing in type '{ __typename: "Comment"; id: string; content: string; }' but required in type '{ __typename: "Comment"; id: string; content: string; post: { __typename: "Post"; id: string; title: string; blog: { __typename: "Blog"; id: string; name: string; }; comments: { __typename: "ModelCommentConnection"; nextToken: string; }; }; }'.

15 c1 = c4
   ~~

  src/API.ts:500:5
    500     post:  {
            ~~~~
    'post' is declared here.

src/typeCheck.ts:19:1 - error TS2322: Type '{ __typename: "Comment"; id: string; content: string; }[]' is not assignable to type '{ __typename: "Comment"; id: string; content: string; post: { __typename: "Post"; id: string; title: string; }; }[]'.
  Property 'post' is missing in type '{ __typename: "Comment"; id: string; content: string; }' but required in type '{ __typename: "Comment"; id: string; content: string; post: { __typename: "Post"; id: string; title: string; }; }'.

19 c2 = c4
   ~~

  src/API.ts:530:7
    530       post:  {
              ~~~~
    'post' is declared here.

src/typeCheck.ts:22:1 - error TS2322: Type '{ __typename: "Comment"; id: string; content: string; post: { __typename: "Post"; id: string; title: string; }; }[]' is not assignable to type '{ __typename: "Comment"; id: string; content: string; post: { __typename: "Post"; id: string; title: string; blog: { __typename: "Blog"; id: string; name: string; }; comments: { __typename: "ModelCommentConnection"; nextToken: string; }; }; }[]'.
  Type '{ __typename: "Comment"; id: string; content: string; post: { __typename: "Post"; id: string; title: string; }; }' is not assignable to type '{ __typename: "Comment"; id: string; content: string; post: { __typename: "Post"; id: string; title: string; blog: { __typename: "Blog"; id: string; name: string; }; comments: { __typename: "ModelCommentConnection"; nextToken: string; }; }; }'.
    Types of property 'post' are incompatible.
      Type '{ __typename: "Post"; id: string; title: string; }' is missing the following properties from type '{ __typename: "Post"; id: string; title: string; blog: { __typename: "Blog"; id: string; name: string; }; comments: { __typename: "ModelCommentConnection"; nextToken: string; }; }': blog, comments

22 c3 = c2
   ~~

src/typeCheck.ts:23:1 - error TS2322: Type '{ __typename: "Comment"; id: string; content: string; }[]' is not assignable to type '{ __typename: "Comment"; id: string; content: string; post: { __typename: "Post"; id: string; title: string; blog: { __typename: "Blog"; id: string; name: string; }; comments: { __typename: "ModelCommentConnection"; nextToken: string; }; }; }[]'.
  Property 'post' is missing in type '{ __typename: "Comment"; id: string; content: string; }' but required in type '{ __typename: "Comment"; id: string; content: string; post: { __typename: "Post"; id: string; title: string; blog: { __typename: "Blog"; id: string; name: string; }; comments: { __typename: "ModelCommentConnection"; nextToken: string; }; }; }'.

23 c3 = c4
   ~~

  src/API.ts:674:5
    674     post:  {
            ~~~~
    'post' is declared here.


Found 5 errors.

I hit the problem when I tried to use comment list and comment subscription together.

Current workaround is creating model types manually.

export type Blog = {
  __typename: "Blog",
  id: string,
  name: string,
  posts?: ModelPostConnection | null,
};

export type ModelPostConnection = {
  __typename: "ModelPostConnection",
  items?: Array< Post | null > | null,
  nextToken?: string | null,
};

export type Post = {
  __typename: "Post",
  id: string,
  title: string,
  blog?: Blog | null,
  comments?: ModelCommentConnection | null,
};

export type ModelCommentConnection = {
  __typename: "ModelCommentConnection",
  items?: Array< Comment | null > | null,
  nextToken?: string | null,
};

export type Comment = {
  __typename: "Comment",
  id: string,
  content?: string | null,
  post?: Post | null,
};

// test code

type CommentList = Comment[]

let c: CommentList

c = c1
c = c2
c = c3
c = c4

I'd say these types should be automatically generated by amplify-cli.

Describe the solution you'd like

generate model types.

Describe alternatives you've considered

I have no other ideas.

Additional context

hirochachacha added a commit to hirochachacha/amplify-cli that referenced this issue Feb 3, 2020
@hirochachacha hirochachacha changed the title generate model type generate model type for type interchangeability Feb 3, 2020
@UnleashedMind UnleashedMind added feature-request Request a new feature code-gen labels Feb 3, 2020
yuth pushed a commit to hirochachacha/amplify-cli that referenced this issue Jan 20, 2021
yuth added a commit that referenced this issue Jan 20, 2021
Fixes #3323
- accumulate return type of operation
- accumulate object types, interface types and union types
- generate object types, interface types and union types (for TypeScript)

This PR mainly handles TypeScript. Other frontends may have similary issue.



Co-authored-by: Yathi <511386+yuth@users.noreply.github.com>
@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 for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request a new feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants