简体   繁体   中英

Property 'id' is missing in type {`id`} using GraphQL and Typescript

I am new to coding and Typescript in general so pardon my lack of knowledge. I am trying to get my application to talk to my api, but I get the following error because of my primary id key having a "?" as follows:

export interface PartMaster {
    id?: number;
 }

When I remove the "?" I get a different error and when I keep it I also get a different error. Am I missing something in GraphQL? I am mostly front end developer, and my coworker has done all of the GraphQL management, but I need to figure this out on my own today so any help would be appreciated.

Here is the code it does not like when I have the "?"in id:

computed: {
    part(): PartMaster {
      return {
        partNumber: this.partNumber,
        
      }
    }
  },

The error reads property 'id' missing in type partNumber

This is the error when I include the "?":

editRow(partMaster.id)
editRow(part.id)

It underlines the partmaster.id and the part.id with the following error:

Argument of type 'number | undefined' is not assignable to parameter of type 'number'. Type 'undefined' is not assignable to type 'number'.

This question really needs more details, including the sections where the editRow function is defined and where your GraphQL query is called, and the text of the error when id is not marked as optional. However, I don't yet have enough reputation to comment so will address this in an answer instead.

With the information given, here is what I can guess:

  • The last error messages that you give ( Argument of type 'number | undefined' is not assignable to parameter of type 'number'. Type 'undefined' is not assignable to type 'number'. ) are likely because editRow expects an argument of type number . By marking id as optional, you indicate that it may sometimes be undefined . Either don't mark it as optional, or make editRow accept number | undefined number | undefined and handle the undefined case in code.
  • In the case where you remove the "?"from id, what I would check first is that your GraphQL query does in fact request the id field. The point of GraphQL is to only request what you need so it is possible that your coworker has not added this field to the request. If it's not that, I wouldn't be able to help without the extra details mentioned above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM