Skip to content

Commit

Permalink
ENHANCEMENT: Reverse argument signature of methods using path (#698)
Browse files Browse the repository at this point in the history
* Reverse argument signature of pathed query properties

* Rebase and build
  • Loading branch information
Aaron Carlino authored Oct 18, 2018
1 parent 426bb4d commit 761a6f7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions client/src/lib/dependency-injection/ApolloGraphqlManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,25 +169,25 @@ class ApolloGraphqlManager {
/**
* Adds an arg to the query
*
* @param {string} path The path to the field where the args are applied
* @param {string} name
* @param {string} variableName
* @param {string} path The path to the field where the args are applied
* @returns {ApolloGraphqlManager}
*/
addArg(path = ROOT_FIELD, name, variableName) {
return this.addArgs(path, {
addArg(name, variableName, path = ROOT_FIELD) {
return this.addArgs({
[name]: variableName
});
}, path);
}

/**
* Adds multiple args to the query
*
* @param {string} path The path to the field where the args are applied
* @param {object} args
* @param {string} path The path to the field where the args are applied
* @returns {ApolloGraphqlManager}
*/
addArgs(path = ROOT_FIELD, args = {}) {
addArgs(args = {}, path = ROOT_FIELD) {
const existing = this.config.args[path] || {};
this.config.args[path] = {
...existing,
Expand All @@ -200,21 +200,22 @@ class ApolloGraphqlManager {
/**
* Adds a field to the query
*
* @param {string} path
* @param {string} field
* @param {string} path
* @returns {ApolloGraphqlManager}}
*/
addField(path = ROOT_FIELD, field) {
return this.addFields(path, [field]);
addField(field, path = ROOT_FIELD) {
return this.addFields([field], path);
}

/**
* Adds a list of fields to the query
*
* @param {string[]} fields
* @param {string} path
* @returns {ApolloGraphqlManager}
*/
addFields(path = ROOT_FIELD, fields = []) {
addFields(fields = [], path = ROOT_FIELD) {
let fieldArray = [];
path.split('/').forEach(part => {
if (part === ROOT_FIELD) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ describe('ApolloGraphqlManager', () => {

it('adds fields', () => {
const manager = createMock();
manager.addField('root', 'test');
manager.addFields('root', ['rest', 'fest']);
manager.addField('test');
manager.addFields(['rest', 'fest']);

const fields = manager.getConfig().fields;
expect(Array.isArray(fields)).toBe(true);
Expand All @@ -79,9 +79,9 @@ describe('ApolloGraphqlManager', () => {

it('adds nested fields', () => {
const manager = createMock();
manager.addFields('root', ['FirstName', 'Surname']);
manager.addFields('root', ['Avatar', []]);
manager.addField('root/Avatar', 'URL');
manager.addFields(['FirstName', 'Surname']);
manager.addFields(['Avatar', []]);
manager.addField('URL', 'root/Avatar');

const fields = manager.getConfig().fields;
expect(Array.isArray(fields)).toBe(true);
Expand All @@ -91,19 +91,19 @@ describe('ApolloGraphqlManager', () => {
const i = fields.indexOf('Avatar') + 1;
expect(Array.isArray(fields[i])).toBe(true);
expect(fields[i]).toContain('URL');
expect(() => manager.addFields('root/Surname', 'Foo')).toThrow();
expect(() => manager.addFields('Foo', 'root/Surname')).toThrow();
});

it('adds args to fields', () => {
const manager = createMock({ pagination: false });
manager.addFields('root', ['FirstName', 'Surname']);
manager.addFields('root', ['Avatar', []]);
manager.addField('root/Avatar', 'URL');
manager.addFields(['FirstName', 'Surname']);
manager.addFields(['Avatar', []]);
manager.addField('URL', 'root/Avatar');

manager.addArg('root/FirstName', 'Test', 'SomeVar');
manager.addArgs('root/Avatar/URL', {
manager.addArg('Test', 'SomeVar', 'root/FirstName');
manager.addArgs({
Relative: 'SomeBool',
});
}, 'root/Avatar/URL');
// eslint-disable-next-line no-unused-expressions
manager.setTemplate`query test { ${getFields} }`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('Dynamic graphql injection', () => {
'test-transform',
(updater) => {
updater.test('MyTestReadQuery', (manager) => {
manager.addField('root', 'Plates');
manager.addField('Plates');
manager.transformApolloConfig('props', () => (previous) => ({
...previous,
qux: 'baz'
Expand Down Expand Up @@ -119,7 +119,7 @@ describe('Dynamic graphql injection', () => {
'test-transform',
(updater) => {
updater.test('MyTestReadOneQuery', (manager) => {
manager.addField('root', 'Plates');
manager.addField('Plates');
});
}
);
Expand Down Expand Up @@ -151,7 +151,7 @@ describe('Dynamic graphql injection', () => {
'test-transform',
(updater) => {
updater.test('MyTestUpdateQuery', (manager) => {
manager.addField('root', 'Plates');
manager.addField('Plates');
});
}
);
Expand Down Expand Up @@ -182,7 +182,7 @@ describe('Dynamic graphql injection', () => {
'test-transform',
(updater) => {
updater.test('MyTestDeleteQuery', (manager) => {
manager.addField('root', 'ID');
manager.addField('ID');
});
}
);
Expand Down Expand Up @@ -214,7 +214,7 @@ describe('Dynamic graphql injection', () => {
'test-transform',
(updater) => {
updater.test('MyTestCreateQuery', (manager) => {
manager.addField('root', 'Claws');
manager.addField('Claws');
});
}
);
Expand Down

0 comments on commit 761a6f7

Please sign in to comment.