kamilkisiela / apollo-angular

A fully-featured, production ready caching GraphQL client for Angular and every GraphQL server 🎁
https://apollo-angular.com
MIT License
1.5k stars 309 forks source link

Got 404 when server redirect because tokens expire #1741

Closed khripunovpp closed 2 years ago

khripunovpp commented 2 years ago

Describe the bug

Hi. There was a problem with a redirect by the server when the tokens expired. When the server tries to redirect and the client accepts a 404, the development server starts correctly.

image image

To Reproduce

Steps to reproduce the behavior: ng serve --proxy-config ./src/proxy.conf.json Then clear all cookies and try to send graphql query.

this.apollo
    .watchQuery<Resp>({
      query: gql`
        query Campaigns {
          campaigns {
            ID,
            UserID,
            RedirectURL
          }
        }
      `,
    })

Expected behavior

The page is redirected to / login.

Environment:

Packages:

├── @angular/cli@12.2.13
├── @angular/core@12.2.0
├── @apollo/client@3.0.0
├── apollo-angular@2.0.4
├── graphql@15.0.0
└── typescript@4.3.5

Additional context

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "strictNullChecks": true,
    "alwaysStrict": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2017",
    "module": "es2020",
    "lib": [
      "es2018",
      "dom",
      "esnext.asynciterable"
    ],
    "allowSyntheticDefaultImports": true,
    "paths": {
      "@tracker/*": [
        "src/app/tracker/*"
      ],
      "@campaings/*": [
        "src/app/tracker/src/modules/campaings/src/*"
      ],
      "@campaings-overview/*": [
        "src/app/tracker/src/modules/campaings/src/campaings-overview/src/campaings-overview/*"
      ],
      "@campaing-edit/*": [
        "src/app/tracker/src/modules/campaings/src/campaing-edit/src/campaing-edit/*"
      ],
      "@notifications/*": [
        "src/app/tracker/src/modules/notifications/*"
      ],
      "@tracker": [
        "src/app/tracker/tracker.module.ts"
      ],
      "@notifications": [
        "src/app/tracker/src/modules/notifications/notifications.module.ts"
      ],
      "@api/*": [
        "src/app/tracker/src/core/api/*"
      ],
      "@auth/*": [
        "src/app/tracker/src/core/auth/*"
      ],
      "@gql/*": [
        "src/app/tracker/src/core/api/src/modules/graphql-api/*"
      ]
    }
  },
  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "ng-tracker": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        },
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "build",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss",
              "node_modules/primeng/resources/themes/saga-blue/theme.css",
              "node_modules/primeng/resources/primeng.min.css",
              "node_modules/primeicons/primeicons.css",
              "node_modules/primeflex/primeflex.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "optimization": false,
              "vendorChunk": true,
              "sourceMap": true,
              "namedChunks": true,
              "extractLicenses": false,
              "buildOptimizer": false
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "ng-tracker:build:development"
          },
          "configurations": {
            "production": {
              "browserTarget": "ng-tracker:build:production"
            },
            "development": {
              "browserTarget": "ng-tracker:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "ng-tracker:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          }
        }
      }
    }
  },
  "defaultProject": "ng-tracker"
}

proxy.conf.json

{
  "/be": {
    "target": "http://REMOTE_HOST/",
    "changeOrigin": true,
    "secure": false,
    "historyApiFallback": true
  }
}

create client

@NgModule()
export class GraphqlApiModule {
  static forRoot(): ModuleWithProviders<GraphqlApiModule> {
    const link = onError(({graphQLErrors, networkError, forward, operation}) => {
      if (graphQLErrors) {
        graphQLErrors.map(({message, extensions, locations, path}) => {
          // Here you may display a message to indicate graphql error
          // You may use 'sweetalert', 'ngx-toastr' or any of your preference
          console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
          console.log(extensions!.code as any)
        })
      }
      if (networkError) {
        console.log(`[Network error]: ${networkError.message}`);

      }
    });
    return {
      ngModule: GraphqlApiModule,
      providers: [
        {
          provide: APOLLO_OPTIONS,
          useFactory: (httpLink: HttpLink) => {
            return {
              cache: new InMemoryCache(),
              link: ApolloLink.from([link, httpLink.create({
                uri: environment.graphql_endpoint,
                withCredentials: true,
              })]),
              connectToDevTools: true,
              defaultOptions: {
                watchQuery: {
                  errorPolicy: 'all'
                }
              },
              fetchOptions: {
                mode: 'no-cors',
              },
            };
          },
          deps: [HttpLink],
        },
      ]
    }
  }
}

image

image

image

kamilkisiela commented 2 years ago

I don't see how it's related to apollo-angular, please provide a small reproduction that I could play with.