pinale
pinale

Reputation: 2214

react-admin dataprovider specify a custom field as primary key - a field that is not named "id"

I'm using the hasura data provider in react-admin ra-data-hasura (but i'm sure its more or less the same thing of ra-data-graphql)

My entity has their primary keys that are named differently from id Is it possible to set a field as primary that is not named id Eg: MyEntityId

Is it possible to specifiy it resource by resource (because each table could have its own name pattern) or globally

Upvotes: 0

Views: 754

Answers (2)

WiXSL
WiXSL

Reputation: 707

Alex, You have that answer in the docs: https://marmelab.com/react-admin/FAQ.html#can-i-have-custom-identifiersprimary-keys-for-my-resources

you have to resolve this at the dataProvider level. You have to implement your own dataProvider's methods that mapped your MyEntityId field to the id field required by react-admin.

For example (implementing getList method):

const myDataProvider = {
    getList: (resource, params) => {
        return fetch(API_URL)
               .then(({ json }) => ({
                    data: json.map((record) => ({ id: record.MyEntityId, ...record })),
                    total: parseInt(json.total, 10),
                })
        );
    }
   //...
}
json.map(record => ({"id": record.userID, ...record})),

Upvotes: 1

Fraction
Fraction

Reputation: 12984

Yes, you can customize the cache IDs using the keyFields for each type in the typePolicies option of the InMemoryCache constructor:

const client = new ApolloClient({
  ...
  cache: new InMemoryCache({
    typePolicies: {
      MyEntity1: {
        keyFields: ["MyEntityId"],
      },
      MyEntity2: {
        keyFields: ["MyEntityId"],
      },
      ...
    }
  }),
  ...
})

buildHasuraProvider({ client });

Upvotes: 1

Related Questions