graphqlapollo-servergraphql-mutation

Can we perform post/put/delete operations in GraphQL query?


I read somewhere that queries are only for Get request and can't handle Request body. But when I tried handling a mutation in query, it just worked! If that's so, what's the use of mutations then?

P.S. - Many websites say mutations can be used to perform crud operations. But don't have any data store as such, all my get/post/ put requests are fetching data and are rest APIs. How should I utilised the power of mutations then?


Solution

  • GraphQL is a separate protocol. It does not depend on HTTP operations such as POST, PUT, or DELETE. So, in GraphQL, POST, PUT, or DELETE does not make sense. Instead, GraphQL has its own set of operations. Namely, Query, Mutation, and Subscription.

    The Query operation is used to retrieve data from a GraphQL server, and the Mutation is used to mutate data. Subscription is used to continuously retrieve the data.

    How a GraphQL endpoint handles these operations solely depends on the implementers of those endpoints. It can be either updating a database or changing files in the server. As far as GraphQL is concerned, a Mutation operation is intended to have side effects, but not necessarily. A Query operation on the other hand should not do anything like that. It should only read the data. But if the developers want to violate these rules, they can.

    However, most of the GraphQL implementations use HTTP as the underlying network protocol since the GraphQL spec does not specify a network protocol. So, internally, GraphQL servers will handle requests using the HTTP GET and POST methods. But they don't have any difference from GraphQL's point of view.