Mutation component becomes out of sync with the store after updates












0















This snippet appears long but is actually simple; it creates a node and renders its data and then updates the node and renders the updated data, but the data from the creation doesn't get updated, even though inspecting the store directly shows that the store for the createFoo query has been updated. I would like to understand how to ensure that components relying on data from the Apollo Client store are up to date, preferably automatically.



const CREATE_FOO = gql`
mutation {
createFoo(data: {}) {
id
bar
}
}
`;

const UPDATE_FOO = gql`
mutation($id: ID!) {
updateFoo(where: { id: $id }, data: { bar: "baz" }) {
id
bar
}
}
`;

<Mutation mutation={CREATE_FOO}>
{(createFoo, { data: createData }) => {
if (createData) {
return (
<div>
<p>{JSON.stringify(createData.createFoo)}</p>
<Mutation
mutation={UPDATE_FOO}
variables={{ id: createData.createFoo.id }}
>
{(updateFoo, { data: updateData }) => {
if (updateData) {
return <p>{JSON.stringify(updateData.updateFoo)}</p>;
}
return (
<button onClick={updateFoo} type="button">
updateFoo
</button>
);
}}
</Mutation>
</div>
);
}
return (
<button onClick={createFoo} type="button">
createFoo
</button>
);
}}
</Mutation>


Data model:



type Foo {
id: ID! @unique
bar: String
}


Store data:



{
"ROOT_QUERY": {
"userSidebarWidth": 200
},
"Foo:cjqd2cd41zi140a98ugrjpz2m": {
"id": "cjqd2cd41zi140a98ugrjpz2m",
"bar": "baz",
"__typename": "Foo"
},
"ROOT_MUTATION": {
"createFoo({"data":{}})": {
"type": "id",
"generated": false,
"id": "Foo:cjqd2cd41zi140a98ugrjpz2m",
"typename": "Foo"
},
"updateFoo({"data":{"bar":"baz"},"where":{"id":"cjqd2cd41zi140a98ugrjpz2m"}})": {
"type": "id",
"generated": false,
"id": "Foo:cjqd2cd41zi140a98ugrjpz2m",
"typename": "Foo"
}
}
}









