Recently I learned about GraphQL which claims to be superior to RESTful. However, I started wondering why don't we simply put SQL statements into an HTTP GET request.
For example, in GraphQL I would write
{ Movie(id: "cixos5gtq0ogi0126tvekxo27") { id title actors { name } } }
Which isn't much simpler than its SQL counterpart
SELECT id, title FROM movies WHERE id = cixos5gtq0ogi0126tvekxo27; SELECT actors.name FROM actors, actors_movies WHERE actors.id == movies.actor_id AND movie.id == cixos5gtq0ogi0126tvekxo27;
Maybe we can URL-encode the query and send to the server
GET endpoint?q=SELECT%20id%2C%20title%20FROM%20movies%20WHERE%20id%20%3D%20cixos5gtq0ogi0126tvekxo27%3B%0ASELECT%20actors.name%20FROM%20actors%2C%20actors_movies%20WHERE%20actors.id%20%3D%3D%20movies.actor_id%20AND%20movie.id%20%3D%3D%20cixos5gtq0ogi0126tvekxo27%3B HTTP/1.1
Yes, the query URL can be too long, but you can put it into the body of a POST request if you don't care about REST compliance. (By the way, I think the HTTP RFC need be revised for REST to make sense: capping the length of query strings mixes implementation with specification at the very beginning)
Directly issuing SQL from the client also has the advantage of
- No server-side code/library is required to parse GraphQL, reducing development time.
- No server-side overhead is needed to parse GraphQL, reducing runtime.
- SQL statements are much more flexible than GraphQL because (in most cases) the latter will reduce to SQL anyway.
- Everyone knows SQL.
So, what the advantages GraphQL have over SQL?