I am using graphql-codegen to generate Typescript typings for my React project. With graphql, you can set up queries to return only the properties that you need. This is useful but also results in pretty complex looking object types for my Typescript definition file. I have a Query
that returns several nested items. I was wondering if it would be possible to extract the typings of just the tags
property?
How would I use Typescript to extract the following type:
tags?: Maybe<Array<Maybe<(
{ __typename?: 'Tag' }
& Pick<Tag, 'id' | 'name'>
)>>>
From this base object type (I've indicated the part of the type I'd like to extract):
type ManageTeamPageQuery = (
{ __typename?: 'Query' }
& { authedTeam?: Maybe<(
{ __typename?: 'Team' }
& Pick<Team, 'id' | 'name' | 'imageEnding' | 'facebookPixelId' | 'subscriptionId'>
& { domains?: Maybe<Array<Maybe<(
{ __typename?: 'Domain' }
& Pick<Domain, 'id' | 'name'>
)>>>, tags?: Maybe<Array<Maybe<( <--------- HERE
{ __typename?: 'Tag' } <----------------- HERE
& Pick<Tag, 'id' | 'name'> <------------- HERE
)>>> }
)> }
);
What's tricky for me is that the nested properties can be null or undefined, so I am not sure how to deeply access them?
Please note that graphql-codegen defines Maybe
as:
export type Maybe<T> = T | null;
For reference, the type generated from ManageTeamPageQuery
results from a graphql that looks like:
query ManageTeamPage($id: Int!) {
authedTeam(id: $id) {
id
name
imageEnding
facebookPixelId
subscriptionId
domains {
id
name
}
tags {
id
name
}
}
}