Skip to content

Commit df318ce

Browse files
committed
Added "extensions" sub-dir and initialized it with first extension, "apollo". Finished basic first version of apollo extension, which allows a normal JavaScript object to be converted into a valid graphql query for use with Apollo Client.
1 parent 811bb2d commit df318ce

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { expect } from 'chai';
2+
import {objToApolloQuery, addApolloTargetToGqlQueryString} from '../';
3+
4+
describe('addApolloTarget()', () => {
5+
6+
it('Converts a simple JavaScript object into a valid Apollo query', () => {
7+
const input = {
8+
appState2: {
9+
hello2: 'some arbitrary value',
10+
hello: 'arbitrary value'
11+
}
12+
};
13+
const apolloTarget = 'client';
14+
const expected = 'query { appState2 { hello2 hello @client } }';
15+
expect(objToApolloQuery(input, apolloTarget)).to.equal(expected);
16+
});
17+
18+
it('Converts a somewhat complex JavaScript object into a valid Apollo query', () => {
19+
const input = {
20+
__typename: 'everyday-health-focuses',
21+
diet: {
22+
__typename: 'diet',
23+
title: 'Diet',
24+
id: 'diet',
25+
options: {
26+
'__typename': 'diet-options', // Why does my IDE tell me only this line needs quotation?
27+
'calorie-count': {
28+
__typename: 'calorie-count',
29+
text: 'Calorie Count',
30+
id: 'calorie-count',
31+
icon: 'fa fa-question-circle',
32+
category: 'Diet',
33+
selected: false
34+
},
35+
'weight': {
36+
__typename: 'weight',
37+
text: 'Weight',
38+
id: 'weight',
39+
icon: 'fa fa-question-circle',
40+
category: 'Diet',
41+
selected: false
42+
},
43+
'mood': {
44+
__typename: 'mood',
45+
text: 'Mood',
46+
id: 'mood',
47+
icon: 'fa fa-question-circle',
48+
category: 'Diet',
49+
selected: false
50+
},
51+
}
52+
},
53+
};
54+
const apolloTarget = 'client';
55+
// const expected = 'query { __typename diet { __typename title id options { __typename calorie-count ' +
56+
// '{ __typename text id icon category } weight { __typename text id icon category } mood ' +
57+
// '\{ __typename text id icon category @client } } } }';
58+
const expected = 'query { diet { title id options { calorie-count ' +
59+
'{ text id icon category } weight { text id icon category } mood ' +
60+
'\{ text id icon category @client } } } }';
61+
expect(objToApolloQuery(input, apolloTarget)).to.equal(expected);
62+
});
63+
64+
it('Adds apollo target to appropriate location in a graphql query string.', () => {
65+
const input = 'query { appState2 { hello2 hello } }';
66+
const apolloTarget = 'client';
67+
const expected = 'query { appState2 { hello2 hello @client } }';
68+
expect(addApolloTargetToGqlQueryString(input, apolloTarget)).to.equal(expected);
69+
});
70+
71+
});

src/extensions/apollo/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { jsonToGraphQLQuery } from '../../index';
2+
3+
export function addApolloTargetToGqlQueryString(query: string, target: string): string {
4+
for (let i = query.length; i--; i === 0) {
5+
const thisChar = query[i];
6+
if ((thisChar !== ' ') && (thisChar !== '}')) {
7+
return query.slice(0, i + 1) + ' @' + target + query.slice(i + 1 , query.length);
8+
}
9+
}
10+
}
11+
12+
export function objToApolloQuery(object: object, target: string): string {
13+
const gqlQuery = jsonToGraphQLQuery(object);
14+
const querySansTypenames = gqlQuery.replace(/__typename /g, '');
15+
const queryWithTarget = addApolloTargetToGqlQueryString(querySansTypenames, target);
16+
return 'query { ' + queryWithTarget + ' }';
17+
}
18+
19+
// TODO: Add functionality to automatically generate mutations.
20+
21+
// TODO: Add functionality to automatically generate resolvers.

src/extensions/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
export * from './apollo';

src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
export * from './jsonToGraphQLQuery';
2-
export {EnumType} from './types/EnumType';
3-
export {VariableType} from './types/VariableType';
1+
2+
export * from './jsonToGraphQLQuery';
3+
export {EnumType} from './types/EnumType';
4+
export {VariableType} from './types/VariableType';
5+
export * from './extensions/index';

0 commit comments

Comments
 (0)