GraphQL is often presented as a revolutionary new way of understanding API. Instead of working with endpoints fixed on the server, you can get exactly the data you need with one request. And yes - GraphQL is flexible when implemented in an organization; it makes collaboration between frontend and backend development teams as smooth as ever. However, in practice, both of these technologies involve sending an HTTP request and getting some result, and inside GraphQL there are a lot of elements from the REST model embedded.
So what is really the difference at the technical level? What are the similarities and differences between these two API paradigms? By the end of the article, I will show you that GraphQL and REST are not so different, but GraphQL has some minor differences that significantly change the process of building and using API by developers.
REST — . URL, GET
- URL. , JSON, API. :
GET /books/1
{
"title": " ",
"author": {
"firstName": "",
"lastName": ""
}
// ...
}
: REST API ( «author») .
REST , , , . REST API, «book endpoint».
GraphQL , GraphQL . , Book
Author
:
type Book {
id: ID
title: String
published: Date
price: String
author: Author
}
type Author {
id: ID
firstName: String
lastName: String
books: [Book]
}
: , , . REST GraphQL : .
, Query
:
type Query {
book(id: ID!): Book
author(id: ID!): Author
}
, REST-, , GraphQL:
GET /graphql?query={ book(id: "1") { title, author { firstName } } }
{
"title": " ",
"author": {
"firstName": "",
}
}
, -! GraphQL, REST, , URL, JSON-.
, , GraphQL- URL , . , , author
, — , API.
, , , Books Authors, . .
:
GraphQL / REST, . GraphQL, Launchpad , .
API , . API, - , , , .
, API — , . , , API, GraphQL- REST API Swagger .
REST API API (endpoints):
GET /books/:id
GET /authors/:id
GET /books/:id/comments
POST /books/:id/comments
, «» API — . - , : « endpoint »?
GraphQL, , URL- , API. GraphQL-:
type Query {
book(id: ID!): Book
author(id: ID!): Author
}
type Mutation {
addComment(input: AddCommentInput): Comment
}
type Book { ... }
type Author { ... }
type Comment { ... }
input AddCommentInput { ... }
REST . : HTTP- URL- (GET, PUT, DELETE ..) GraphQL — Mutation Query. GraphQL- , , :
query { ... }
mutation { ... }
, Query
REST, . , , GraphQL «URL (endpoint URL)».
GraphQL API , REST: ; , . GraphQL , , , REST , , - URL- .
REST (endpoints), GraphQL .
Query
Mutation
, GraphQL API. .Query
, , query. , . REST URL.GET
- POST
. GraphQL .- , , Query
« » «» GraphQL. , , Query
, , .
, , API? , - , . , , API, , . , , . REST, GraphQL API, , .
JavaScript, , , , REST, GraphQL API. , , .
«Hello World» express, API Node:
app.get('/hello', function (req, res) {
res.send('Hello World!')
})
, /hello
, 'Hello World!'
. HTTP- REST API:
GET
) URLconst resolvers = {
Query: {
hello: () => {
return 'Hello world!';
},
},
};
, URL , , — hello
Query
. GraphQL , , (resolver).
, (query):
query {
hello
}
, , GraphQL-:
hello
, Query
.:
{ "hello": "Hello, world!" }
: !
query {
hello
secondHello: hello
}
, ( ), hello
. , , , .
(«nested» resolvers):
{
Query: {
author: (root, { id }) => find(authors, { id: id }),
},
Author: {
posts: (author) => filter(posts, { authorId: author.id }),
},
}
:
query {
author(id: 1) {
firstName
posts {
title
}
}
}
, , - . , GraphQL, «GraphQL Explained».
: REST , GraphQL -
REST, GraphQL API . REST API, GraphQL API . GraphQL : .
, GraphQL . REST.
, . , , . , . , , , , REST GraphQL , .
, GraphQL. , , API -, , . API , API , .
, GraphQL , REST. , HTTP- GraphQL API , REST API. , GraphQL , Apollo Client Relay.
REST GraphQL — .
Source: https://habr.com/ru/post/335158/
All Articles