![](/img/trans.png)
[英]GraphQL AWS React Can't resolve value type mismatch expected type LIST
[英]typescript can't resolve typing from graphql query that returns a union type
我想了解這里發生了什么,並且由於我對 typescript 不是很有經驗,因此將不勝感激任何幫助!
我有一個解析器從數據庫返回一個成本:
@Query(() => FixedOrVariableCost, { name: 'oneCost' })
async getOneCost(
@Args('id', { type: () => String }) id: MongooseSchema.Types.ObjectId
) {
return this.costsService.getOneCost(id);
}
它的返回類型 FixedOrVariableCost 是一個聯合類型:
const FixedOrVariableCost = createUnionType({
name: 'FixedOrVariableCost',
types: () => [FixedCost, VariableCost] as const,
});
基本上在我的 model 中,成本是固定的或可變的。 Cost 是我的 graphql 層中的一個抽象接口。
生成的架構:
union FixedOrVariableCost = FixedCost | VariableCost
oneCost(id: String!): FixedOrVariableCost!
這是我正在使用的查詢:
const useGetOneCost = (id: string) =>
useQuery(['getOneCost', id], async () => {
return (
graphQLClient.request(
gql`
query GetOneCost($id: String!) {
oneCost(id: $id) {
... on Cost {
_id
kind
code
label
title
content
costType {
_id
code
label
}
costMargin {
_id
code
label
}
createdAt
createdBy {
_id
}
updatedAt
updatedBy {
_id
}
status {
_id
code
label
color
}
}
... on FixedCost {
fixedCostValue {
amount
currency {
_id
code
label
symbol
}
}
}
... on VariableCost {
variableCostValue
}
}
}
`,
{ id }
),
{
enabled: !!id,
}
);
});
graphql codegen 生成的類型如下:
export type GetOneCostQuery = {
__typename?: 'Query';
oneCost:
| {
__typename?: 'FixedCost';
_id: string;
kind: CostKind;
code: string;
label: string;
title?: string | null;
content?: string | null;
createdAt: any;
updatedAt: any;
costType: {
__typename?: 'CostType';
_id: string;
code: string;
label: string;
};
costMargin: {
__typename?: 'CostMargin';
_id: string;
code: string;
label: string;
};
createdBy: { __typename?: 'User'; _id: string };
updatedBy: { __typename?: 'User'; _id: string };
status: {
__typename?: 'GenericStatus';
_id: string;
code: string;
label: string;
color: string;
};
fixedCostValue: {
__typename?: 'FixedCostValue';
amount: number;
currency: {
__typename?: 'Currency';
_id: string;
code: string;
label: string;
symbol: string;
};
};
}
| {
__typename?: 'VariableCost';
_id: string;
kind: CostKind;
code: string;
label: string;
title?: string | null;
content?: string | null;
createdAt: any;
updatedAt: any;
variableCostValue: number;
costType: {
__typename?: 'CostType';
_id: string;
code: string;
label: string;
};
costMargin: {
__typename?: 'CostMargin';
_id: string;
code: string;
label: string;
};
createdBy: { __typename?: 'User'; _id: string };
updatedBy: { __typename?: 'User'; _id: string };
status: {
__typename?: 'GenericStatus';
_id: string;
code: string;
label: string;
color: string;
};
};
};
非常好 ! 打字生成按預期工作
現在,我的一個組件接收成本作為道具,所以我在GetOneCostQuery
類型中選擇oneCost
type PropType<TObj, TProp extends keyof TObj> = TObj[TProp];
type OneCost = PropType<GetOneCostQuery, 'oneCost'>;
type Props = {
cost: OneCost;
};
export const MyComponent = ({
cost
}: Props) => {
console.log(cost.fixedCostValue);
return null;
}
``fixedCostValue 上的 ts 錯誤:
La propriété 'fixedCostValue' n'existe pas sur le type '{ __typename?: "FixedCost" | undefined; _id: string; kind: CostKind; code: string; label: string; title?: string | null | undefined; content?: string | null | undefined; createdAt: any; ... 6 more ...; fixedCostValue: { ...; }; } | { ...; }'.
La propriété 'fixedCostValue' n'existe pas sur le type '{ __typename?: "VariableCost" | undefined; _id: string; kind: CostKind; code: string; label: string; title?: string | null | undefined; content?: string | null | undefined; createdAt: any; ... 6 more ...; status: { ...; }; }'.ts(2339)
大致用英語:“blablabla 類型不存在屬性‘fixedCostValue’”
如果成本是固定的,這個值不應該是可用的嗎? 為什么 typescript 不能識別這個值? 這讓我很困惑...
謝謝你的幫助
類型OneCost
是一個聯合,這意味着它是兩個對象之一 - FixedCost
或VariableCost
。 雖然第一個包含一個名為fixedCostValue
的屬性,但第二個沒有,因此出現錯誤。
如果您檢查 object 中是否存在此屬性,它將通過。 例如:
export const MyComponent = ({
cost
}: Props) => {
if ("fixedCostValue" in cost) {
console.log(cost.fixedCostValue);
};
return null;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.