React-apollo: Is it possible to use the default 'dataIdFromObject' when specifying 'customResolvers'

Created on 13 Jun 2017  路  8Comments  路  Source: apollographql/react-apollo

After reading part 5 of the _Full-stack GraphQL + React Tutorial_, I was just wondering if it's possible to use the default dataIdFromObject when specifying the customResolvers?

It looks a bit cumbersome to _re茂mplement_ the default dataIdFromObject if you're happy with that one isn't it?
But I couldn't find a way to access it so I could use it in the customResolvers definition. 馃槙

Most helpful comment

It's only possible because the custom resolve function only runs after the client has been initialized. If it was run during the initialization then you're definitely right that it wouldn't work. GraphQL-js uses thunks for defining fields for the same reason (circular references).

All 8 comments

@MichaelDeBoey yes you can! Maybe it's not well documented, but you can access the function that the client instance uses as client.dataIdFromObject.

@helfer So you could use the following then?

import {ApolloClient, toIdValue} from 'react-apollo';

const client = new ApolloClient({
  customResolvers: {
    Query: {
      channel: (_, args) => {
        return toIdValue(ApolloClient.dataIdFromObject({ __typename: 'Channel', id: args['id'] }))
      },
    },
  },
});

Yes, but you'll have to replace ApolloClient.dataIdFromObject with client.dataIdFromObject.

@helfer But I'm still initializing the client here (i.e new ApolloClient) so...

I know, but just give it a try, it should work. The wonders of JavaScript! 馃槈

OK now I feel really dumb 馃槢 馃槗 馃槥
Why's this even possible? 馃槷

It's only possible because the custom resolve function only runs after the client has been initialized. If it was run during the initialization then you're definitely right that it wouldn't work. GraphQL-js uses thunks for defining fields for the same reason (circular references).

Thanks for the explanation @helfer 馃槈

Was this page helpful?
0 / 5 - 0 ratings