我收到错误消息:http:// localhost:10600 / playground /的Http错误响应:在新的ApolloError上找不到404

问题描述

我正在使用graphQL。我正在尝试提出要求。服务器具有授权。我以官方网站为例,将uri更改为http:// localhost:10600 / playground /。但是错误404不断发生。尽管该服务本身可以在此地址使用。

我的graphql.module.ts

import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { Apollo,APOLLO_OPTIONS } from 'apollo-angular';
import { HttpLink } from 'apollo-angular/http';
import { InMemoryCache,ApolloLink } from '@apollo/client/core';
import { setContext } from '@apollo/client/link/context';

const uri = 'http://localhost:10600/playground/';

export function createApollo(httpLink: HttpLink) {
  const basic = setContext((operation,context) => ({
    headers: {
      Accept: 'charset=utf-8'
    }
  }));

  const auth = setContext((operation,context) => {
    const token = localStorage.getItem('access_token');
    console.log(token)
    if (token === null) {
      return {};
    } else {
      return {
        headers: {
          Authorization: `Bearer ${token}`
        }
      };
    }
  });

  const link = ApolloLink.from([basic,auth,httpLink.create({ uri })]);
  const cache = new InMemoryCache();
  console.log(link);
  return {
    link,cache
  };
}

@NgModule({
  exports: [
    HttpClientModule,],providers: [{
    provide: APOLLO_OPTIONS,useFactory: createApollo,deps: [HttpLink]
  }]
})
export class GraphQLModule { }

enter image description here

令牌有效。客户端是http:// localhost:4200 /

解决方法

对于 2 个不同的错误,我收到了相同的错误消息。

  1. 我的网址错误,我提供了 https://server-name.domain 但实际上它是 https://server-name.domain/graphql

在您的情况下,请尝试删除 /playground 2. 我的查询有错字

在 chrome 中检查您的网络选项卡并阅读服务器响应,比 console.log Http failure response

更有帮助