{"id":303278,"date":"2020-05-09T09:00:18","date_gmt":"2020-05-09T09:00:18","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=303278"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=303278","title":{"rendered":"\u041a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c Apollo \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 GraphQL \u0432 Android"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/501184\/\">\n<h3>\u0417\u0430\u0447\u0435\u043c \u0441\u0442\u0430\u0442\u044c\u044f<\/h3>\n<p>  \u041d\u0435\u0434\u0430\u0432\u043d\u043e \u0443 \u043c\u0435\u043d\u044f \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0431\u044d\u043a\u043e\u043c \u043d\u0430 GraphQL. \u0422\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u043e\u0432 \u043f\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0435 \u043d\u0430 Android, \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 REST \u043d\u0435 \u0442\u0430\u043a \u043c\u043d\u043e\u0433\u043e \u0438 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0438\u0437 \u043d\u0438\u0445 \u0443\u0436\u0435 \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b.<\/p>\n<h3>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 GraphQL<\/h3>\n<p>  GraphQL \u2014 \u043c\u043e\u0434\u043d\u0430\u044f \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430 REST API, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0431\u043e\u043b\u0435\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c, \u043e\u0442\u0434\u0430\u0432\u0430\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0443\u0436\u043d\u044b\u0435 \u0432\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<h3>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f<\/h3>\n<p>  \u0414\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0447\u0435\u0440\u0435\u0437 Apollo \u2014 \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 \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442.<\/p>\n<p>  \u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u0440\u0430\u0431\u043e\u0442\u0435. \u041f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043c\u0430\u043d\u0438\u0444\u0435\u0441\u0442 \u043d\u0443\u0436\u043d\u044b\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0441\u0435\u0442\u044c\u044e: <\/p>\n<pre><code class=\"xml\">&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; \/&gt; &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; \/&gt; <\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c apollo. \u0418\u0434\u0451\u043c \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 build.gradle \u0438 \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u0435 dependencies \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u0447\u043a\u0443:<\/p>\n<pre><code class=\"xml\">classpath 'com.apollographql.apollo:apollo-gradle-plugin:2.0.0' <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432 \u0444\u0430\u0439\u043b\u0435 build.gradle \u043c\u043e\u0434\u0443\u043b\u044f app \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"xml\">implementation(&quot;com.apollographql.apollo:apollo-runtime:2.0.0&quot;) implementation &quot;com.apollographql.apollo:apollo-android-support:2.0.0&quot; <\/code><\/pre>\n<p>  \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0432 \u0441\u0430\u043c\u044b\u0439 \u0432\u0435\u0440\u0445 \u0444\u0430\u0439\u043b\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043f\u043b\u0430\u0433\u0438\u043d\u0430: <\/p>\n<pre><code class=\"xml\">apply plugin: 'com.apollographql.apollo' <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043f\u0440\u043e\u0435\u043a\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043a\u043e\u0434\u043e\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e \u043c\u043e\u0434\u0435\u043b\u0435\u0439, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a GraphQL.<\/p>\n<p>  \u0421\u0432\u0435\u0440\u043d\u0435\u043c \u0441\u0440\u0435\u0434\u0443 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438 \u043e\u0442\u043a\u0440\u043e\u0435\u043c \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b. \u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u043f\u0430\u043f\u043a\u0443 \u0441 \u0432\u0430\u0448\u0438\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u043c:<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<pre><code class=\"xml\">cd \/Users\/user\/Desktop\/ProjectName <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0449\u0451 \u043d\u0435\u0442 npm, \u0442\u043e \u0441\u043f\u0435\u0440\u0432\u0430 \u0441\u043a\u0430\u0447\u0430\u0439\u0442\u0435 \u0441 <a href=\"https:\/\/www.npmjs.com\/get-npm\">\u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u0430\u0439\u0442\u0430<\/a><\/p>\n<p>  \u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c apollo-codegen \u2014 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0441\u043a\u0430\u0447\u0430\u0442\u044c schema.json \u2014 \u0444\u0430\u0439\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0441\u043b\u0443\u0436\u0438\u0442 apollo \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u043c \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043c\u043e\u0434\u0435\u043b\u0435\u0439:<\/p>\n<pre><code class=\"xml\">npm install apollo-codegen <\/code><\/pre>\n<p>  \u0421\u043a\u0430\u0447\u0438\u0432\u0430\u0435\u043c schema.json(\u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u0433\u0434\u0435 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043f\u0430\u043f\u043a\u0430 node_modules):<\/p>\n<pre><code class=\"xml\">node_modules\/.bin\/apollo-codegen download-schema https:\/\/\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0432\u0430\u0448\u0435 api\/ --output schema.json <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0432 \u043f\u0430\u043f\u043a\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u0444\u0430\u0439\u043b schema.json. \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c apollo \u0444\u0430\u0439\u043b\u044b \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0435\u0439. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0434\u0435\u043b\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0448\u0430\u0433\u0438.<\/p>\n<p>  \u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u043f\u0430\u043f\u043a\u0443 app \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u0434\u0430\u043b\u0435\u0435 src -&gt; main. \u0417\u0434\u0435\u0441\u044c \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u0430\u043f\u043a\u0443 graphQL. \u0421\u044e\u0434\u0430 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c \u043d\u0430\u0448\u0438 .graphql \u0444\u0430\u0439\u043b\u044b.<\/p>\n<p>  \u041a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u0443\u044e \u043f\u0430\u043f\u043a\u0443 \u0444\u0430\u0439\u043b, \u0441\u043a\u0430\u0447\u0430\u043d\u043d\u044b\u0439 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0448\u0430\u0433\u0435 \u2014 schema.json<br \/>  \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d\u0430, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u043a\u043e\u0434\u0443<\/p>\n<h3>\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430 \u043c\u043e\u0434\u0435\u043b\u0435\u0439<\/h3>\n<p>  \u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0444\u0430\u0439\u043b\u043e\u0432 .graphql. \u0422\u0443\u0442 \u0431\u0443\u0434\u0443\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u044b.<br \/>  \u0412 GraphQL \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0432\u0438\u0434\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432:<br \/>  query \u2014 \u0430\u043d\u0430\u043b\u043e\u0433 GET<br \/>  mutation \u2014 \u0430\u043d\u0430\u043b\u043e\u0433 POST\/PUT\/DELETE<\/p>\n<p>  \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0432\u044b \u0434\u0435\u043b\u0430\u0435\u0442\u0435 \u0441\u0435\u0440\u0432\u0438\u0441 \u0434\u043b\u044f \u0437\u0430\u0441\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u0442\u043e\u044f\u043b\u044c\u0446\u0435\u0432 \u0432 \u043a\u043e\u043c\u043d\u0430\u0442\u0443 \u0432 \u043e\u0442\u0435\u043b\u0435 \u0434\u043b\u044f \u0430\u0434\u043c\u0438\u043d\u0438\u0441\u0442\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u044d\u0442\u043e\u0433\u043e \u043e\u0442\u0435\u043b\u044f. \u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0443\u043c\u0435\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c 3 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041b\u043e\u0433\u0438\u043d\u0438\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f(\u0430\u0434\u043c\u0438\u043d\u0438\u0441\u0442\u0440\u0430\u0442\u043e\u0440\u0430), \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043a\u043e\u043c\u043d\u0430\u0442\u0435 \u043f\u043e id \u0438 \u0437\u0430\u0441\u0435\u043b\u044f\u0442\u044c \u043f\u043e\u0441\u0442\u043e\u044f\u043b\u044c\u0446\u0435\u0432<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0430\u0439\u043b loginUser.graphql \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0439 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 app\/src\/main\/graphQL. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 apollo \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043d\u0430\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u043b\u044f \u043b\u043e\u0433\u0438\u043d\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<p>  \u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430:<\/p>\n<pre><code class=\"xml\">mutation loginUser($email:String!, $password:String!) {     login(         user: {             email: $email,             password: $password         }     ){         email,         token,         refreshToken     } } <\/code><\/pre>\n<p>  \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0444\u0430\u0439\u043b getRoom.graphql, \u0441 \u0435\u0433\u043e \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043a\u043e\u043c\u043d\u0430\u0442\u044b \u043e\u0442\u0435\u043b\u044f: <\/p>\n<pre><code class=\"xml\">query getRoom($room_id: String) {     room(room_id: $room_id) {         title,         room_number,         cheked_in_family_name,         has_minibar     } } <\/code><\/pre>\n<p>  \u0418 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u2014 \u0437\u0430\u0441\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u0442\u043e\u044f\u043b\u044c\u0446\u0435\u0432 checkUserIn.graphql. \u0422\u043e\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 mutation:<\/p>\n<pre><code class=\"xml\">mutation checkInFamily($room_id: String!, $family_name: String!) {     room(         room: {             title: $family_name,             room_id: $room_id         }     ){         room_id,         family_name,         minibar_products{             title,             weight         }     } } <\/code><\/pre>\n<p>  \u0411\u0438\u043b\u0434\u0438\u043c \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u0432\u0438\u0434\u0438\u043c 3 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u044c\u043a\u0438 \u0432 \u043f\u0430\u043f\u043a\u0435 app\/build\/generated\/source\/apollo\/debug\/service: GetRoomQuery, \u0421heckUserInMutation, LoginUserMutation<\/p>\n<h3>\u0412\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/h3>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c singleton \u043a\u043b\u0430\u0441\u0441 NetworkService, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0438\u0442\u044c \u043d\u0430\u043c ApolloClient. \u0412 \u043d\u0451\u043c \u0434\u0435\u043b\u0430\u0435\u043c 2 \u043c\u0435\u0442\u043e\u0434\u0430. getApolloClient() \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0442\u043e\u043a\u0435\u043d\u0430 \u0438\u043b\u0438 \u043a\u0430\u043a\u0438\u0445-\u043b\u0438\u0431\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432, \u0438 getApolloClientWithTokenInterceptor(), \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u043a\u0438\u0434\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043a\u0435\u043d, \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432:<\/p>\n<pre><code class=\"kotlin\">import com.apollographql.apollo.ApolloClient import okhttp3.Interceptor import okhttp3.OkHttpClient import okhttp3.Request  class NetworkService {    fun getApolloClient(): ApolloClient {     val okHttp = OkHttpClient       .Builder()       .build()      return ApolloClient.builder()       .serverUrl(BASE_URL)       .okHttpClient(okHttp)       .build()   }    fun getApolloClientWithTokenInterceptor(token: String): ApolloClient {      val httpClient = OkHttpClient.Builder()       .addInterceptor(Interceptor { chain: Interceptor.Chain -&gt;         val original: Request = chain.request()                  val builder: Request.Builder = original           .newBuilder()           .method(original.method, original.body)                  builder.header(&quot;Authorization&quot;, &quot;Bearer $token&quot;)         return@Interceptor chain.proceed(builder.build())       })       .build()      return ApolloClient.builder()       .serverUrl(BASE_URL)       .okHttpClient(httpClient)       .build()   }    companion object {     private var mInstance: NetworkService? = null      fun getInstance(): NetworkService? {       if (mInstance == null) {         mInstance = NetworkService()       }       return mInstance     }   } } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0438\u0434\u0451\u043c \u0432 \u043d\u0430\u0448\u0435 activity \u0438\u043b\u0438 fragment, \u0442\u0443\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0437\u0430\u043b\u043e\u0433\u0438\u043d\u0435\u043c\u0441\u044f:<\/p>\n<pre><code class=\"kotlin\">  private fun loginUser() {      val client = NetworkService.getInstance()?.getApolloClient()     val loginMutation = LoginUserMutation       .builder()       .email(emailEdit.text.toString())       .password(passwordEdit.text.toString())       .build()      client       ?.mutate(loginMutation)       ?.enqueue(object : ApolloCall.Callback&lt;LoginUserMutation.Data&gt;() {          override fun onResponse(response: Response&lt;LoginUserMutation.Data&gt;) {           if (!response.hasErrors()) {             val token = response.data?.login()?.token()             val email = response.data?.login()?.email()             \/\/\u0414\u0435\u043b\u0430\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u043d\u0435 \u0442\u0440\u043e\u0433\u0430\u044e\u0449\u0438\u0435 ui, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0442\u043e\u043a\u0435\u043d \u0432 \u0411\u0414             runOnUiThread {               \/\/\u0412\u044b\u0432\u043e\u0434\u0438\u043c \u043d\u0430 \u044d\u043a\u0440\u0430\u043d \u0442\u043e, \u0447\u0442\u043e \u0445\u043e\u0442\u0438\u043c             }           }         }          override fun onFailure(e: ApolloException) {}       })   } <\/code><\/pre>\n<p>  \u042d\u0442\u043e \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 mutation. \u0414\u0430\u043d\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043c\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b\u0438 \u0431\u0435\u0437 \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 header. \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 query, \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u043a\u043e\u043c\u043d\u0430\u0442\u0435. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043a\u043e\u043c\u043d\u0430\u0442\u0430 \u043d\u0430\u043c \u043e\u0442\u0434\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u0442\u043e\u043a\u0435\u043d\u043e\u043c. \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c: <\/p>\n<pre><code class=\"kotlin\">  private fun getRoom() {     val token = &quot;123456&quot;     val client = NetworkService.getInstance()       ?.getApolloClientWithTokenIntercetor(token)      val roomId = &quot;123&quot;     val allRoomsQuery = GetRoomQuery(Input.fromNullable(roomId))      client       ?.query(allRoomsQuery)       ?.enqueue(object : ApolloCall.Callback&lt;GetRoomQuery.Data&gt;() {          override fun onResponse(response: Response&lt;GetRoomQuery.Data&gt;) {           if (!response.hasErrors()) {             val familyName = response.data?.room()?.family_name()           }         }          override fun onFailure(e: ApolloException) {}       })   } <\/code><\/pre>\n<p>  \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0434\u043e\u043c\u0430\u0448\u043d\u0435\u0433\u043e \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 \u0441\u0430\u043c\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0437\u0430\u0441\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u0442\u043e\u044f\u043b\u044c\u0446\u0435\u0432. <\/p>\n<p>  \u041f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438:<\/p>\n<p>  <a href=\"https:\/\/www.apollographql.com\/docs\/android\/\">\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f apollo-client \u0434\u043b\u044f Android<br \/>  <\/a><a href=\"https:\/\/github.com\/apollographql\/apollo-android\">Github apollo-client android<\/a><\/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\/501184\/\"> https:\/\/habr.com\/ru\/post\/501184\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/501184\/\">\n<h3>\u0417\u0430\u0447\u0435\u043c \u0441\u0442\u0430\u0442\u044c\u044f<\/h3>\n<p>  \u041d\u0435\u0434\u0430\u0432\u043d\u043e \u0443 \u043c\u0435\u043d\u044f \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0431\u044d\u043a\u043e\u043c \u043d\u0430 GraphQL. \u0422\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u043e\u0432 \u043f\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0435 \u043d\u0430 Android, \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 REST \u043d\u0435 \u0442\u0430\u043a \u043c\u043d\u043e\u0433\u043e \u0438 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0438\u0437 \u043d\u0438\u0445 \u0443\u0436\u0435 \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b.<\/p>\n<h3>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 GraphQL<\/h3>\n<p>  GraphQL \u2014 \u043c\u043e\u0434\u043d\u0430\u044f \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430 REST API, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0431\u043e\u043b\u0435\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c, \u043e\u0442\u0434\u0430\u0432\u0430\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0443\u0436\u043d\u044b\u0435 \u0432\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<h3>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f<\/h3>\n<p>  \u0414\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0447\u0435\u0440\u0435\u0437 Apollo \u2014 \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 \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442.<\/p>\n<p>  \u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u0440\u0430\u0431\u043e\u0442\u0435. \u041f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043c\u0430\u043d\u0438\u0444\u0435\u0441\u0442 \u043d\u0443\u0436\u043d\u044b\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0441\u0435\u0442\u044c\u044e: <\/p>\n<pre><code class=\"xml\">&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; \/&gt; &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; \/&gt; <\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c apollo. \u0418\u0434\u0451\u043c \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 build.gradle \u0438 \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u0435 dependencies \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u0447\u043a\u0443:<\/p>\n<pre><code class=\"xml\">classpath 'com.apollographql.apollo:apollo-gradle-plugin:2.0.0' <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432 \u0444\u0430\u0439\u043b\u0435 build.gradle \u043c\u043e\u0434\u0443\u043b\u044f app \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"xml\">implementation(&quot;com.apollographql.apollo:apollo-runtime:2.0.0&quot;) implementation &quot;com.apollographql.apollo:apollo-android-support:2.0.0&quot; <\/code><\/pre>\n<p>  \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0432 \u0441\u0430\u043c\u044b\u0439 \u0432\u0435\u0440\u0445 \u0444\u0430\u0439\u043b\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043f\u043b\u0430\u0433\u0438\u043d\u0430: <\/p>\n<pre><code class=\"xml\">apply plugin: 'com.apollographql.apollo' <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043f\u0440\u043e\u0435\u043a\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043a\u043e\u0434\u043e\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e \u043c\u043e\u0434\u0435\u043b\u0435\u0439, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a GraphQL.<\/p>\n<p>  \u0421\u0432\u0435\u0440\u043d\u0435\u043c \u0441\u0440\u0435\u0434\u0443 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438 \u043e\u0442\u043a\u0440\u043e\u0435\u043c \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b. \u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u043f\u0430\u043f\u043a\u0443 \u0441 \u0432\u0430\u0448\u0438\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u043c:  <\/p>\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-303278","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/303278","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=303278"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/303278\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=303278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=303278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=303278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}