Skip to content

Commit

Permalink
refactor: rm nested 'links' dir, raise links into apolloLinks/
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-anderson committed May 18, 2023
1 parent 8cb288a commit 40cf65f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 35 deletions.
63 changes: 33 additions & 30 deletions src/app/apolloLink/apolloLink.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
import { ApolloLink } from "@apollo/client/link/core";
import {
apiHttpLink,
errorLink,
getAuthTokenLink,
// loggerLink,
retryLink,
} from "./links";

// TODO add apollo-link-rest and delete httpService & authService
import { apiHttpLink } from "./link.apiHttp";
import { errorLink } from "./link.error";
import { getAuthTokenLink } from "./link.getAuthToken";
import { retryLink } from "./link.retry";

/**
* This is the Apollo Link that is used to make all requests to the server.
*
* **Link Process Outline:** `(1, 2, 3, 4, 5 => SERVER => 5, 4, 3, 2, 1)`
*
* **Links:**
*
* 1. loggerLink
* - Only imported and used in dev mode
* - Logs all requests and responses
*
* 2. getAuthTokenLink
* - Sets Authorization header (value will be null if !token)
*
* 3. retryLink
* - Catches and retries _NETWORK_ errors
* - _Does not catch/retry GraphQL errors_
*
* 4. errorLink
* - Logs network errors _AND_ GraphQL errors
* - Catches and retries _GraphQL_ errors
* - When AuthenticationError is thrown on the server, that comes back as a
* _GraphQL_ error which can not be caught by the `retryLink`, so this link
* handles sending the retry-request to the `/auth/token` endpoint.
*
* 5. apiHttpLink
* - Sends request to the `/api` endpoint
*/
export const apolloLink = ApolloLink.from([
// loggerLink,
// if needed, loggerLink goes here
getAuthTokenLink,
retryLink,
errorLink,
apiHttpLink,
]);

/*
LINK PROCESS (1, 2, 3, 4, 5 => SERVER => 5, 4, 3, 2, 1)
1) loggerLink
Gets called every time no matter what.
2) getAuthTokenLink
Sets Authorization header; value will be null if !token.
3) retryLink
CAN ONLY CATCH NETWORK ERRORS, NOT GraphQL ERRORS!
4) errorLink
Logs errors AND MOST IMPORTANTLY, GraphQL Errors can only be caught here!
errorLink CAN ALSO RETRY REQUESTS! When AuthenticationError is thrown on
the server, that comes back as a GraphQL error that can ONLY be caught here!
So it is HERE that we make the switch to retry and go to /auth/token.
5) authSplitterLink
If operationName is Login/Register, go to restLink (/auth endpoint)
Else go to apiHttpLink (/api endpoint)
*/
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 0 additions & 5 deletions src/app/apolloLink/links/index.ts

This file was deleted.

0 comments on commit 40cf65f

Please sign in to comment.