-
Hey there, Iam currently running into issues with autocompletion. my codegen config is like so:
and i have the following query:
But in the auto completion only id and slug are available... In the genereated code it seams that all types are generated for "entries" and not only the type for the specific query. Anyone has an idea how to solve this? Or is this a bug? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
For everybody struggling with the same problem, one possible solution is to use type guards and type predicates to narrow down the type: https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates maybe that helps somebody |
Beta Was this translation helpful? Give feedback.
-
Hi, could you please give the full example of how you extracted the correct type from the generated type? |
Beta Was this translation helpful? Give feedback.
-
I ended up using this: const WORKSPACES_PAGE_QUERY = gql`
query WorkspacePage($workspaceId: ID!) {
workspace: node(id: $workspaceId) {
... on Workspace {
id
name
}
}
}
`;
type GetDeepProp<T extends object, Key extends string> = Key extends keyof T
? T[Key]
: {
[k in keyof T]: T[k] extends object
? GetDeepProp<T[k], Key>
: unknown
}[keyof T]
const { data } = useSuspenseQuery<WorkspacePageQuery>(WORKSPACES_PAGE_QUERY, { variables: { workspaceId: params.workspaceId } });
type Workspace = Extract<GetDeepProp<WorkspacePageQuery, 'workspace'>, { __typename?: "Workspace" }>;
const workspace = data.workspace as Workspace; |
Beta Was this translation helpful? Give feedback.
But it also works with autocompletion. The best way of checking the type would be;