-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] REST GraphQL

, 08 2017 . 13:18 +

HTTP: ?


Sashko Stubailo GraphQL vs. REST


GraphQL API. (endpoints) , . Ƞࠗ GraphQL , frontend- backend- , . HTTP- - , GraphQL REST.


?   API? ʠ , GraphQL REST , GraphQL , API.


. API, , GraphQL REST.


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, . .



:


  • : , .
  • : GET- URL- HTTP.
  • : JSON.
  • : REST (endpoint) .  GraphQL , .
  • : REST .  GraphQL , .

GraphQL/ REST, . GraphQL, Launchpad , .


URL- GraphQL


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 { ... }

GraphQL (.), .


, Query REST, . , , GraphQL URL (endpoint URL).


GraphQLAPI , REST: ; , .  GraphQL , , , REST , , - URL- .



 REST (endpoints), GraphQL .


  • : REST API Query Mutation, GraphQLAPI. .
  • : API .
  • : GraphQL , , .  REST .
  • : GraphQL Query , , query. , .  REST URL.
  • : REST , HTTP- GET - POST.  GraphQL .

- , , Query GraphQL. , , Query , , .



, , API? , - , . , , API, , . , , . REST, GraphQL API, , .


  JavaScript, , , , REST, GraphQLAPI. ߠ , , .


Hello World express, API Node:


app.get('/hello', function (req, res) {
  res.send('Hello World!')
})

, /hello, 'Hello World!'. HTTP- REST API:


  1. HTTP- ( GET) URL
  2. API- ,
  3. API- ,

GraphQL , :


const resolvers = {
  Query: {
    hello: () => {
      return 'Hello world!';
    },
  },
};

, URL , , 堗 hello Query.  GraphQL, , (resolver).


, (query):


query {
  hello
}

, , GraphQL-:


  1. HTTP- GraphQL-
  2. (query) , (resolver).   , hello, Query.
  3. GraphQL , (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, GraphQLExplained.


, !




: REST , GraphQL -



REST, GraphQLAPI . REST API, GraphQLAPI . GraphQL : .


  • : REST GraphQL .
  • : REST, GraphQL .
  • : REST - .  GraphQL - .
  • : REST .  GraphQL , GraphQL, .

, GraphQL . REST.


?


, . , , . , . ߠ, , , , REST GraphQL , .


ߠ, GraphQL.  , , API -, , . API , API , .


Ѡ , GraphQL , REST. , HTTP- GraphQLAPI , REST API. , GraphQL , Apollo Client Relay.


REST GraphQL .

Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/335158/

:  

: [1] []
 

:
: 

: ( )

:

  URL