{"id":415737,"date":"2024-06-30T00:32:49","date_gmt":"2024-06-30T00:32:49","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=415737"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-06-30T00:32:49","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=415737","title":{"rendered":"<span>\u041a\u0430\u043a \u0441\u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c GraphQL request \u0441 \u0438\u043d\u0442\u0435\u0440\u0441\u0435\u043f\u0442\u043e\u0440\u0430\u043c\u0438 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 JWT \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<ol>\n<li>\n<p><strong>GraphQL request<\/strong> &#8212; \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u044b\u0439 \u0438 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 graphql \u043a\u043b\u0438\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u0441\u043e\u0447\u0435\u0442\u0430\u0442\u044c \u0441 \u043b\u044e\u0431\u044b\u043c \u0441\u0442\u0435\u0439\u0442 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u043e\u043c.<\/p>\n<\/li>\n<li>\n<p><strong>\u0418\u043d\u0442\u0435\u0440\u0441\u0435\u043f\u0442\u043e\u0440\u044b<\/strong> &#8212; \u0443\u0434\u043e\u0431\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043e\u0442\u0432\u0435\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0448\u0438\u0440\u043e\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0445\u043e\u0440\u043e\u0448\u043e \u0443\u043f\u0430\u043a\u043e\u0432\u0430\u043d\u043d\u044b\u043c\u0438 http \u043a\u043b\u0438\u0435\u043d\u0442\u0430\u043c\u0438 \u0442\u0438\u043f\u0430 axios.<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0440\u0430\u043c\u043a\u0430\u0445 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u0430 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0439 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 <strong>GraphQL request<\/strong> \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435\u00a0 \u043f\u0440\u043e\u0431\u0440\u043e\u0441\u0430 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 c access \u0442\u043e\u043a\u0435\u043d\u043e\u043c \u0438 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430 401 \u043e\u0448\u0438\u0431\u043a\u0438 \u043e\u0442\u0432\u0435\u0442\u0430 \u0434\u043b\u044f \u0440\u0435\u0444\u0440\u0435\u0448\u0430 \u044d\u0442\u043e\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430<\/p>\n<\/li>\n<\/ol>\n<p>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u043f\u043e \u043f\u0430\u043a\u0435\u0442\u0443: https:\/\/www.npmjs.com\/package\/graphql-request<\/p>\n<p>\u0418\u0442\u0430\u043a, \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c.\u00a0<\/p>\n<p><strong>\u0428\u0430\u0433 1. \u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043f\u0430\u043a\u0435\u0442\u00a0<\/strong><\/p>\n<p><code>yarn add graphql-request graphql<\/code><\/p>\n<p><strong>\u0428\u0430\u0433 2. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/strong><\/p>\n<pre><code>export class GQLContext {      private client: GraphQLClient     private snapshot: RequestSnapshot;     private readonly requestInterceptor = new RequestStrategy();     private readonly responseInterceptor = new ResponseStrategy();      public req: GQLRequest;     public res: GQLResponse;     public isRepeated = false;      constructor(client: GraphQLClient) {         this.client = client     }      async setRequest(req: GQLRequest) {         this.req = req         await this.requestInterceptor.handle(this)     }      async setResponse(res: GQLResponse) {         this.res = res         await this.responseInterceptor.handle(this)     }      async sendRequest(): Promise&lt;GQLResponse> {         if (!this.snapshot) {             this.createSnapshot()         }         const res = await this.client.rawRequest.apply(this.client, new NativeRequestAdapter(this)) as GQLResponse         await this.setResponse(res)          return this.res     }      async redo(): Promise&lt;GQLResponse> {         await this.snapshot.restore()         this.isRepeated = true         return await this.sendRequest()     }       createSnapshot() {         this.snapshot = new RequestSnapshot(this)     } }<\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0435, \u043e\u0442\u0432\u0435\u0442\u0435 ( \u043f\u0440\u0438 \u0435\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 ), \u0430 \u0442\u0430\u043a \u0436\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0440\u0435\u0444\u0435\u0440\u0435\u043d\u0441 \u043d\u0430 \u0441\u0430\u043c GQL \u043a\u043b\u0438\u0435\u043d\u0442.\u00a0<\/p>\n<p>\u0414\u043b\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u0432\u0430 \u0431\u0438\u043b\u0434 \u043c\u0435\u0442\u043e\u0434\u0430: <strong>setRequest<\/strong> \u0438 <strong>setResponse<\/strong>. \u041a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043d\u0438\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0441\u0435\u043f\u0442\u043e\u0440\u043e\u0432, \u043a\u0430\u0436\u0434\u0443\u044e \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0438\u0436\u0435.<\/p>\n<p><strong>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0441\u043d\u044d\u043f\u0448\u043e\u0442\u0430:<\/strong><\/p>\n<pre><code>export class RequestSnapshot {      instance: GQLContext;     init: GQLRequest;      constructor(ctx: GQLContext) {         this.instance = ctx         this.init = ctx.req     }      async restore() {         await this.instance.setRequest(this.init)     } }<\/code><\/pre>\n<p>\u0421\u043d\u044d\u043f\u0448\u043e\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0440\u0435\u0444\u0435\u0440\u0435\u043d\u0441 \u043d\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u0430 \u0442\u0430\u043a \u0436\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f (\u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438) \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 restore\u00a0<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <strong>sendRequest<\/strong> \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0443\u0436\u0438\u0442\u044c \u043e\u0431\u0435\u0440\u0442\u043a\u043e\u0439 \u0434\u043b\u044f gql-request, \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u043d\u044d\u043f\u0448\u043e\u0442\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 <strong>createSnapshot<\/strong><\/p>\n<p><strong>NativeRequestAdapter<\/strong> &#8212; \u0430\u0434\u0430\u043f\u0442\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043b\u0443\u0436\u0438\u0442 \u0434\u043b\u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043a \u0432\u0438\u0434\u0443, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0443\u043c\u0435\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 gql-request:<\/p>\n<pre><code>export function NativeRequestAdapter (ctx: GQLContext){     return Array.of(ctx.req.type, ctx.req.variables, ctx.req.headers) }<\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 <strong>redo<\/strong> \u0441\u043b\u0443\u0436\u0438\u0442 \u0434\u043b\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u044f \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0442\u0440\u0435\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439:<\/p>\n<ol>\n<li>\n<p>\u0412\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/p>\n<\/li>\n<li>\n<p>\u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0444\u043b\u0430\u0433, \u0433\u043e\u0432\u043e\u0440\u044f\u0449\u0438\u0439 \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0439<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 3. \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f \u043e\u0448\u0438\u0431\u043a\u0438<\/strong><\/p>\n<pre><code>export class GraphQLError extends Error {     code: number;      constructor(message: string, code: number) {         super(message)         this.code = code     } }<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u043e\u0448\u0438\u0431\u043a\u0438 JS, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u0442\u0443\u0434\u0430 \u043a\u043e\u0434 \u043e\u0442\u0432\u0435\u0442\u0430.\u00a0<\/p>\n<p><strong>\u0428\u0430\u0433 4. \u041f\u0438\u0448\u0435\u043c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u044e \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a\u0430<\/strong><\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a\u0430 \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u00ab\u0426\u0435\u043f\u043e\u0447\u043a\u0430 \u043e\u0431\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u0435\u0439(<strong>\u0421oR<\/strong>)\u00bb. \u0414\u0430\u043d\u043d\u044b\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043f\u043e \u0446\u0435\u043f\u043e\u0447\u043a\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432, \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0435\u0448\u0430\u0435\u0442, \u043a\u0430\u043a \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u043c\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 (\u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0431\u0443\u0434\u0435\u0442 \u044f\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u043d\u0430\u0448 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430), \u0430 \u0442\u0430\u043a \u0436\u0435 \u0441\u0442\u043e\u0438\u0442 \u043b\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u043e \u0446\u0435\u043f\u0438.<\/p>\n<p>\u0418\u0442\u0430\u043a, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u044d\u0442\u0443 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044e \u0447\u0443\u0442\u044c \u0431\u043b\u0438\u0436\u0435:\u00a0<\/p>\n<pre><code>export type GQLRequest = {     type: string;     variables?: any;     headers?: Record&lt;string, string> } export type GQLResponse = {     data: any     extensions?: any     headers: Headers,     status: number     errors?: any[]; }   interface Interceptor {     setNext(interceptor: Interceptor): Interceptor;      intercept(type: GQLContext): Promise&lt;GQLContext>; }  export abstract class AbstractInterceptor implements Interceptor {      private nextHandler: Interceptor;      public setNext(interceptor: Interceptor): Interceptor {         this.nextHandler = interceptor         return interceptor     }      public async intercept(ctx: GQLContext) {         if (this.nextHandler) return await this.nextHandler.intercept(ctx)          return ctx     }  }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430:\u00a0<\/p>\n<ol>\n<li>\n<p><strong>setNext &#8212; <\/strong>\u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u0434\u043b\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a\u0430 \u0432 \u0446\u0435\u043f\u0438, \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u00a0 \u0432 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0435 <strong>nextHandler<\/strong>\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>intercept &#8212; <\/strong>\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d<strong>\u00a0<\/strong> \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443. \u042d\u0442\u0438\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 5. \u0418\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/strong><\/p>\n<pre><code>export class AuthInterceptor extends AbstractInterceptor{     intercept(ctx: GQLContext): Promise&lt;GQLContext> {         if (typeof window !== 'undefined') {              const token = window.localStorage.getItem('token')             if (!!token &amp;&amp; token !== 'undefined') {                 ctx.req.headers = {                 ...ctx.req.headers,                  Authorization: `Bearer ${token}`                 }             }         }         return super.intercept(ctx)      }  }<\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a \u0434\u043e\u0441\u0442\u0430\u0435\u0442 access token \u0438\u0437 localStorage \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0441 \u0442\u043e\u043a\u0435\u043d\u043e\u043c<\/p>\n<p><strong>\u0428\u0430\u0433 6. \u0418\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a\u0430 \u043e\u0442\u0432\u0435\u0442\u043e\u0432\u00a0<\/strong><\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442 401 \u043e\u0448\u0438\u0431\u043a\u0438 \u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f, \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0440\u0435\u0444\u0440\u0435\u0448 \u0442\u043e\u043a\u0435\u043d\u0430 \u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u0438\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441.\u00a0<\/p>\n<pre><code>export const REFRESH_TOKEN = gql`     query refreshToken {         refreshToken{             access_token         }     } `  export class HandleRefreshToken extends AbstractInterceptor {     async intercept(ctx: GQLContext): Promise&lt;GQLContext> {          if ( !('errors' in ctx.res)) return await super.intercept(ctx)          const exception = ctx.res.errors[0]?.extensions?.exception          if (!exception) return await super.intercept(ctx)          const Error = new GraphQLError(exception.message, exception.status)         if (Error.code === 401 &amp;&amp; !ctx.isRepeated &amp;&amp; typeof window !== 'undefined') {             try {                 await ctx.setRequest({type: REFRESH_TOKEN})                 const res = await ctx.sendRequest()                 \/\/@ts-ignore                 localStorage.setItem('token', res.refreshToken.access_token)                 await ctx.redo()                  return await super.intercept(ctx)             } catch (e) {                 throw Error             }         }         throw Error     } }<\/code><\/pre>\n<ol>\n<li>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0435\u0441\u0442\u044c \u043b\u0438 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443. \u0415\u0441\u043b\u0438 \u0434\u0430, \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0431\u0440\u043e\u0448\u0435\u043d\u043d\u044b\u0439 \u044d\u043a\u0441\u0435\u043f\u0448\u043d.<\/p>\n<\/li>\n<li>\n<p>\u0418\u0437 \u044d\u043a\u0441\u0435\u043f\u0448\u0435\u043d\u0430 \u0434\u043e\u0441\u0442\u0430\u0435\u043c \u0441\u0442\u0430\u0442\u0443\u0441 \u043e\u0442\u0432\u0435\u0442\u0430 \u0438 \u043a\u043e\u0434 \u043e\u0448\u0438\u0431\u043a\u0438.\u00a0<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0435\u0441\u043b\u0438 \u043a\u043e\u0434 \u043e\u0448\u0438\u0431\u043a\u0438 401, \u0442\u043e \u0434\u0435\u043b\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0440\u0435\u0444\u0440\u0435\u0448 \u0442\u043e\u043a\u0435\u043d\u0430,\u00a0 \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439 access token \u0432 localStorage<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 <strong>redo, <\/strong>\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u043b\u0438 \u0440\u0430\u043d\u0435\u0435.\u00a0<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0434\u0430\u043d\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0448\u043b\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e, \u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443.\u00a0\u0412 \u0438\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443 \u0438 \u043f\u0440\u0435\u043a\u0440\u0430\u0449\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443.<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 7. \u041f\u0438\u0448\u0435\u043c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u044e \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438<\/strong><\/p>\n<pre><code>export abstract class InterceptStrategy {      protected makeChain(collection: AbstractInterceptor[]) {         collection.forEach((handler, index) => collection[index + 1] &amp;&amp; handler.setNext(collection[index + 1]))     }      abstract handle(ctx: GQLContext): any; }<\/code><\/pre>\n<p>\u0410\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430 \u0434\u0432\u0443\u043c\u044f \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438:\u00a0<\/p>\n<ol>\n<li>\n<p><strong>makeChain<\/strong> &#8212; \u0445\u0435\u043b\u043f\u0435\u0440, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0443\u0434\u043e\u0431\u043d\u043e \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0438\u0437 \u043c\u0430\u0441\u0441\u0438\u0432\u0430<\/p>\n<\/li>\n<li>\n<p><strong>Handle<\/strong> &#8212; \u043c\u0435\u0442\u043e\u0434, \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043e\u0441\u043d\u043e\u0432\u043d\u0443\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u0435\u0433\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f\u0445<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 8. \u0418\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0439 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043e\u0442\u0432\u0435\u0442\u043e\u0432<\/strong><\/p>\n<pre><code>export class RequestStrategy extends InterceptStrategy{      async handle(ctx: GQLContext): Promise&lt;GQLContext> {         const handlersOrder: AbstractInterceptor[] = [             new AuthInterceptor(),         ]         this.makeChain(handlersOrder)          return await handlersOrder[0].intercept(ctx)     } }<\/code><\/pre>\n<pre><code>export class ResponseStrategy extends InterceptStrategy{      async handle(ctx: GQLContext): Promise&lt;GQLResponse['data']> {         const handlersOrder: AbstractInterceptor[] = [             new HandleRefreshToken(),             new RetrieveDataInterceptor(),         ]         this.makeChain(handlersOrder)          return await handlersOrder[0].intercept(ctx)     } }<\/code><\/pre>\n<p>\u041a\u0430\u043a \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u043e\u0431\u0435 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u043f\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u043e. \u0418\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434 handle, \u043a\u043e\u0442\u043e\u0440\u044b\u0439:\u00a0<\/p>\n<ol>\n<li>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0432\u044b\u0437\u043e\u0432\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u0442 \u0438\u0437 \u043d\u0438\u0445 \u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 makeChain<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438\u00a0<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 9. \u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u0432\u043c\u0435\u0441\u0442\u0435.\u00a0<\/strong><\/p>\n<pre><code>const request = async function (this: GraphQLClient, type: string, variables: any, headers = {}): Promise&lt;any> {      const ctx = new GQLContext(this)     await ctx.setRequest({type, variables, headers})     try {         await ctx.sendRequest()     } catch (e) {         await ctx.setResponse(e.response)     }      return ctx.res }  GraphQLClient.prototype.request = request  export const client = new GraphQLClient('http:\/\/localhost:4000\/graphql', {     credentials: 'include', })<\/code><\/pre>\n<ol>\n<li>\n<p>\u041f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 request, \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0439 \u043f\u0430\u043a\u0435\u0442\u043e\u043c.\u00a0<\/p>\n<\/li>\n<li>\n<p>\u0412\u043d\u0443\u0442\u0440\u0438 \u043d\u0430\u0448\u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442<\/p>\n<\/li>\n<li>\n<p>\u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/p>\n<\/li>\n<li>\n<p>\u041e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441, \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043e\u0442\u0432\u0435\u0442<\/p>\n<\/li>\n<li>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442\u0432\u0435\u0442\u0430<\/p>\n<\/li>\n<li>\n<p>\u042d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0438\u0435\u043d\u0442<\/p>\n<\/li>\n<\/ol>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, GQL \u0433\u043e\u0442\u043e\u0432 \u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e.<\/p>\n<p>\u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044e \u0437\u0430 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0435. \u0411\u0443\u0434\u0443 \u0440\u0430\u0434 \u0412\u0430\u0448\u0435\u043c\u0443 \u0444\u0438\u0434\u0431\u044d\u043a\u0443. <\/p>\n<p><a href=\"https:\/\/github.com\/IAlexanderI1994\/gql-request-article\" rel=\"noopener noreferrer nofollow\">Cc\u044b\u043b\u043a\u0430 \u043d\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \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\/articles\/574592\/\"> https:\/\/habr.com\/ru\/articles\/574592\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<ol>\n<li>\n<p><strong>GraphQL request<\/strong> &#8212; \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u044b\u0439 \u0438 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 graphql \u043a\u043b\u0438\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u0441\u043e\u0447\u0435\u0442\u0430\u0442\u044c \u0441 \u043b\u044e\u0431\u044b\u043c \u0441\u0442\u0435\u0439\u0442 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u043e\u043c.<\/p>\n<\/li>\n<li>\n<p><strong>\u0418\u043d\u0442\u0435\u0440\u0441\u0435\u043f\u0442\u043e\u0440\u044b<\/strong> &#8212; \u0443\u0434\u043e\u0431\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043e\u0442\u0432\u0435\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0448\u0438\u0440\u043e\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0445\u043e\u0440\u043e\u0448\u043e \u0443\u043f\u0430\u043a\u043e\u0432\u0430\u043d\u043d\u044b\u043c\u0438 http \u043a\u043b\u0438\u0435\u043d\u0442\u0430\u043c\u0438 \u0442\u0438\u043f\u0430 axios.<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0440\u0430\u043c\u043a\u0430\u0445 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u0430 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0439 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 <strong>GraphQL request<\/strong> \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435\u00a0 \u043f\u0440\u043e\u0431\u0440\u043e\u0441\u0430 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 c access \u0442\u043e\u043a\u0435\u043d\u043e\u043c \u0438 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430 401 \u043e\u0448\u0438\u0431\u043a\u0438 \u043e\u0442\u0432\u0435\u0442\u0430 \u0434\u043b\u044f \u0440\u0435\u0444\u0440\u0435\u0448\u0430 \u044d\u0442\u043e\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430<\/p>\n<\/li>\n<\/ol>\n<p>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u043f\u043e \u043f\u0430\u043a\u0435\u0442\u0443: https:\/\/www.npmjs.com\/package\/graphql-request<\/p>\n<p>\u0418\u0442\u0430\u043a, \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c.\u00a0<\/p>\n<p><strong>\u0428\u0430\u0433 1. \u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043f\u0430\u043a\u0435\u0442\u00a0<\/strong><\/p>\n<p><code>yarn add graphql-request graphql<\/code><\/p>\n<p><strong>\u0428\u0430\u0433 2. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/strong><\/p>\n<pre><code>export class GQLContext {      private client: GraphQLClient     private snapshot: RequestSnapshot;     private readonly requestInterceptor = new RequestStrategy();     private readonly responseInterceptor = new ResponseStrategy();      public req: GQLRequest;     public res: GQLResponse;     public isRepeated = false;      constructor(client: GraphQLClient) {         this.client = client     }      async setRequest(req: GQLRequest) {         this.req = req         await this.requestInterceptor.handle(this)     }      async setResponse(res: GQLResponse) {         this.res = res         await this.responseInterceptor.handle(this)     }      async sendRequest(): Promise&lt;GQLResponse> {         if (!this.snapshot) {             this.createSnapshot()         }         const res = await this.client.rawRequest.apply(this.client, new NativeRequestAdapter(this)) as GQLResponse         await this.setResponse(res)          return this.res     }      async redo(): Promise&lt;GQLResponse> {         await this.snapshot.restore()         this.isRepeated = true         return await this.sendRequest()     }       createSnapshot() {         this.snapshot = new RequestSnapshot(this)     } }<\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0435, \u043e\u0442\u0432\u0435\u0442\u0435 ( \u043f\u0440\u0438 \u0435\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 ), \u0430 \u0442\u0430\u043a \u0436\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0440\u0435\u0444\u0435\u0440\u0435\u043d\u0441 \u043d\u0430 \u0441\u0430\u043c GQL \u043a\u043b\u0438\u0435\u043d\u0442.\u00a0<\/p>\n<p>\u0414\u043b\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u0432\u0430 \u0431\u0438\u043b\u0434 \u043c\u0435\u0442\u043e\u0434\u0430: <strong>setRequest<\/strong> \u0438 <strong>setResponse<\/strong>. \u041a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043d\u0438\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0441\u0435\u043f\u0442\u043e\u0440\u043e\u0432, \u043a\u0430\u0436\u0434\u0443\u044e \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0438\u0436\u0435.<\/p>\n<p><strong>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0441\u043d\u044d\u043f\u0448\u043e\u0442\u0430:<\/strong><\/p>\n<pre><code>export class RequestSnapshot {      instance: GQLContext;     init: GQLRequest;      constructor(ctx: GQLContext) {         this.instance = ctx         this.init = ctx.req     }      async restore() {         await this.instance.setRequest(this.init)     } }<\/code><\/pre>\n<p>\u0421\u043d\u044d\u043f\u0448\u043e\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0440\u0435\u0444\u0435\u0440\u0435\u043d\u0441 \u043d\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u0430 \u0442\u0430\u043a \u0436\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f (\u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438) \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 restore\u00a0<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <strong>sendRequest<\/strong> \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0443\u0436\u0438\u0442\u044c \u043e\u0431\u0435\u0440\u0442\u043a\u043e\u0439 \u0434\u043b\u044f gql-request, \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u043d\u044d\u043f\u0448\u043e\u0442\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 <strong>createSnapshot<\/strong><\/p>\n<p><strong>NativeRequestAdapter<\/strong> &#8212; \u0430\u0434\u0430\u043f\u0442\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043b\u0443\u0436\u0438\u0442 \u0434\u043b\u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043a \u0432\u0438\u0434\u0443, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0443\u043c\u0435\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 gql-request:<\/p>\n<pre><code>export function NativeRequestAdapter (ctx: GQLContext){     return Array.of(ctx.req.type, ctx.req.variables, ctx.req.headers) }<\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 <strong>redo<\/strong> \u0441\u043b\u0443\u0436\u0438\u0442 \u0434\u043b\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u044f \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0442\u0440\u0435\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439:<\/p>\n<ol>\n<li>\n<p>\u0412\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/p>\n<\/li>\n<li>\n<p>\u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0444\u043b\u0430\u0433, \u0433\u043e\u0432\u043e\u0440\u044f\u0449\u0438\u0439 \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0439<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 3. \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f \u043e\u0448\u0438\u0431\u043a\u0438<\/strong><\/p>\n<pre><code>export class GraphQLError extends Error {     code: number;      constructor(message: string, code: number) {         super(message)         this.code = code     } }<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u043e\u0448\u0438\u0431\u043a\u0438 JS, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u0442\u0443\u0434\u0430 \u043a\u043e\u0434 \u043e\u0442\u0432\u0435\u0442\u0430.\u00a0<\/p>\n<p><strong>\u0428\u0430\u0433 4. \u041f\u0438\u0448\u0435\u043c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u044e \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a\u0430<\/strong><\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a\u0430 \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u00ab\u0426\u0435\u043f\u043e\u0447\u043a\u0430 \u043e\u0431\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u0435\u0439(<strong>\u0421oR<\/strong>)\u00bb. \u0414\u0430\u043d\u043d\u044b\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043f\u043e \u0446\u0435\u043f\u043e\u0447\u043a\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432, \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0435\u0448\u0430\u0435\u0442, \u043a\u0430\u043a \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u043c\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 (\u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0431\u0443\u0434\u0435\u0442 \u044f\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u043d\u0430\u0448 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430), \u0430 \u0442\u0430\u043a \u0436\u0435 \u0441\u0442\u043e\u0438\u0442 \u043b\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u043e \u0446\u0435\u043f\u0438.<\/p>\n<p>\u0418\u0442\u0430\u043a, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u044d\u0442\u0443 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044e \u0447\u0443\u0442\u044c \u0431\u043b\u0438\u0436\u0435:\u00a0<\/p>\n<pre><code>export type GQLRequest = {     type: string;     variables?: any;     headers?: Record&lt;string, string> } export type GQLResponse = {     data: any     extensions?: any     headers: Headers,     status: number     errors?: any[]; }   interface Interceptor {     setNext(interceptor: Interceptor): Interceptor;      intercept(type: GQLContext): Promise&lt;GQLContext>; }  export abstract class AbstractInterceptor implements Interceptor {      private nextHandler: Interceptor;      public setNext(interceptor: Interceptor): Interceptor {         this.nextHandler = interceptor         return interceptor     }      public async intercept(ctx: GQLContext) {         if (this.nextHandler) return await this.nextHandler.intercept(ctx)          return ctx     }  }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430:\u00a0<\/p>\n<ol>\n<li>\n<p><strong>setNext &#8212; <\/strong>\u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u0434\u043b\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a\u0430 \u0432 \u0446\u0435\u043f\u0438, \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u00a0 \u0432 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0435 <strong>nextHandler<\/strong>\u00a0<\/p>\n<\/li>\n<li>\n<p><strong>intercept &#8212; <\/strong>\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d<strong>\u00a0<\/strong> \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443. \u042d\u0442\u0438\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 5. \u0418\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/strong><\/p>\n<pre><code>export class AuthInterceptor extends AbstractInterceptor{     intercept(ctx: GQLContext): Promise&lt;GQLContext> {         if (typeof window !== 'undefined') {              const token = window.localStorage.getItem('token')             if (!!token &amp;&amp; token !== 'undefined') {                 ctx.req.headers = {                 ...ctx.req.headers,                  Authorization: `Bearer ${token}`                 }             }         }         return super.intercept(ctx)      }  }<\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a \u0434\u043e\u0441\u0442\u0430\u0435\u0442 access token \u0438\u0437 localStorage \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0441 \u0442\u043e\u043a\u0435\u043d\u043e\u043c<\/p>\n<p><strong>\u0428\u0430\u0433 6. \u0418\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a\u0430 \u043e\u0442\u0432\u0435\u0442\u043e\u0432\u00a0<\/strong><\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442 401 \u043e\u0448\u0438\u0431\u043a\u0438 \u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f, \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0440\u0435\u0444\u0440\u0435\u0448 \u0442\u043e\u043a\u0435\u043d\u0430 \u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u0438\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441.\u00a0<\/p>\n<pre><code>export const REFRESH_TOKEN = gql`     query refreshToken {         refreshToken{             access_token         }     } `  export class HandleRefreshToken extends AbstractInterceptor {     async intercept(ctx: GQLContext): Promise&lt;GQLContext> {          if ( !('errors' in ctx.res)) return await super.intercept(ctx)          const exception = ctx.res.errors[0]?.extensions?.exception          if (!exception) return await super.intercept(ctx)          const Error = new GraphQLError(exception.message, exception.status)         if (Error.code === 401 &amp;&amp; !ctx.isRepeated &amp;&amp; typeof window !== 'undefined') {             try {                 await ctx.setRequest({type: REFRESH_TOKEN})                 const res = await ctx.sendRequest()                 \/\/@ts-ignore                 localStorage.setItem('token', res.refreshToken.access_token)                 await ctx.redo()                  return await super.intercept(ctx)             } catch (e) {                 throw Error             }         }         throw Error     } }<\/code><\/pre>\n<ol>\n<li>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0435\u0441\u0442\u044c \u043b\u0438 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443. \u0415\u0441\u043b\u0438 \u0434\u0430, \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0431\u0440\u043e\u0448\u0435\u043d\u043d\u044b\u0439 \u044d\u043a\u0441\u0435\u043f\u0448\u043d.<\/p>\n<\/li>\n<li>\n<p>\u0418\u0437 \u044d\u043a\u0441\u0435\u043f\u0448\u0435\u043d\u0430 \u0434\u043e\u0441\u0442\u0430\u0435\u043c \u0441\u0442\u0430\u0442\u0443\u0441 \u043e\u0442\u0432\u0435\u0442\u0430 \u0438 \u043a\u043e\u0434 \u043e\u0448\u0438\u0431\u043a\u0438.\u00a0<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0435\u0441\u043b\u0438 \u043a\u043e\u0434 \u043e\u0448\u0438\u0431\u043a\u0438 401, \u0442\u043e \u0434\u0435\u043b\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0440\u0435\u0444\u0440\u0435\u0448 \u0442\u043e\u043a\u0435\u043d\u0430,\u00a0 \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439 access token \u0432 localStorage<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 <strong>redo, <\/strong>\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u043b\u0438 \u0440\u0430\u043d\u0435\u0435.\u00a0<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0434\u0430\u043d\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0448\u043b\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e, \u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443.\u00a0\u0412 \u0438\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443 \u0438 \u043f\u0440\u0435\u043a\u0440\u0430\u0449\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443.<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 7. \u041f\u0438\u0448\u0435\u043c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u044e \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438<\/strong><\/p>\n<pre><code>export abstract class InterceptStrategy {      protected makeChain(collection: AbstractInterceptor[]) {         collection.forEach((handler, index) => collection[index + 1] &amp;&amp; handler.setNext(collection[index + 1]))     }      abstract handle(ctx: GQLContext): any; }<\/code><\/pre>\n<p>\u0410\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430 \u0434\u0432\u0443\u043c\u044f \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438:\u00a0<\/p>\n<ol>\n<li>\n<p><strong>makeChain<\/strong> &#8212; \u0445\u0435\u043b\u043f\u0435\u0440, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0443\u0434\u043e\u0431\u043d\u043e \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0438\u0437 \u043c\u0430\u0441\u0441\u0438\u0432\u0430<\/p>\n<\/li>\n<li>\n<p><strong>Handle<\/strong> &#8212; \u043c\u0435\u0442\u043e\u0434, \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043e\u0441\u043d\u043e\u0432\u043d\u0443\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u0435\u0433\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f\u0445<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 8. \u0418\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0439 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043e\u0442\u0432\u0435\u0442\u043e\u0432<\/strong><\/p>\n<pre><code>export class RequestStrategy extends InterceptStrategy{      async handle(ctx: GQLContext): Promise&lt;GQLContext> {         const handlersOrder: AbstractInterceptor[] = [             new AuthInterceptor(),         ]         this.makeChain(handlersOrder)          return await handlersOrder[0].intercept(ctx)     } }<\/code><\/pre>\n<pre><code>export class ResponseStrategy extends InterceptStrategy{      async handle(ctx: GQLContext): Promise&lt;GQLResponse['data']> {         const handlersOrder: AbstractInterceptor[] = [             new HandleRefreshToken(),             new RetrieveDataInterceptor(),         ]         this.makeChain(handlersOrder)          return await handlersOrder[0].intercept(ctx)     } }<\/code><\/pre>\n<p>\u041a\u0430\u043a \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u043e\u0431\u0435 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u043f\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u043e. \u0418\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434 handle, \u043a\u043e\u0442\u043e\u0440\u044b\u0439:\u00a0<\/p>\n<ol>\n<li>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0432\u044b\u0437\u043e\u0432\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u0442 \u0438\u0437 \u043d\u0438\u0445 \u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 makeChain<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438\u00a0<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 9. \u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u0432\u043c\u0435\u0441\u0442\u0435.\u00a0<\/strong><\/p>\n<pre><code>const request = async function (this: GraphQLClient, type: string, variables: any, headers = {}): Promise&lt;any> {      const ctx = new GQLContext(this)     await ctx.setRequest({type, variables, headers})     try {         await ctx.sendRequest()     } catch (e) {         await ctx.setResponse(e.response)     }      return ctx.res }  GraphQLClient.prototype.request = request  export const client = new GraphQLClient('http:\/\/localhost:4000\/graphql', {     credentials: 'include', })<\/code><\/pre>\n<ol>\n<li>\n<p>\u041f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 request, \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0439 \u043f\u0430\u043a\u0435\u0442\u043e\u043c.\u00a0<\/p>\n<\/li>\n<li>\n<p>\u0412\u043d\u0443\u0442\u0440\u0438 \u043d\u0430\u0448\u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442<\/p>\n<\/li>\n<li>\n<p>\u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/p>\n<\/li>\n<li>\n<p>\u041e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441, \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043e\u0442\u0432\u0435\u0442<\/p>\n<\/li>\n<li>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442\u0432\u0435\u0442\u0430<\/p>\n<\/li>\n<li>\n<p>\u042d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0438\u0435\u043d\u0442<\/p>\n<\/li>\n<\/ol>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, GQL \u0433\u043e\u0442\u043e\u0432 \u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e.<\/p>\n<p>\u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044e \u0437\u0430 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0435. \u0411\u0443\u0434\u0443 \u0440\u0430\u0434 \u0412\u0430\u0448\u0435\u043c\u0443 \u0444\u0438\u0434\u0431\u044d\u043a\u0443. <\/p>\n<p><a href=\"https:\/\/github.com\/IAlexanderI1994\/gql-request-article\" rel=\"noopener noreferrer nofollow\">Cc\u044b\u043b\u043a\u0430 \u043d\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \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\/articles\/574592\/\"> https:\/\/habr.com\/ru\/articles\/574592\/<\/a><br \/><\/br><\/br><\/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-415737","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/415737","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=415737"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/415737\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=415737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=415737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=415737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}