7

Apollo client's codegen adds | null in the generated types, and I don't understand why they are there and how to get rid of them.

I see no reason why the API would return an array of null, so I don't want to check in my code weather the oject is null or not everytime.

Offending generated types from apollo codegen:

export interface MusicGenres_musicGenres {
  name: string;
}

export interface MusicGenres {
  musicGenres: (MusicGenres_musicGenres | null)[];
                                        ^^^^^^
                                         WHY ?
}

My Graphql Schema:

type Query {
  musicGenres: [MusicGenre]!
}

type MusicGenre {
  id: ID!
  name: String!
}

Query in my TypeScript code from which are generated the types:

gql`
  query MusicGenres {
    musicGenres { name }
  }
`
Paulin Trognon
  • 762
  • 6
  • 17

1 Answers1

8

In your schema, you have the following field definition:

musicGenres: [MusicGenre]!

This means that while musicGenres will be a list and will itself never be null, any item in the list could be null. If you want to indicate that all items in the list are also non-nullable, your field definition should instead be:

musicGenres: [MusicGenre!]!

See this post for an extended explanation.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183