Skip to content

Commit 8eac915

Browse files
MorganDbsMorgan DUBOIS
andauthored
Adding support of multi directives (#33)
* adding support of multi directives Co-authored-by: Morgan DUBOIS <[email protected]>
1 parent bda6e82 commit 8eac915

File tree

2 files changed

+79
-8
lines changed

2 files changed

+79
-8
lines changed

src/__tests__/directives.tests.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,82 @@ describe('jsonToGraphQLQuery() - directives', () => {
7272
expect(jsonToGraphQLQuery(query)).to.equal(expected);
7373
});
7474

75+
it('converts a simple query with args and multiple directives', () => {
76+
const query = {
77+
query: {
78+
Posts: {
79+
__args: {
80+
where: {
81+
id: 10,
82+
},
83+
orderBy: 'flibble',
84+
},
85+
__directives: {
86+
client: true,
87+
withArgs: {
88+
id: [1, 2, 3],
89+
},
90+
},
91+
id: true,
92+
title: true,
93+
post_date: true,
94+
},
95+
},
96+
} as any;
97+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
98+
`query {
99+
Posts @client @withArgs(id: [1, 2, 3]) (where: {id: 10}, orderBy: "flibble") {
100+
id
101+
title
102+
post_date
103+
}
104+
}`
105+
);
106+
});
107+
108+
it('converts a complex query with multiple directives', () => {
109+
const query = {
110+
query: {
111+
diet: {
112+
__directives: {
113+
client: true,
114+
},
115+
id: 'diet',
116+
options: {
117+
mood: {
118+
category: 'Diet',
119+
id: 'mood',
120+
selected: true,
121+
},
122+
weight: {
123+
category: 'Diet',
124+
icon: 'fa fa-question-circle',
125+
id: 'weight',
126+
selected: false,
127+
text: 'Weight',
128+
},
129+
},
130+
title: 'Diet',
131+
},
132+
someOtherAbritraryKey: {
133+
__directives: {
134+
client: true,
135+
withArgs: {
136+
id: [1, 2, 3],
137+
},
138+
},
139+
arb1: 'arbitrary value',
140+
arb2: 'some other arbitrary value',
141+
},
142+
},
143+
};
144+
const expected =
145+
'query { diet @client { id options { ' +
146+
'mood { category id selected } weight { category icon id text } } ' +
147+
'title } someOtherAbritraryKey @client @withArgs(id: [1, 2, 3]) { arb1 arb2 } }';
148+
expect(jsonToGraphQLQuery(query)).to.equal(expected);
149+
});
150+
75151
// TODO: Need this test still? How to handle variables unless $ declared explicitly?
76152
// it('converts a JavaScript object into a valid query, including single directives ' +
77153
// 'with args, so long as any variables used are enclosed in a string with "$" included', () => {

src/jsonToGraphQLQuery.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,9 @@ function convertQuery(node: any, level: number, output: [string, number][], opti
115115
let argsStr = '';
116116
let dirsStr = '';
117117
if (directivesExist) {
118-
// TODO: Add support for multiple directives on one node.
119-
const numDirectives = Object.keys(value.__directives).length;
120-
if (numDirectives > 1) {
121-
throw new Error(`Too many directives. The object/key ` +
122-
`'${Object.keys(value)[0]}' had ${numDirectives} directives, ` +
123-
`but only 1 directive per object/key is supported at this time.`);
124-
}
125-
dirsStr = `@${buildDirectives(value.__directives)}`;
118+
dirsStr = Object.entries(value.__directives)
119+
.map(item => `@${buildDirectives({[item[0]]: item[1]})}`)
120+
.join(' ')
126121
}
127122
if (argsExist) {
128123
argsStr = `(${buildArgs(value.__args)})`;

0 commit comments

Comments
 (0)