【发布时间】:2021-06-28 23:48:29
【问题描述】:
我正在使用 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 { }
令牌有效。客户端是http://localhost:4200/
【问题讨论】: