Skip to content

Commit e94de82

Browse files
Merge pull request #423 from bertdeblock/add-the-ability-to-globally-override-the-query-manager-service
Add support for a `defaultQueryManagerService` environment option
2 parents 7d42279 + ecb7bf3 commit e94de82

File tree

5 files changed

+45
-11
lines changed

5 files changed

+45
-11
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ let ENV = {
4141
...
4242
apollo: {
4343
apiURL: 'https://test.example/graphql',
44+
4445
// Optionally, set the credentials property of the Fetch Request interface
4546
// to control when a cookie is sent:
4647
// requestCredentials: 'same-origin', // other choices: 'include', 'omit'
48+
49+
// Optionally, define which service a query manager should use by default:
50+
// defaultQueryManagerService: 'my-custom-apollo-service',
4751
},
4852
...
4953
}

addon/-private/apollo/query-manager.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,20 @@ function isElementDescriptor(args) {
2525
}
2626

2727
export function queryManager(...theArgs) {
28-
let serviceName = 'apollo';
29-
let [options] = theArgs;
30-
if (typeof options === 'object' && options.service) {
31-
serviceName = options.service;
32-
}
33-
3428
let setupQueryManager = computed(function () {
35-
const service = getOwner(this).lookup(`service:${serviceName}`);
29+
const [options] = theArgs;
30+
const owner = getOwner(this);
31+
const config = owner.resolveRegistration('config:environment');
32+
33+
const serviceOption = options?.service;
34+
const serviceDefaultOption = config.apollo?.defaultQueryManagerService;
35+
const serviceName = serviceOption || serviceDefaultOption || 'apollo';
36+
37+
const service = owner.lookup(`service:${serviceName}`);
3638
const queryManager = new QueryManager(service);
39+
3740
setupHooks(queryManager, this);
41+
3842
return queryManager;
3943
});
4044

tests/unit/-private/query-manager-hooks-component-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ module(
9595
if (gte('3.10.0')) {
9696
test('it works using decorator syntax', function (assert) {
9797
assert.expect(5);
98-
TestObject = class MyTestClassOjbect extends EmberComponent {
98+
TestObject = class MyTestClassObject extends EmberComponent {
9999
@queryManager({ service: 'overridden-apollo' }) apollo;
100100

101101
willDestroyElement() {

tests/unit/-private/query-manager-hooks-object-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ module('Unit | queryManager | Setup Hooks in EmberOject', function (hooks) {
9393
if (gte('3.10.0')) {
9494
test('it works using decorator syntax', function (assert) {
9595
assert.expect(6);
96-
TestObject = class MyTestClassOjbect extends EmberObject {
96+
TestObject = class MyTestClassObject extends EmberObject {
9797
@queryManager({ service: 'overridden-apollo' }) apollo;
9898

9999
willDestroy() {

tests/unit/-private/query-manager-macro-decorator-test.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module('Unit | queryManager | macro - decorator', function (hooks) {
4040

4141
if (gte('3.10.0')) {
4242
test('it works using decorator syntax without options', function (assert) {
43-
TestObject = class MyTestClassOjbect extends EmberObject {
43+
TestObject = class MyTestClassObject extends EmberObject {
4444
@queryManager apollo;
4545
};
4646

@@ -57,7 +57,7 @@ module('Unit | queryManager | macro - decorator', function (hooks) {
5757
});
5858

5959
test('it works using decorator syntax with options', function (assert) {
60-
TestObject = class MyTestClassOjbect extends EmberObject {
60+
TestObject = class MyTestClassObject extends EmberObject {
6161
@queryManager({ service: 'overridden-apollo' }) apollo;
6262
};
6363

@@ -72,5 +72,31 @@ module('Unit | queryManager | macro - decorator', function (hooks) {
7272
'the apollo service should be an instance of the overridden apollo service'
7373
);
7474
});
75+
76+
test('it works using the `defaultQueryManagerService` environment option', function (assert) {
77+
const config = this.owner.resolveRegistration('config:environment');
78+
79+
config.apollo.defaultQueryManagerService = 'overridden-apollo';
80+
81+
TestObject = class MyTestClassObject extends EmberObject {
82+
@queryManager apollo;
83+
};
84+
85+
let subject = this.subject();
86+
87+
assert.ok(subject.apollo, 'should create an apollo property');
88+
89+
assert.true(
90+
subject.apollo instanceof QueryManager,
91+
'it should be an instance of the query manager'
92+
);
93+
94+
assert.true(
95+
subject.apollo.apollo instanceof OverriddenApollo,
96+
'the apollo service should be an instance of the overridden apollo service'
97+
);
98+
99+
delete config.apollo.defaultQueryManagerService;
100+
});
75101
}
76102
});

0 commit comments

Comments
 (0)