Does Apollo Stack support global object identification, such as the node relay interface?

I am very new to both Apollo Stack and Relay. I try to choose between them to invest my time. After finishing reading Learning GraphQL and Relay , I turned to Apollo to find out what it has to offer, but right now there are not many Internet resources.

I have this question recently, but I can’t find the answer: Does Apollo support global object identification , as Relay does with the node interface? if not, does it have an alternative solution to support global object identification?

+4
source share
1 answer

Yes

The way it currently works (version 0.5 of apollo-client), with the function dataIdFromObjectthat the constructor takes ApolloClient.

If all nodes received a field id, and they are unique in all nodes (for example, in Graphcool we generate unique identifiers with this library ):

import ApolloClient from 'apollo-client';

const client = new ApolloClient({
  dataIdFromObject: o => o.id
});

You must include a field idin every query that you want to normalize.

If your identifiers are unique to each type only, you can combine them with __typenameto create unique identifiers:

const client = new ApolloClient({
  dataIdFromObject: (result) => {
    if (result.id && result.__typename) {
      return result.__typename + result.id;
    }

    // Make sure to return null if this object doesn't have an ID
    return null;
  },
});

The code is taken from the official Apollo documentation .

+7
source

Source: https://habr.com/ru/post/1662757/


All Articles