|
1 | 1 |
|
2 | 2 | import { expect } from 'chai';
|
3 |
| -import { jsonToGraphQLQuery, filterNonConfigFields } from '../'; |
| 3 | +import { jsonToGraphQLQuery } from '../'; |
4 | 4 |
|
5 | 5 | describe('jsonToGraphQL()', () => {
|
6 | 6 |
|
@@ -146,82 +146,64 @@ describe('jsonToGraphQL()', () => {
|
146 | 146 | }`);
|
147 | 147 | });
|
148 | 148 |
|
149 |
| - it('Converts a JavaScript object into a valid query, including a single directive w/ no arguments.', () => { |
150 |
| - interface ILooseObject { [key: string]: any; } |
151 |
| - let input: ILooseObject = { |
152 |
| - __typename: 'everyday-health-focuses', |
153 |
| - diet: { |
154 |
| - __typename: 'diet', |
155 |
| - id: 'diet', |
156 |
| - options: { |
157 |
| - // tslint:disable-next-line:object-literal-key-quotes |
158 |
| - __typename: 'diet-options', |
159 |
| - 'calorie-count': { |
160 |
| - __typename: 'calorie-count', |
161 |
| - category: 'Diet', |
162 |
| - icon: 'fa fa-question-circle', |
163 |
| - id: 'calorie-count', |
164 |
| - selected: false, |
165 |
| - text: 'Calorie Count' |
166 |
| - }, |
167 |
| - // tslint:disable-next-line:object-literal-key-quotes |
168 |
| - mood: { |
169 |
| - __typename: 'mood', |
170 |
| - category: 'Diet', |
171 |
| - icon: 'fa fa-question-circle', |
172 |
| - id: 'mood', |
173 |
| - selected: false, |
174 |
| - text: 'Mood' |
| 149 | + it('Converts a complex query with directives with no arguments', () => { |
| 150 | + const query = { |
| 151 | + query: { |
| 152 | + diet: { |
| 153 | + __directives: { |
| 154 | + client: true |
175 | 155 | },
|
176 |
| - // tslint:disable-next-line:object-literal-key-quotes |
177 |
| - weight: { |
178 |
| - __typename: 'weight', |
179 |
| - category: 'Diet', |
180 |
| - icon: 'fa fa-question-circle', |
181 |
| - id: 'weight', |
182 |
| - selected: false, |
183 |
| - text: 'Weight' |
| 156 | + id: 'diet', |
| 157 | + options: { |
| 158 | + mood: { |
| 159 | + category: 'Diet', |
| 160 | + id: 'mood', |
| 161 | + selected: true, |
| 162 | + }, |
| 163 | + weight: { |
| 164 | + category: 'Diet', |
| 165 | + icon: 'fa fa-question-circle', |
| 166 | + id: 'weight', |
| 167 | + selected: false, |
| 168 | + text: 'Weight' |
| 169 | + }, |
184 | 170 | },
|
| 171 | + title: 'Diet' |
185 | 172 | },
|
186 |
| - title: 'Diet' |
187 |
| - }, |
188 |
| - someOtherAbritraryKey: { |
189 |
| - __typename: 'someArbitraryObjType', |
190 |
| - arb1: 'arbitrary value', |
191 |
| - arb2: 'some other arbitrary value' |
| 173 | + someOtherAbritraryKey: { |
| 174 | + __directives: { |
| 175 | + client: true |
| 176 | + }, |
| 177 | + arb1: 'arbitrary value', |
| 178 | + arb2: 'some other arbitrary value' |
| 179 | + } |
192 | 180 | }
|
193 | 181 | };
|
194 |
| - Object.keys(input) |
195 |
| - .filter(filterNonConfigFields) |
196 |
| - .forEach((key) => { |
197 |
| - input[key]['__directives'] = { client: true, }; |
198 |
| - }); |
199 |
| - input = {query: input}; |
200 |
| - const expected = 'query { diet @client { id options { calorie-count { category ' + |
201 |
| - 'icon id text } mood { category icon id text } weight { category icon id text } } ' + |
| 182 | + const expected = 'query { diet @client { id options { ' + |
| 183 | + 'mood { category id selected } weight { category icon id text } } ' + |
202 | 184 | 'title } someOtherAbritraryKey @client { arb1 arb2 } }';
|
203 |
| - expect(jsonToGraphQLQuery(input)).to.equal(expected); |
| 185 | + expect(jsonToGraphQLQuery(query)).to.equal(expected); |
204 | 186 | });
|
205 | 187 |
|
206 |
| - it('Converts a JavaScript object into a valid query, including single directives ' + |
207 |
| - 'with args, so long as any variables used are enclosed in a string with "$" included.', () => { |
208 |
| - interface ILooseObject { [key: string]: any; } |
209 |
| - let input: ILooseObject = { |
210 |
| - someOtherAbritraryKey: { |
211 |
| - __typename: 'someArbitraryObjType', |
212 |
| - arb1: 'arbitrary value', |
213 |
| - arb2: 'some other arbitrary value' |
214 |
| - } |
215 |
| - }; |
216 |
| - Object.keys(input) |
217 |
| - .filter(filterNonConfigFields) |
218 |
| - .forEach((key) => { |
219 |
| - input[key]['__directives'] = { include: {if: '$isAwesome'}, }; |
220 |
| - }); |
221 |
| - input = {query: input}; |
222 |
| - const expected = 'query { someOtherAbritraryKey @include(if: $isAwesome) { arb1 arb2 } }'; |
223 |
| - expect(jsonToGraphQLQuery(input)).to.equal(expected); |
224 |
| - }); |
| 188 | + // it('Converts a JavaScript object into a valid query, including single directives ' + |
| 189 | + // 'with args, so long as any variables used are enclosed in a string with "$" included.', () => { |
| 190 | + // interface ILooseObject { [key: string]: any; } |
| 191 | + // let input: ILooseObject = { |
| 192 | + // someOtherAbritraryKey: { |
| 193 | + // __typename: 'someArbitraryObjType', |
| 194 | + // arb1: 'arbitrary value', |
| 195 | + // arb2: 'some other arbitrary value' |
| 196 | + // } |
| 197 | + // }; |
| 198 | + // Object.keys(input) |
| 199 | + // .filter(filterNonConfigFields) |
| 200 | + // .forEach((key) => { |
| 201 | + // input[key]['__directives'] = { include: {if: '$isAwesome'}, }; |
| 202 | + // }); |
| 203 | + // input = {query: input}; |
| 204 | + // const expected = 'query { someOtherAbritraryKey @include(if: $isAwesome) { arb1 arb2 } }'; |
| 205 | + // expect(jsonToGraphQLQuery(input)).to.equal(expected); |
| 206 | + // }); |
225 | 207 |
|
226 | 208 | // TODO
|
227 | 209 | // it('Converts a JavaScript object into a valid query, including *multiple* directives ' +
|
@@ -395,4 +377,35 @@ describe('jsonToGraphQL()', () => {
|
395 | 377 | }`);
|
396 | 378 | });
|
397 | 379 |
|
| 380 | + it('we can ignore apollo __typename keys', () => { |
| 381 | + const query = { |
| 382 | + query: { |
| 383 | + Posts: { |
| 384 | + __typename: 'Posts', |
| 385 | + id: true, |
| 386 | + title: true, |
| 387 | + post_date: true, |
| 388 | + subObject: { |
| 389 | + __typename: 'subObject', |
| 390 | + test: 'a value' |
| 391 | + }, |
| 392 | + } |
| 393 | + } |
| 394 | + }; |
| 395 | + expect(jsonToGraphQLQuery(query, { |
| 396 | + pretty: true, |
| 397 | + ignoreFields: ['__typename'] |
| 398 | + })).to.equal( |
| 399 | + `query { |
| 400 | + Posts { |
| 401 | + id |
| 402 | + title |
| 403 | + post_date |
| 404 | + subObject { |
| 405 | + test |
| 406 | + } |
| 407 | + } |
| 408 | +}`); |
| 409 | + }); |
| 410 | + |
398 | 411 | });
|
0 commit comments