reactjs - GraphQL 类型在由 Relay 编译时在 Typescript 中被定义为未知
问题描述
我是使用 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
to更改为Int
,这解决了问题。有没有办法告诉 Relay 如何匹配它不熟悉的类型?bigint
在这种情况下,投到number
完全没问题。
解决方案
回答您的更新:您可以customScalars
在relay.config.js
. 我也在使用 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',
}
};
推荐阅读
- python - How to improve SQLite insert performance in Python 3.6?
- git - 获取每个目录的 git commit stats
- javascript - my markers don't appear using google maps api v3, using php and javascript
- ios - Swift:带有@escaping 闭包的选择器返回 EXC_BAD_ACCESS
- java - 在链接列表 (Java) 中插入值时遇到问题?
- javascript - Haxe DOMStringMap 值无法访问
- amazon-web-services - 授予数以万计的 AWS 账户访问存储桶的权限?
- qt - QMake 项目文件 - 选择替代编译器
- java - Google 搜索 API QueryOptions 和 SortOptions 限制
- mysql - mysql将字符串转换为int不起作用(使用pdo)