简体   繁体   English

当 Relay 编译时,GraphQL 类型在 Typescript 中被定义为未知

[英]GraphQL types being defined as unknown in Typescript when compiled by Relay

I am new to using Typescript and finding there is a mismatch in how the types are being handled from the GraphQL schema and types generated via Relay.我是使用 Typescript 的新手,发现从 GraphQL 模式和通过 Relay 生成的类型处理类型的方式不匹配。

Here is an example:这是一个例子:

# 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>
  )
}

Is there additional config I need to do to have Relay understand the Hasura types?我需要做其他配置才能让 Relay 了解 Hasura 类型吗? I have followed the example via the relay docs .我通过中继文档遵循了这个例子。

I have the assumption that Relay is not compiling bigint to number .我假设 Relay 没有将bigint编译为number


Update更新

I have changed the column type in Hasura from bigint to Int and this solved the problem.我已将 Hasura 中的列类型从bigint更改为Int ,这解决了问题。 Is there a way to tell Relay how to match types it is unfamiliar with?有没有办法告诉 Relay 如何匹配它不熟悉的类型? In this case cast bigint to number is totally fine.在这种情况下,将bigint转换为number是完全可以的。

  1. Did you import the type from // in __generated__/Business_business.graphql ?您是否从// in __generated__/Business_business.graphql导入了类型?

  2. Is there a typo??有错别字吗?? what is $key appended to the typename?什么是 $key 附加到类型名?

     type Props = { fragmentRef: Business_business$key }

Answer to your Update: You can add customScalars within your relay.config.js .回答您的更新:您可以在您的relay.config.js customScalars I'm also using Hasura and this is what I've set up:我也在使用 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',
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM