{"id":311660,"date":"2020-10-19T09:00:12","date_gmt":"2020-10-19T09:00:12","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=311660"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=311660","title":{"rendered":"\u0421\u043e\u0435\u0434\u0438\u043d\u044f\u0435\u043c Redux \u0438 GraphQL \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/524000\/\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/aac\/433\/3a5\/aac4333a559206c248668e95b801312d.png\" width=\"1298\" height=\"653\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c Redux \u0438 GraphQL \u0431\u0435\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f Apollo Client \u0438\u043b\u0438 Relay.<\/p>\n<h3>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 Redux<\/h3>\n<p><a href=\"https:\/\/github.com\/reduxjs\/redux\" rel=\"noopener noreferrer nofollow\">\u0420\u0435\u0434\u0430\u043a\u0441<\/a> \u044d\u0442\u043e \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0432 \u0432\u0438\u0434\u0435 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438. \u041e\u043d \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442, \u0447\u0442\u043e \u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0435\u0441\u0442\u044c \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 <code>state<\/code>. \u0418\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 <code>state<\/code>  \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043d\u0435\u043b\u044c\u0437\u044f. \u042d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0435\u0440\u0435\u0437 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438  <code>reducers<\/code>. <\/p>\n<p>\u041f\u0441\u0435\u0432\u0434\u043e\u043a\u043e\u0434:<\/p>\n<pre><code class=\"javascript\">function dispatch(action) {     state = reducer(state, action); }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0430\u043a\u0442\u0430 \u0438 \u0440\u0435\u0434\u0430\u043a\u0441\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 <code>react-redux<\/code>. \u041e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 <code>state<\/code> \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0438 \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0447\u0435\u0440\u0435\u0437 <code>dispatch<\/code>.<\/p>\n<h3>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 GraphQL<\/h3>\n<p>GraphQL \u0434\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e POST-\u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u043e\u0441\u043e\u0431\u044b\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u043e\u043c body:<\/p>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>REST<\/p>\n<\/th>\n<th>\n<p>GraphQL<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p>GET \/books\/id<\/p>\n<\/td>\n<td>\n<p>POST \/graphql<br \/><code>query{ book(id) {} }<\/code><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>POST \/books<br \/>{\u2026}<\/p>\n<\/td>\n<td>\n<p>POST \/graphql<br \/><code>mutation{ addBook() {...} }<\/code><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>UPDATE \/books\/id<br \/>{\u2026}<\/p>\n<\/td>\n<td>\n<p>POST \/graphql<br \/><code>mutation{ updateBook(id) {...} }<\/code><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>DELETE \/books\/id<\/p>\n<\/td>\n<td>\n<p>POST \/graphql<br \/><code>mutation{ deleteBook(id) {} }<\/code><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3>GraphQL \u0431\u0435\u0437 Apollo Client (\u0438 Relay)<\/h3>\n<p>Apollo-client \u044d\u0442\u043e \u0441\u0430\u043c\u0430\u044f \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 GraphQL. \u041e\u043d\u0430 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0440\u0435\u0448\u0430\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0443 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043a\u044d\u0448\u0430. \u0412\u043c\u0435\u0441\u0442\u043e Redux \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b. \u041e\u0434\u043d\u0430\u043a\u043e \u044d\u0442\u0438 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b \u0434\u0430\u043b\u0435\u043a\u043e \u043d\u0435 \u0442\u0430\u043a\u0438\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0438 \u0443\u0434\u043e\u0431\u043d\u044b\u0435.<\/p>\n<p>\u0412\u043c\u0435\u0441\u0442\u043e Apollo Client \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0442\u0430\u043a\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439:<\/p>\n<pre><code class=\"javascript\">const graphqlAPI = (query, variables) =&gt; fetch(\"\/graphql\", {     method: \"POST\",     body: JSON.stringify({ query, variables }) });<\/code><\/pre>\n<h3>redux-toolkit<\/h3>\n<p>\u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <strong>\u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439<\/strong> \u043d\u0430\u0431\u043e\u0440 \u0443\u0442\u0438\u043b\u0438\u0442 \u043e\u0442 redux \u2014 <a href=\"https:\/\/github.com\/reduxjs\/redux-toolkit\" rel=\"noopener noreferrer nofollow\">redux-toolkit<\/a>. \u041e\u043d\u0438 \u0440\u0435\u0448\u0430\u044e\u0442 \u0441\u0430\u043c\u0443\u044e \u0447\u0430\u0441\u0442\u0443\u044e \u043f\u0440\u0435\u0442\u0435\u043d\u0437\u0438\u044e \u043a \u0440\u0435\u0434\u0430\u043a\u0441\u0443 \u2014 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. \u0412 \u044d\u0442\u043e\u0442 \u043d\u0430\u0431\u043e\u0440 \u0432\u0445\u043e\u0434\u0438\u0442:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/redux-toolkit.js.org\/api\/createEntityAdapter\" rel=\"noopener noreferrer nofollow\">createEntityAdapter<\/a> \u0434\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440\u0430\u043c\u0438 \u0438 \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440\u0430\u043c\u0438.<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/redux-toolkit.js.org\/api\/createAsyncThunk\" rel=\"noopener noreferrer nofollow\">createAsyncThunk<\/a> \u0434\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0441\u0430\u0439\u0434-\u044d\u0444\u0444\u0435\u043a\u0442\u0430\u043c\u0438 \u0438 API.<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/redux-toolkit.js.org\/api\/createSlice\" rel=\"noopener noreferrer nofollow\">createSlice<\/a> \u0434\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 actions \u0438 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<\/li>\n<\/ul>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440<\/h3>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043d\u0438\u0433 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0438\u0445.<\/p>\n<pre><code class=\"javascript\">\/\/ booksSlice.js import {   createEntityAdapter,   createAsyncThunk,   createSlice } from \"@reduxjs\/toolkit\";  \/\/ \u041d\u0430\u0448 \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440 \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043d\u0438\u0433.  \/\/ createEntityAdapter \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \/\/ \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f, \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043a\u043d\u0438\u0433 \u0432\u043d\u0443\u0442\u0440\u0438 \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440\u0430 \/\/ (addOne, addMany, updateOne, removeOne, setAll, ...),  const books = createEntityAdapter();  \/\/ createAsyncThunk \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c \u043a\u043e\u0434\u043e\u043c. \/\/ \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c\u0438 \u043a API. \/\/ getBooks \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u0430\u043a \u043d\u0430\u0431\u043e\u0440 actions, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u0434\u0438\u0441\u043f\u0430\u0442\u0447\u0438\u0442\u044c \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438. export const getBooks = createAsyncThunk(\"get books\", async () =&gt;    await graphqlAPI(`     query {       books  {         id         title       }     }   `) );  export const addBook = createAsyncThunk(\"add book\", ({ title }) =&gt;   graphqlAPI(`     mutation ($title: string!){       add_book(objects: { title: $title }) {         id         title       }     }   `, { title }) );  \/\/ createSlice \u2014 \u043e\u0431\u0435\u0440\u0442\u043a\u0430 \u043d\u0430\u0434 createAction \u0438 createReducer. \/\/ \u041e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u043e \u0441\u0442\u0435\u0439\u0442\u043e\u043c \u043c\u0443\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e. export const booksSlice = createSlice({   name: \"books\",   initialState: books.getInitialState(),   extraReducers: {     \/\/ \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 getBooks     [getBooks.fulfilled]: (state, action) =&gt; {       \/\/ setAll \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u043d \u0432 createEntityAdapter       books.setAll(state, action.payload);     },     \/\/ \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 addBook     [addBook.fulfilled]: (state, action) =&gt; {       \/\/ addOne \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u043d \u0432 createEntityAdapter       books.addOne(state, action.payload);     },   }, });  \/\/ createEntityAdapter \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043d\u0430\u0431\u043e\u0440 \u0433\u043e\u0442\u043e\u0432\u044b\u0445 \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440\u043e\u0432 \/\/ (selectIds, selectById, selectAll, ...) export const booksSelectors = books.getSelectors((s) =&gt; s.books);  export default booksSlice.reducer;<\/code><\/pre>\n<pre><code class=\"javascript\">\/\/ index.js import { useDispatch, useSelector } from \"react-redux\"; import { getBooks, addBook, booksSelectors } from \".\/booksSlice\";  export function Books() {   const dispatch = useDispatch();    \/\/ booksSelectors.selectAll \u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u043d \u0447\u0435\u0440\u0435\u0437 createEntityAdapter   const books = useSelector(booksSelectors.selectAll);     useEffect(() =&gt; {     dispatch(getBooks());   }, [dispatch]);    return (     &lt;div&gt;       &lt;button onClick={() =&gt; dispatch(addBook({ title: \"New Book\" }))}&gt;         Add book       &lt;\/button&gt;       &lt;ul&gt;         {books.map((b) =&gt; &lt;li key={b.id}&gt;{b.title}&lt;\/li&gt;)}       &lt;\/ul&gt;     &lt;\/div&gt;   ); }<\/code><\/pre>\n<h3>\u0427\u0442\u043e \u0434\u0430\u043b\u044c\u0448\u0435<\/h3>\n<p>\u041f\u043e\u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441 Redux \u043c\u043e\u0436\u043d\u043e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e.<br \/>\u0414\u043b\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0434\u043d\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b:<br \/><code>npx create-react-app my-app --template redux<\/code><\/p>\n<p>\u0414\u043b\u044f Typescript:<br \/><code>npx create-react-app my-app --template redux-typescript<\/code><\/p>\n<p>\u0412\u043c\u0435\u0441\u0442\u043e <code>fetch<\/code> \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439 <a href=\"https:\/\/github.com\/prisma-labs\/graphql-request\" rel=\"noopener noreferrer nofollow\">graphql-request<\/a>.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0431\u044d\u043a\u0435\u043d\u0434 \u043d\u0430 GraphQL \u043c\u043e\u0436\u043d\u043e \u0431\u0435\u0437 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043a\u043e\u0434\u0430.<br \/>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0442 \u0442\u0430\u043a\u0438\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u044b \u043a\u0430\u043a <a href=\"https:\/\/hasura.io\/\" rel=\"noopener noreferrer nofollow\">hasura<\/a> \u0438\u043b\u0438 <a href=\"https:\/\/www.prisma.io\/\" rel=\"noopener noreferrer nofollow\">prisma<\/a>. <\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/524000\/\"> https:\/\/habr.com\/ru\/post\/524000\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/524000\/\">\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c Redux \u0438 GraphQL \u0431\u0435\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f Apollo Client \u0438\u043b\u0438 Relay.<\/p>\n<h3>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 Redux<\/h3>\n<p><a href=\"https:\/\/github.com\/reduxjs\/redux\" rel=\"noopener noreferrer nofollow\">\u0420\u0435\u0434\u0430\u043a\u0441<\/a> \u044d\u0442\u043e \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0432 \u0432\u0438\u0434\u0435 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438. \u041e\u043d \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442, \u0447\u0442\u043e \u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0435\u0441\u0442\u044c \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 <code>state<\/code>. \u0418\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 <code>state<\/code>  \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043d\u0435\u043b\u044c\u0437\u044f. \u042d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0435\u0440\u0435\u0437 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438  <code>reducers<\/code>. <\/p>\n<p>\u041f\u0441\u0435\u0432\u0434\u043e\u043a\u043e\u0434:<\/p>\n<pre><code class=\"javascript\">function dispatch(action) {     state = reducer(state, action); }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0430\u043a\u0442\u0430 \u0438 \u0440\u0435\u0434\u0430\u043a\u0441\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 <code>react-redux<\/code>. \u041e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 <code>state<\/code> \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0438 \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0447\u0435\u0440\u0435\u0437 <code>dispatch<\/code>.<\/p>\n<h3>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 GraphQL<\/h3>\n<p>GraphQL \u0434\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e POST-\u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u043e\u0441\u043e\u0431\u044b\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u043e\u043c body:<\/p>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>REST<\/p>\n<\/th>\n<th>\n<p>GraphQL<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p>GET \/books\/id<\/p>\n<\/td>\n<td>\n<p>POST \/graphql<br \/><code>query{ book(id) {} }<\/code><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>POST \/books<br \/>{\u2026}<\/p>\n<\/td>\n<td>\n<p>POST \/graphql<br \/><code>mutation{ addBook() {...} }<\/code><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>UPDATE \/books\/id<br \/>{\u2026}<\/p>\n<\/td>\n<td>\n<p>POST \/graphql<br \/><code>mutation{ updateBook(id) {...} }<\/code><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>DELETE \/books\/id<\/p>\n<\/td>\n<td>\n<p>POST \/graphql<br \/><code>mutation{ deleteBook(id) {} }<\/code><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3>GraphQL \u0431\u0435\u0437 Apollo Client (\u0438 Relay)<\/h3>\n<p>Apollo-client \u044d\u0442\u043e \u0441\u0430\u043c\u0430\u044f \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 GraphQL. \u041e\u043d\u0430 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0440\u0435\u0448\u0430\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0443 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043a\u044d\u0448\u0430. \u0412\u043c\u0435\u0441\u0442\u043e Redux \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b. \u041e\u0434\u043d\u0430\u043a\u043e \u044d\u0442\u0438 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b \u0434\u0430\u043b\u0435\u043a\u043e \u043d\u0435 \u0442\u0430\u043a\u0438\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0438 \u0443\u0434\u043e\u0431\u043d\u044b\u0435.<\/p>\n<p>\u0412\u043c\u0435\u0441\u0442\u043e Apollo Client \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0442\u0430\u043a\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439:<\/p>\n<pre><code class=\"javascript\">const graphqlAPI = (query, variables) =&gt; fetch(\"\/graphql\", {     method: \"POST\",     body: JSON.stringify({ query, variables }) });<\/code><\/pre>\n<h3>redux-toolkit<\/h3>\n<p>\u042f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <strong>\u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439<\/strong> \u043d\u0430\u0431\u043e\u0440 \u0443\u0442\u0438\u043b\u0438\u0442 \u043e\u0442 redux \u2014 <a href=\"https:\/\/github.com\/reduxjs\/redux-toolkit\" rel=\"noopener noreferrer nofollow\">redux-toolkit<\/a>. \u041e\u043d\u0438 \u0440\u0435\u0448\u0430\u044e\u0442 \u0441\u0430\u043c\u0443\u044e \u0447\u0430\u0441\u0442\u0443\u044e \u043f\u0440\u0435\u0442\u0435\u043d\u0437\u0438\u044e \u043a \u0440\u0435\u0434\u0430\u043a\u0441\u0443 \u2014 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. \u0412 \u044d\u0442\u043e\u0442 \u043d\u0430\u0431\u043e\u0440 \u0432\u0445\u043e\u0434\u0438\u0442:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/redux-toolkit.js.org\/api\/createEntityAdapter\" rel=\"noopener noreferrer nofollow\">createEntityAdapter<\/a> \u0434\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440\u0430\u043c\u0438 \u0438 \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440\u0430\u043c\u0438.<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/redux-toolkit.js.org\/api\/createAsyncThunk\" rel=\"noopener noreferrer nofollow\">createAsyncThunk<\/a> \u0434\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0441\u0430\u0439\u0434-\u044d\u0444\u0444\u0435\u043a\u0442\u0430\u043c\u0438 \u0438 API.<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/redux-toolkit.js.org\/api\/createSlice\" rel=\"noopener noreferrer nofollow\">createSlice<\/a> \u0434\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 actions \u0438 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<\/li>\n<\/ul>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440<\/h3>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043d\u0438\u0433 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0438\u0445.<\/p>\n<pre><code class=\"javascript\">\/\/ booksSlice.js import {   createEntityAdapter,   createAsyncThunk,   createSlice } from \"@reduxjs\/toolkit\";  \/\/ \u041d\u0430\u0448 \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440 \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043d\u0438\u0433.  \/\/ createEntityAdapter \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \/\/ \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f, \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043a\u043d\u0438\u0433 \u0432\u043d\u0443\u0442\u0440\u0438 \u0440\u0435\u0434\u044c\u044e\u0441\u0435\u0440\u0430 \/\/ (addOne, addMany, updateOne, removeOne, setAll, ...),  const books = createEntityAdapter();  \/\/ createAsyncThunk \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c \u043a\u043e\u0434\u043e\u043c. \/\/ \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c\u0438 \u043a API. \/\/ getBooks \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u0430\u043a \u043d\u0430\u0431\u043e\u0440 actions, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u0434\u0438\u0441\u043f\u0430\u0442\u0447\u0438\u0442\u044c \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438. export const getBooks = createAsyncThunk(\"get books\", async () =&gt;    await graphqlAPI(`     query {       books  {         id         title       }     }   `) );  export const addBook = createAsyncThunk(\"add book\", ({ title }) =&gt;   graphqlAPI(`     mutation ($title: string!){       add_book(objects: { title: $title }) {         id         title       }     }   `, { title }) );  \/\/ createSlice \u2014 \u043e\u0431\u0435\u0440\u0442\u043a\u0430 \u043d\u0430\u0434 createAction \u0438 createReducer. \/\/ \u041e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u043e \u0441\u0442\u0435\u0439\u0442\u043e\u043c \u043c\u0443\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e. export const booksSlice = createSlice({   name: \"books\",   initialState: books.getInitialState(),   extraReducers: {     \/\/ \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 getBooks     [getBooks.fulfilled]: (state, action) =&gt; {       \/\/ setAll \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u043d \u0432 createEntityAdapter       books.setAll(state, action.payload);     },     \/\/ \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 addBook     [addBook.fulfilled]: (state, action) =&gt; {       \/\/ addOne \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u043d \u0432 createEntityAdapter       books.addOne(state, action.payload);     },   }, });  \/\/ createEntityAdapter \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043d\u0430\u0431\u043e\u0440 \u0433\u043e\u0442\u043e\u0432\u044b\u0445 \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440\u043e\u0432 \/\/ (selectIds, selectById, selectAll, ...) export const booksSelectors = books.getSelectors((s) =&gt; s.books);  export default booksSlice.reducer;<\/code><\/pre>\n<pre><code class=\"javascript\">\/\/ index.js import { useDispatch, useSelector } from \"react-redux\"; import { getBooks, addBook, booksSelectors } from \".\/booksSlice\";  export function Books() {   const dispatch = useDispatch();    \/\/ booksSelectors.selectAll \u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u043d \u0447\u0435\u0440\u0435\u0437 createEntityAdapter   const books = useSelector(booksSelectors.selectAll);     useEffect(() =&gt; {     dispatch(getBooks());   }, [dispatch]);    return (     &lt;div&gt;       &lt;button onClick={() =&gt; dispatch(addBook({ title: \"New Book\" }))}&gt;         Add book       &lt;\/button&gt;       &lt;ul&gt;         {books.map((b) =&gt; &lt;li key={b.id}&gt;{b.title}&lt;\/li&gt;)}       &lt;\/ul&gt;     &lt;\/div&gt;   ); }<\/code><\/pre>\n<h3>\u0427\u0442\u043e \u0434\u0430\u043b\u044c\u0448\u0435<\/h3>\n<p>\u041f\u043e\u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441 Redux \u043c\u043e\u0436\u043d\u043e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e.<br \/>\u0414\u043b\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0434\u043d\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b:<br \/><code>npx create-react-app my-app --template redux<\/code><\/p>\n<p>\u0414\u043b\u044f Typescript:<br \/><code>npx create-react-app my-app --template redux-typescript<\/code><\/p>\n<p>\u0412\u043c\u0435\u0441\u0442\u043e <code>fetch<\/code> \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439 <a href=\"https:\/\/github.com\/prisma-labs\/graphql-request\" rel=\"noopener noreferrer nofollow\">graphql-request<\/a>.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0431\u044d\u043a\u0435\u043d\u0434 \u043d\u0430 GraphQL \u043c\u043e\u0436\u043d\u043e \u0431\u0435\u0437 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043a\u043e\u0434\u0430.<br \/>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0442 \u0442\u0430\u043a\u0438\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u044b \u043a\u0430\u043a <a href=\"https:\/\/hasura.io\/\" rel=\"noopener noreferrer nofollow\">hasura<\/a> \u0438\u043b\u0438 <a href=\"https:\/\/www.prisma.io\/\" rel=\"noopener noreferrer nofollow\">prisma<\/a>. <\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/524000\/\"> https:\/\/habr.com\/ru\/post\/524000\/<\/a><br \/><\/br><\/br><\/br><\/p>\n<\/p>\n<p><\/br><\/p>\n<\/td>\n<\/tr>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-311660","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/311660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=311660"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/311660\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=311660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=311660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=311660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}