Apollo client's codegen adds unwanted "or null" in my types
Asked Answered
L

1

8

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 }
  }
`
Lubricant answered 10/5, 2020 at 9:23 Comment(0)
T
9

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.

Titrant answered 10/5, 2020 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.