Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(apollo): make apollo client a dependency #685

Merged
merged 1 commit into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 19 additions & 37 deletions addon/apollo.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,25 @@
import { assert } from "@ember/debug";
import {
dependencySatisfies,
macroCondition,
importSync,
} from "@embroider/macros";
import { setContext } from "@apollo/client/link/context";
import { onError } from "@apollo/client/link/error";
import { handleUnauthorized } from "ember-simple-auth-oidc";

let apolloMiddleware;
export default function apolloMiddleware(httpLink, session) {
const authMiddleware = setContext(async (_, context) => {
await session.refreshAuthentication.perform();

if (macroCondition(dependencySatisfies("@apollo/client", ">=3.0.0"))) {
const { setContext } = importSync("@apollo/client/link/context");
const { onError } = importSync("@apollo/client/link/error");
return {
...context,
headers: {
...context.headers,
...session.headers,
},
};
});

apolloMiddleware = (httpLink, session) => {
const authMiddleware = setContext(async (_, context) => {
await session.refreshAuthentication.perform();
const authAfterware = onError((error) => {
if (error.networkError && error.networkError.statusCode === 401) {
handleUnauthorized(session);
}
});

return {
...context,
headers: {
...context.headers,
...session.headers,
},
};
});

const authAfterware = onError((error) => {
if (error.networkError && error.networkError.statusCode === 401) {
handleUnauthorized(session);
}
});

return authMiddleware.concat(authAfterware).concat(httpLink);
};
} else {
apolloMiddleware = () =>
assert(
"@apollo/client >= 3.0.0 must be installed in order to use the apollo middleware"
);
return authMiddleware.concat(authAfterware).concat(httpLink);
}

export default apolloMiddleware;
10 changes: 1 addition & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"test:ember-compatibility": "ember try:each"
},
"dependencies": {
"@apollo/client": "^3.7.0",
"@embroider/macros": "^1.9.0",
"base64-js": "^1.5.1",
"ember-auto-import": "^2.4.3",
Expand All @@ -40,7 +41,6 @@
"devDependencies": {
"@adfinis-sygroup/eslint-config": "1.5.0",
"@adfinis-sygroup/semantic-release-config": "3.4.0",
"@apollo/client": "3.7.0",
"@ember/optional-features": "2.0.0",
"@ember/test-helpers": "2.8.1",
"@embroider/test-setup": "1.8.3",
Expand Down Expand Up @@ -80,14 +80,6 @@
"qunit-dom": "2.0.0",
"webpack": "5.74.0"
},
"peerDependencies": {
"@apollo/client": ">= 3.0.0"
},
"peerDependenciesMeta": {
"@apollo/client": {
"optional": true
}
},
"engines": {
"node": "14.* || >= 16"
},
Expand Down