0
votes

I'm trying to store data in apollo cache and get this data in my component by graphql HOC

Here I init Apollo Client and add user data to the apollo cache:

const cache = new InMemoryCache();

const client = new ApolloClient({
  cache,
  resolvers: {}
});
cache.writeQuery({
  query: GET_CURRENT_USER,
  data: {
    userData: {
      id: 1,
      name: "Alexander",
      email: "[email protected]"
    }
  }
});

const App = () => (
  <ApolloProvider client={client}>
    <Main />
  </ApolloProvider>
);

Here is my GET_CURRENT_USER query:

import { gql } from "apollo-boost";

export const GET_CURRENT_USER = gql`
  query getUserData($id: ID!) {
    userData @client {
      id
      name
      email
    }
  }
`;

Here I wrapp my component into graphql HOC:

...
export default graphql(GET_CURRENT_USER)(ProfileScreen);

And I'm checking props.data property into render method of ProfileScreen component, it's fired 2 times:

  1. error: undefined; loading: true; no userData field

  2. error: undefined; loading: false; no userData field

Also I checked Apollo Cache and userData field is stored

Environment:

  1. "apollo-boost": "^0.4.3",
  2. "react-apollo": "^2.5.8",
  3. "react-native": "0.60.4",
  4. "apollo-cache-inmemory": "^1.6.2",
  5. "react": "16.8.6"
1

1 Answers

0
votes

Fixed by adding __typename field to the userData object