Ember-Octane 3.28 使用 graphql 端点配置了 enviroment.js 文件,我的服务文件夹中的 apollo.js 文件看起来像
import ApolloService from 'ember-apollo-client/services/apollo';
import { inject as service } from '@ember/service';
import { setContext } from '@apollo/client/link/context';
export default class OverridenApollo extends ApolloService {
@service session;
link() {
let httpLink = super.link();
let authLink = setContext((request, context) => {
return this._runAuthorize(request, context);
});
return authLink.concat(httpLink);
}
_runAuthorize() {
if (!this.session.isAuthenticated) {
return {};
}
return new Promise((success) => {
let headers = {};
let token = this.session.data.authenticated.token;
headers['Authorization'] = `Bearer ${token}`;
success({ headers });
});
}
clientOptions() {
return {
link: this.link(),
cache: this.cache,
};
}
//...
}
使用 ember-apollo-client pkg