我是使用Typescript的新手,发现从GraphQL模式处理类型的方式与通过Relay生成的类型不匹配。
这里有一个例子:
# in schema.graphql
"""
columns and relationships of "businesses"
"""
type businesses implements Node {
businessId: bigint!
name: String!
}
// in __generated__/Business_business.graphql
export type Business_business = {
readonly name: string;
readonly businessId: unknown;
readonly " $refType": "Business_business";
};
export type Business_business$data = Business_business;
export type Business_business$key = {
readonly " $data"?: Business_business$data;
readonly " $fragmentRefs": FragmentRefs<"Business_business">;
};
const BusinessFragment = graphql`
fragment Business_business on businesses {
name
businessId
}
`
type Props = {
fragmentRef: Business_business$key
}
const Business = ({ fragmentRef }: Props) => {
const business = useFragment(BusinessFragment, fragmentRef)
return (
<div>
<p>my html!</>
{/* I get the error: Type 'unknown' is not assignable to type 'number' for businessId */}
<ChildComponent businessId={business.businessId} />
</div>
)
}
interface Props {
businessId: number
}
const ChildComponent = ({ businessId }: Props) => {
return (
<p>my business id: {businessId}</p>
)
}
我需要做额外的配置吗?让Relay了解Hasura类型?我已经通过中继文档遵循了这个示例。
我假设Relay没有将bigint
编译为number
。
更新
我已经将Hasura中的列类型从bigint
更改为Int
,这解决了问题。有没有办法告诉Relay如何匹配它不熟悉的类型?在这种情况下,将bigint
转换为number
是完全可以的。
对更新的回答:您可以在relay.config.js
中添加customScalars
。我也在使用Hasura,这就是我设置的:
module.exports = {
// ... your other configs
customScalars: {
uuid: 'string',
int8: 'string',
bigint: 'string',
numeric: 'number',
varbit: 'string',
bit: 'string',
char: 'string',
varchar: 'string',
bool: 'boolean',
int: 'number',
int4: 'number',
float8: 'number',
timestamptz: 'string',
timetz: 'string',
jsonb: 'Record<string, unknown>',
_text: 'string',
date: 'string',
}
};
-
是否从
// in __generated__/Business_business.graphql
导入类型? -
有打字错误吗??类型名后面附加的$key是什么?
type Props = { fragmentRef: Business_business$key }