Search code examples
apolloreact-apolloapollo-clientapollo-serverapollostack

difference between Query and Mutation in Apollo?


We can make some requests to the server using both Query and Mutation. In these queries we can pass some params and we will get some results from the server in both cases. The only one obligatory difference is that we can call the mutation from our props like "this.props.mutation", but it looks like a syntax sugar, because we can wrap our HOC in "withApollo" and we'll receive "query" method in props too. So what is the main difference between these two types of requests?


Solution

  • Strictly speaking there is no difference.

    ... technically any query could be implemented to cause a data write. However, it's useful to establish a convention that any operations that cause writes should be sent explicitly via a mutation.

    However, the reference implementation does enforce the following.

    While query fields are executed in parallel, mutation fields run in series, one after the other.

    This means that if we send two incrementCredits mutations in one request, the first is guaranteed to finish before the second begins, ensuring that we don't end up with a race condition with ourselves.

    Both quotes can be found from the links below.

    http://graphql.org/learn/queries/#mutations

    http://graphql.org/learn/queries/#multiple-fields-in-mutations