diff --git a/addon/apollo.js b/addon/apollo.js index c6631bf5..4a1a9f1b 100644 --- a/addon/apollo.js +++ b/addon/apollo.js @@ -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; diff --git a/package.json b/package.json index ff54dc0c..df7514db 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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" },