首页 > 解决方案 > 使用 GraphQL 在 react-admin 列表组件中过滤

问题描述

在我的应用程序中,我有location实体和DPD实体。DPD 包含location_id字段。GQL 架构:

  type Location @key(fields: "id") {
    id: ID!
    label: String
    address: Address
    tenant: Tenant
    manager: User
  }

  extend type Query {
    Location(id: ID!): Location
    allLocations(
      page: Int
      perPage: Int
      sortField: String
      sortOrder: String
      filter: LocationFilter
    ): [Location]
    _allLocationsMeta(
      page: Int
      perPage: Int
      sortField: String
      sortOrder: String
      filter: LocationFilter
    ): ListMetadata
  }

  input LocationFilter {
    ids: [ID]
    label: String
  }


  type DPD @key(fields: "id") {
    id: ID
    name: String
    location: Location
    location_id: ID
  }

  input DPDFilter {
    ids: [ID]
    id: ID
    name: String
    location_id: ID
  }

  extend type Query {
    DPD(id: ID!): DPD
    allDPDS(page: Int, perPage: Int, sortField: String, sortOrder: String, filter: DPDFilter): [DPD]
    _allDPDSMeta(page: Int, perPage: Int, sortField: String, sortOrder: String, filter: DPDFilter): ListMetadata
  }

在列出 DPD 时,我需要按location. 在 RA 项目中,我有DPDList组件:

const filters = [
  <ReferenceInput key="location" source="location.id" reference="Location">
    <SelectInput optionText="label" />
  </ReferenceInput>,
];

export const DPDList: FC<ResourceComponentProps> = (props: ResourceComponentProps) => (
  <List {...props} filter={filters}>
    <Datagrid>
      <TextField source="name" />
      <ReferenceField label="Location" source="location.id" reference="Location" sortable={false}>
        <TextField source="label" />
      </ReferenceField>
      <EditButton />
    </Datagrid>
  </List>
);

如果没有应用过滤器,我会呈现正确的列表。我可以看到查询:

allDPDS带参数:

{filter: {}, page: 0, perPage: 10, sortField: "id", sortOrder: "ASC"}

allLocations带参数

{filter: {ids: ["3", "1", "4", "7"]}}

当我应用过滤allDPDS查询更改的参数时:

{filter: {0: {key: "location", ref: null,…}}, page: 0, perPage: 10, sortField: "id", sortOrder: "ASC"}

我有错误:

Variable "$filter" got invalid value ...

我应该在模式(自省)或 RA 中更改什么?

标签: graphqlapollo-clientreact-admin

解决方案


推荐阅读