Why need a server for running GraphQL

2019-09-04 13:00发布

问题:

I am totally new in GraphQL. So many things are confusing for me like why i need a server for running GraphQL as why some library doesn't sort out the implementation. As far as i know server is for respond data. So what else it will do for GraphQL.

回答1:

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

https://graphql.org/

Essentially, you have a client side and a server side. The client requests (query) data from the server, or requests the server to update data (mutation). If you're working on the client side only, you don't need a server (given it already exists).

This link may also be helpful: https://www.apollographql.com/why-graphql

Hope this makes sense?



回答2:

As you note, it's "just" a query language, and you don't need a server per se. For example, in the reference graphql-js implementation, you can just call graphql with a query; similarly, in graphql-ruby, you can #execute a query on a schema object.

The usual case for GraphQL "in the wild" is as an API layer, though. You'd have a GraphQL interface over a native database, or as an alternative to a REST API. In both of those cases if you were calling something from the same system you wouldn't usually go through GraphQL just to translate it into, say, SQL; you'd directly call the database layer.

You might compare GraphQL to to SQL, where there are also library-based implementations but the query language is the only way to interact with the system; and also to other API layers like SOAP, which use HTTP as a minimal transport layer but don't really use the full expressiveness of the protocol.