Typescript-operation: Return types of a Query not as seperate type #5379
-
Hi, iam using the typescript, typescript-operation and typescript-react-apollo plugin. My Problem is the generation of the Operation query types: query GetDeviceList {
devices {
id
name
description
subDevices {
type
... on SubDeviceA {
a
}
... on SubDeviceB {
a
b
}
... on SubDeviceC{
y
}
}
}
}
export type GetDeviceList = {
devices?: Maybe<
Array<
Pick<
Device,
"id" | "name" | "description"
> & {
subDevices?: Maybe<
Array<
Maybe<
| Pick<
SubDeviceA, "a" | "type"
>
| Pick<
SubDeviceB,
"a" | "b" | "type"
>
| Pick<
SubDeviceC,
"y" | "type"
>
>
>
>;
}
>
>;
}; In my react component iam not able to direct access the "y"-property without casting. const { data } = useGetDeviceList()
const subDeviceCs = data?.subDevices?.filter(d => d.type === SubDevice.TYPE_C);
// Rendering
{
subDevicesCs.map(d => (<span>{ d?.y }</span>)). // <=== Iam not able to access the property y
} My current solution is to cast the const subDeviceCs = data?.subDevices?.filter(d => d.type === SubDevice.TYPE_C) as SubDeviceC[]; But is it possible to generate the Query-types instead with Pick<..> | Pick<...> with an exported named type? e.g // generated types
export const OnSubDeviceC = Pick<
SubDeviceC,
"y" | "type"
>
export type GetDeviceList = {
...
subDevices?: Maybe<
Array<
Maybe<
| OnSubDeviceA
| OnSubDeviceB
| OnSubDeviceC
>
...
};
// in react component
const subDeviceCs = data?.subDevices?.filter(d => d.type === SubDevice.TYPE_C) as OnSubDeviceC[]; |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
You should not use |
Beta Was this translation helpful? Give feedback.
-
export type DeviceA = {
__typename: string;
a: string;
b: string;
}
export type DeviceB = {
__typename: string;
b: string;
otherAttributesNotInActualQuery: string;
otherAttributesNotInActualQuery2: string;
}
export type GetDeviceList = {
devices: Array<Pick<DeviceA, 'a' | '__typename'> | Pick<DeviceB, 'b' | '__typename'>>;
}
function foot(): GetDeviceList {
return {
devices: [{
__typename: 'DeviceA',
a: 'test',
b: 'test'
}, {
__typename: 'DeviceB',
b: 'test device b'
}]
}
}
const result = foot();
result.devices.filter(d => d.__typename === 'DeviceB').forEach(d => console.log(d.b)); // <== Cannot access b I must cast to DeviceB but than iam lose the information about which attributes i have queried e.g only property 'b' from DeviceB |
Beta Was this translation helpful? Give feedback.
You should not use
type
, you should use the built-in field called__typename
, it will allow you to discriminate the types.