我正在尝试构建一个模块以将 GraphQL 与 ionic 4 一起使用。我已经能够将不安全的 GraphQL 站点导入此 ionic 应用程序。我在将 header 导入导出函数 createApollo 时遇到问题。这是我离开的地方。
GraphQL.module.ts
import {NgModule} from '@angular/core';
import {ApolloModule, APOLLO_OPTIONS} from 'apollo-angular';
import {HttpLinkModule, HttpLink} from 'apollo-angular-link-http';
import {InMemoryCache} from 'apollo-cache-inmemory';
import { HttpHeaders } from "@angular/common/http";
// const uri = 'https://api.graph.cool/simple/v1/cjv14rb014vcb0108wgqy0zmq';
const uri = 'https://somewhere.com/graphql/mobile';
const headers = new HttpHeaders({
'x-authorization-token': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'access-control-allow-origin': 'https://app.procuredox.com,https://b2b.procuredox.com',
'access-control-allow-methods': 'GET,POST,OPTIONS',
'access-control-allow-headers': 'Content-Type, X-Authorization-Token, Origin',
'content-type': 'application/json'
});
export function createApollo(httpLink: HttpLink) {
return {
link: httpLink.create({uri}),
cache: new InMemoryCache(),
};
}
@NgModule({
exports: [ApolloModule, HttpLinkModule],
providers: [
{
provide: APOLLO_OPTIONS,
useFactory: createApollo,
deps: [HttpLink],
},
],
})
export class GraphQLModule {}
最佳答案
在经历了一系列论坛和 Apollo Github 项目之后,我发现这是可行的。
import {NgModule} from '@angular/core';
import {ApolloModule, APOLLO_OPTIONS} from 'apollo-angular';
import {HttpLinkModule, HttpLink} from 'apollo-angular-link-http';
import {InMemoryCache} from 'apollo-cache-inmemory';
import { HttpLink as myHttpLink } from 'apollo-link-http';
const LINK:myHttpLink = new myHttpLink({
uri: 'https://XXXXXXXXXXXXXXXXXXXXX/graphql/mobile/',
// Additional fetch options like `credentials` or `headers`,
headers: {
"Access-Control-Allow-Origin": "https://XXXXXXXXXXXXXXXX",
"Access-Control-Allow-Methods": "GET",
'Access-Control-Allow-Headers': 'application/json',
"Access-Control-Allow-Credentials" : true
},
useGETForQueries: true
});
export function createApollo(httpLink: myHttpLink) {
return {
link: LINK,
// fetchOptions: {
// mode: "no-cors",
// },
cache: new InMemoryCache(),
};
}
@NgModule({
exports: [ApolloModule, HttpLinkModule],
providers: [
{
provide: APOLLO_OPTIONS,
useFactory: createApollo,
deps: [HttpLink],
},
],
})
export class GraphQLModule {}
关于graphql - 如何将标题添加到 ionic 4/Apollo GraphQL App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57280567/