share|improve this question



























    0















    This snippet appears long but is actually simple; it creates a node and renders its data and then updates the node and renders the updated data, but the data from the creation doesn't get updated, even though inspecting the store directly shows that the store for the createFoo query has been updated. I would like to understand how to ensure that components relying on data from the Apollo Client store are up to date, preferably automatically.



    const CREATE_FOO = gql`
    mutation {
    createFoo(data: {}) {
    id
    bar
    }
    }
    `;

    const UPDATE_FOO = gql`
    mutation($id: ID!) {
    updateFoo(where: { id: $id }, data: { bar: "baz" }) {
    id
    bar
    }
    }
    `;

    <Mutation mutation={CREATE_FOO}>
    {(createFoo, { data: createData }) => {
    if (createData) {
    return (
    <div>
    <p>{JSON.stringify(createData.createFoo)}</p>
    <Mutation
    mutation={UPDATE_FOO}
    variables={{ id: createData.createFoo.id }}
    >
    {(updateFoo, { data: updateData }) => {
    if (updateData) {
    return <p>{JSON.stringify(updateData.updateFoo)}</p>;
    }
    return (
    <button onClick={updateFoo} type="button">
    updateFoo
    </button>
    );
    }}
    </Mutation>
    </div>
    );
    }
    return (
    <button onClick={createFoo} type="button">
    createFoo
    </button>
    );
    }}
    </Mutation>


    Data model:



    type Foo {
    id: ID! @unique
    bar: String
    }


    Store data:



    {
    "ROOT_QUERY": {
    "userSidebarWidth": 200
    },
    "Foo:cjqd2cd41zi140a98ugrjpz2m": {
    "id": "cjqd2cd41zi140a98ugrjpz2m",
    "bar": "baz",
    "__typename": "Foo"
    },
    "ROOT_MUTATION": {
    "createFoo({"data":{}})": {
    "type": "id",
    "generated": false,
    "id": "Foo:cjqd2cd41zi140a98ugrjpz2m",
    "typename": "Foo"
    },
    "updateFoo({"data":{"bar":"baz"},"where":{"id":"cjqd2cd41zi140a98ugrjpz2m"}})": {
    "type": "id",
    "generated": false,
    "id": "Foo:cjqd2cd41zi140a98ugrjpz2m",
    "typename": "Foo"
    }
    }
    }









    share|improve this question

























      0












      0








      0








      This snippet appears long but is actually simple; it creates a node and renders its data and then updates the node and renders the updated data, but the data from the creation doesn't get updated, even though inspecting the store directly shows that the store for the createFoo query has been updated. I would like to understand how to ensure that components relying on data from the Apollo Client store are up to date, preferably automatically.



      const CREATE_FOO = gql`
      mutation {
      createFoo(data: {}) {
      id
      bar
      }
      }
      `;

      const UPDATE_FOO = gql`
      mutation($id: ID!) {
      updateFoo(where: { id: $id }, data: { bar: "baz" }) {
      id
      bar
      }
      }
      `;

      <Mutation mutation={CREATE_FOO}>
      {(createFoo, { data: createData }) => {
      if (createData) {
      return (
      <div>
      <p>{JSON.stringify(createData.createFoo)}</p>
      <Mutation
      mutation={UPDATE_FOO}
      variables={{ id: createData.createFoo.id }}
      >
      {(updateFoo, { data: updateData }) => {
      if (updateData) {
      return <p>{JSON.stringify(updateData.updateFoo)}</p>;
      }
      return (
      <button onClick={updateFoo} type="button">
      updateFoo
      </button>
      );
      }}
      </Mutation>
      </div>
      );
      }
      return (
      <button onClick={createFoo} type="button">
      createFoo
      </button>
      );
      }}
      </Mutation>


      Data model:



      type Foo {
      id: ID! @unique
      bar: String
      }


      Store data:



      {
      "ROOT_QUERY": {
      "userSidebarWidth": 200
      },
      "Foo:cjqd2cd41zi140a98ugrjpz2m": {
      "id": "cjqd2cd41zi140a98ugrjpz2m",
      "bar": "baz",
      "__typename": "Foo"
      },
      "ROOT_MUTATION": {
      "createFoo({"data":{}})": {
      "type": "id",
      "generated": false,
      "id": "Foo:cjqd2cd41zi140a98ugrjpz2m",
      "typename": "Foo"
      },
      "updateFoo({"data":{"bar":"baz"},"where":{"id":"cjqd2cd41zi140a98ugrjpz2m"}})": {
      "type": "id",
      "generated": false,
      "id": "Foo:cjqd2cd41zi140a98ugrjpz2m",
      "typename": "Foo"
      }
      }
      }









      share|improve this question














      This snippet appears long but is actually simple; it creates a node and renders its data and then updates the node and renders the updated data, but the data from the creation doesn't get updated, even though inspecting the store directly shows that the store for the createFoo query has been updated. I would like to understand how to ensure that components relying on data from the Apollo Client store are up to date, preferably automatically.



      const CREATE_FOO = gql`
      mutation {
      createFoo(data: {}) {
      id
      bar
      }
      }
      `;

      const UPDATE_FOO = gql`
      mutation($id: ID!) {
      updateFoo(where: { id: $id }, data: { bar: "baz" }) {
      id
      bar
      }
      }
      `;

      <Mutation mutation={CREATE_FOO}>
      {(createFoo, { data: createData }) => {
      if (createData) {
      return (
      <div>
      <p>{JSON.stringify(createData.createFoo)}</p>
      <Mutation
      mutation={UPDATE_FOO}
      variables={{ id: createData.createFoo.id }}
      >
      {(updateFoo, { data: updateData }) => {
      if (updateData) {
      return <p>{JSON.stringify(updateData.updateFoo)}</p>;
      }
      return (
      <button onClick={updateFoo} type="button">
      updateFoo
      </button>
      );
      }}
      </Mutation>
      </div>
      );
      }
      return (
      <button onClick={createFoo} type="button">
      createFoo
      </button>
      );
      }}
      </Mutation>


      Data model:



      type Foo {
      id: ID! @unique
      bar: String
      }


      Store data:



      {
      "ROOT_QUERY": {
      "userSidebarWidth": 200
      },
      "Foo:cjqd2cd41zi140a98ugrjpz2m": {
      "id": "cjqd2cd41zi140a98ugrjpz2m",
      "bar": "baz",
      "__typename": "Foo"
      },
      "ROOT_MUTATION": {
      "createFoo({"data":{}})": {
      "type": "id",
      "generated": false,
      "id": "Foo:cjqd2cd41zi140a98ugrjpz2m",
      "typename": "Foo"
      },
      "updateFoo({"data":{"bar":"baz"},"where":{"id":"cjqd2cd41zi140a98ugrjpz2m"}})": {
      "type": "id",
      "generated": false,
      "id": "Foo:cjqd2cd41zi140a98ugrjpz2m",
      "typename": "Foo"
      }
      }
      }






      graphql react-apollo apollo-client






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 1:20









      sliktsslikts

      6,33112043




      6,33112043
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Let's assume you have a Query component like this:



          const GET_FOO = gql`
          query SomeQuery {
          getFoo {
          id
          bar
          }
          }
          `;

          <Query query={GET_FOO}>
          {({ loading, error, data }) => (
          <SomeComponent/>
          )}
          </Query>


          When the component is mounted, Apollo fetches data from the server or the cache (depending on whether the data is cached and what your fetch policy is). However, it also subscribes to changes in the cache. So if you fetched a Foo with an id of 1 and it's updated in the cache through another operation, the component will be notified of them and will update accordingly. Specifically, the data value passed to the render prop function will be changed to reflect the change in the cache.



          A Mutation component has a render prop function signature similar to the Query component, including a data property. However, this property only reflects the data returned when the mutate function is called. While the results of the mutation are stored in the cache, the data exposed through the Mutation component is not the result of a subscription to the cache -- rather, it's just a convenient way to react to the results from the mutate call. The only time the data inside the Mutation component will potentially change is if the mutate function is called again.



          If we modify your example and wrap the components in a Query component, we can show the data from the query inside your component and potentially ignore the data from either of the two Mutation components. Assuming the Query and the Mutations reference the same item in the cache, the data from the Query component will be updated any time one of the mutations resolves.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992481%2fmutation-component-becomes-out-of-sync-with-the-store-after-updates%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Let's assume you have a Query component like this:



            const GET_FOO = gql`
            query SomeQuery {
            getFoo {
            id
            bar
            }
            }
            `;

            <Query query={GET_FOO}>
            {({ loading, error, data }) => (
            <SomeComponent/>
            )}
            </Query>


            When the component is mounted, Apollo fetches data from the server or the cache (depending on whether the data is cached and what your fetch policy is). However, it also subscribes to changes in the cache. So if you fetched a Foo with an id of 1 and it's updated in the cache through another operation, the component will be notified of them and will update accordingly. Specifically, the data value passed to the render prop function will be changed to reflect the change in the cache.



            A Mutation component has a render prop function signature similar to the Query component, including a data property. However, this property only reflects the data returned when the mutate function is called. While the results of the mutation are stored in the cache, the data exposed through the Mutation component is not the result of a subscription to the cache -- rather, it's just a convenient way to react to the results from the mutate call. The only time the data inside the Mutation component will potentially change is if the mutate function is called again.



            If we modify your example and wrap the components in a Query component, we can show the data from the query inside your component and potentially ignore the data from either of the two Mutation components. Assuming the Query and the Mutations reference the same item in the cache, the data from the Query component will be updated any time one of the mutations resolves.






            share|improve this answer




























              1














              Let's assume you have a Query component like this:



              const GET_FOO = gql`
              query SomeQuery {
              getFoo {
              id
              bar
              }
              }
              `;

              <Query query={GET_FOO}>
              {({ loading, error, data }) => (
              <SomeComponent/>
              )}
              </Query>


              When the component is mounted, Apollo fetches data from the server or the cache (depending on whether the data is cached and what your fetch policy is). However, it also subscribes to changes in the cache. So if you fetched a Foo with an id of 1 and it's updated in the cache through another operation, the component will be notified of them and will update accordingly. Specifically, the data value passed to the render prop function will be changed to reflect the change in the cache.



              A Mutation component has a render prop function signature similar to the Query component, including a data property. However, this property only reflects the data returned when the mutate function is called. While the results of the mutation are stored in the cache, the data exposed through the Mutation component is not the result of a subscription to the cache -- rather, it's just a convenient way to react to the results from the mutate call. The only time the data inside the Mutation component will potentially change is if the mutate function is called again.



              If we modify your example and wrap the components in a Query component, we can show the data from the query inside your component and potentially ignore the data from either of the two Mutation components. Assuming the Query and the Mutations reference the same item in the cache, the data from the Query component will be updated any time one of the mutations resolves.






              share|improve this answer


























                1












                1








                1







                Let's assume you have a Query component like this:



                const GET_FOO = gql`
                query SomeQuery {
                getFoo {
                id
                bar
                }
                }
                `;

                <Query query={GET_FOO}>
                {({ loading, error, data }) => (
                <SomeComponent/>
                )}
                </Query>


                When the component is mounted, Apollo fetches data from the server or the cache (depending on whether the data is cached and what your fetch policy is). However, it also subscribes to changes in the cache. So if you fetched a Foo with an id of 1 and it's updated in the cache through another operation, the component will be notified of them and will update accordingly. Specifically, the data value passed to the render prop function will be changed to reflect the change in the cache.



                A Mutation component has a render prop function signature similar to the Query component, including a data property. However, this property only reflects the data returned when the mutate function is called. While the results of the mutation are stored in the cache, the data exposed through the Mutation component is not the result of a subscription to the cache -- rather, it's just a convenient way to react to the results from the mutate call. The only time the data inside the Mutation component will potentially change is if the mutate function is called again.



                If we modify your example and wrap the components in a Query component, we can show the data from the query inside your component and potentially ignore the data from either of the two Mutation components. Assuming the Query and the Mutations reference the same item in the cache, the data from the Query component will be updated any time one of the mutations resolves.






                share|improve this answer













                Let's assume you have a Query component like this:



                const GET_FOO = gql`
                query SomeQuery {
                getFoo {
                id
                bar
                }
                }
                `;

                <Query query={GET_FOO}>
                {({ loading, error, data }) => (
                <SomeComponent/>
                )}
                </Query>


                When the component is mounted, Apollo fetches data from the server or the cache (depending on whether the data is cached and what your fetch policy is). However, it also subscribes to changes in the cache. So if you fetched a Foo with an id of 1 and it's updated in the cache through another operation, the component will be notified of them and will update accordingly. Specifically, the data value passed to the render prop function will be changed to reflect the change in the cache.



                A Mutation component has a render prop function signature similar to the Query component, including a data property. However, this property only reflects the data returned when the mutate function is called. While the results of the mutation are stored in the cache, the data exposed through the Mutation component is not the result of a subscription to the cache -- rather, it's just a convenient way to react to the results from the mutate call. The only time the data inside the Mutation component will potentially change is if the mutate function is called again.



                If we modify your example and wrap the components in a Query component, we can show the data from the query inside your component and potentially ignore the data from either of the two Mutation components. Assuming the Query and the Mutations reference the same item in the cache, the data from the Query component will be updated any time one of the mutations resolves.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 1 at 5:41









                Daniel ReardenDaniel Rearden

                14.7k11137




                14.7k11137
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992481%2fmutation-component-becomes-out-of-sync-with-the-store-after-updates%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas