question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

writeToStore: Missing field __typename

See original GitHub issue

Intended outcome:

I wanted a warning not to be displayed.

Actual outcome:

A warning was displayed on the console.

writeToStore.js:106 Missing field __typename in { “edges”: [ { “node”: { “id”: “2789”, “__typename”: “Shop” },

(anonymous) @ writeToStore.js:106 writeSelectionSetToStore @ writeToStore.js:89 writeFieldToStore @ writeToStore.js:200 (anonymous) @ writeToStore.js:96 writeSelectionSetToStore @ writeToStore.js:89 writeResultToStore @ writeToStore.js:69 replaceQueryResults @ replaceQueryResults.js:13 data @ store.js:133 apolloReducer @ store.js:46 combination @ combineReducers.js:120 dispatch @ createStore.js:165 (anonymous) @ ApolloClient.js:174 (anonymous) @ store.js:18 ObservableQuery.updateQuery @ ObservableQuery.js:223 (anonymous) @ ObservableQuery.js:142 ```

How to reproduce the issue:

I used such a code.

import React from 'react'
import ReactDOM from 'react-dom'

import { ApolloClient, createNetworkInterface, ApolloProvider } from 'react-apollo';

const client = new ApolloClient();

class ShopList extends React.Component {
  render() {
    if (this.props.shops) {
      return (
        <div>
          { this.props.shops.edges.map((edge) =>
            <div key={edge.node.id}>{edge.node.id}</div>
          )}
          <button onClick={this.props.loadMoreEntries} >Load More</button>
        </div>
      )
    } else {
      return (<div>Loading...</div>)
    }
  }
}

import { gql, graphql } from 'react-apollo';

const ShopsQuery = gql`
  query Shops($cursor: String) {
    shops(first: 30, after: $cursor) {
      edges {
        node {
          id
        }
      }
      pageInfo {
        endCursor
        hasNextPage
      }
    }
  }
`;

const AreasWithData = graphql(ShopsQuery, {
  props({ data: { loading, shops, fetchMore } }) {
    return {
      loading,
      shops,
      loadMoreEntries: () => {
        return fetchMore({
          query: ShopsQuery,
          variables: {
            cursor: shops.pageInfo.endCursor
          },
          updateQuery: (previousResult, { fetchMoreResult }) => {
            const newEdges = fetchMoreResult.shops.edges;
            const pageInfo = fetchMoreResult.shops.pageInfo;
            return {
              shops: {
                edges: [...previousResult.shops.edges, ...newEdges],
                pageInfo,
              },
            };
          },
        });
      },
    };
  },
})(ShopList);

ReactDOM.render(
  <ApolloProvider client={client}>
    <AreasWithData />
  </ApolloProvider>,
  document.getElementById('root')
)

Could you advise me on this issue?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

50reactions
helfercommented, Jun 30, 2017

Hi @yoneapp, you need to include __typename as follows in the result you return from updateQuery:

              shops: {
                __typename: previousResult.shops.__typename,
                edges: [...previousResult.shops.edges, ...newEdges],
                pageInfo,
              },
4reactions
cooperkacommented, Apr 4, 2018

@natterstefan This confused me also 🙂 The difference is that in the examples you linked to, the data is being returned from GraphQL and thus already has __typename defined on it. If you pass in your own custom data, you’ll have to add __typename yourself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does Apollo Client throw "Missing field" errors when ...
I'm using Apollo Client 3.4.x. When calling writeQuery and passing data that's missing fields specified in the query, I would expect Apollo ...
Read more >
Why do I receive the writetostore error in the following code?
I'm trying to make optimistic update while adding comments to a video, and receiving the following errors -. writeToStore.js:112 Missing ...
Read more >
Missing field __typename error despite ... - Meteor forums
In an optimistic response Im getting a missing field error for a field that I think I'm providing. Here is the error:
Read more >
Common mistakes when initializing Apollo's local state with ...
Following are common mistakes when writing data to the Apollo cache. Forgetting the typename. Try to comment out the typename field and just ......
Read more >
GraphQL: The Case of the Missing Fields - Thoughtbot
It goes without saying that debugging issues between the frontend and backend of a codebase can be frustrating. But, it's also like solving ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found