【问题标题】:Use enumValues from graphql introspection in SelectInput of react-admin在 react-admin 的 SelectInput 中使用来自 graphql 自省的 enumValues
【发布时间】:2019-07-30 04:07:59
【问题描述】:

我试图避免在我的 react-admin 应用程序中出现这种代码:

<SelectInput source="currency" choices={[
  { id: 'EUR', name: 'EUR' },
  { id: 'USD', name: 'USD' },
]} />

相反,我想查询 graphql 端点以获取可用枚举列表并在此元素中使用它们。 对于这个特定的示例,如果我执行这样的 graphql 查询:

query GetEnum {
__type(name: "Currency") {
  enumValues {
    name
  }
}}

我收到以下回复:

{ "data": {
"__type": {
  "enumValues": [
    {
      "name": "EUR"
    },
    {
      "name": "USD"
    }
  ]
} 
}}

在 API 端添加/编辑枚举选项时,我希望能够避免编辑我的 react-admin 应用程序。

如果在 react-admin 中已经预见到这一点,有什么想法吗?我在文档中找不到任何示例或用法。

谢谢!

【问题讨论】:

    标签: reactjs graphql react-admin


    【解决方案1】:

    对我来说最好的解决方案是用你的货币建立一个新的资源。

    <Admin {...props}>
        {/* Notice you don't have to set any view to declare a resource */}
        <Resource name="currencies" />
    </Admin>
    

    这样,您就可以使用&lt;ReferenceInput&gt; 在其他资源中选择您的货币。

    <ReferenceInput label="Currency" source="currency" reference="currencies">
        <SelectInput optionText="name" />
    </ReferenceInput>
    

    【讨论】:

      猜你喜欢
      • 2017-11-29
      • 2019-02-15
      • 2019-12-02
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 2019-11-13
      • 1970-01-01
      相关资源
      最近更新 更多