|
1 |
| -import { GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLInterfaceType, GraphQLUnionType } from 'graphql'; |
| 1 | +import { GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLInterfaceType, GraphQLUnionType, GraphQLFloat } from 'graphql'; |
2 | 2 |
|
3 | 3 | import getFields from '../../src/generator/getFields';
|
4 | 4 | import getFragment from '../../src/generator/getFragment';
|
@@ -185,4 +185,73 @@ describe('getField', () => {
|
185 | 185 | expect(getFragment.mock.calls[1][3]).toEqual(commonField);
|
186 | 186 | });
|
187 | 187 | });
|
| 188 | + |
| 189 | + describe('aggregateItems should generate two additional levels', () => { |
| 190 | + beforeEach(() => { |
| 191 | + jest.resetAllMocks(); |
| 192 | + }); |
| 193 | + const aggregateScalarResult = new GraphQLObjectType({ |
| 194 | + name: 'SearchableAggregateScalarResult', |
| 195 | + fields: { |
| 196 | + value: { type: GraphQLFloat }, |
| 197 | + }, |
| 198 | + }); |
| 199 | + |
| 200 | + const aggregateBucketResultItem = new GraphQLObjectType({ |
| 201 | + name: 'SearchableAggregateBucketResultItem', |
| 202 | + fields: { |
| 203 | + key: { type: GraphQLString }, |
| 204 | + doc_count: { type: GraphQLInt }, |
| 205 | + }, |
| 206 | + }); |
| 207 | + |
| 208 | + const aggregateBucketResult = new GraphQLObjectType({ |
| 209 | + name: 'SearchableAggregateBucketResult', |
| 210 | + fields: { |
| 211 | + buckets: { type: aggregateBucketResultItem }, |
| 212 | + }, |
| 213 | + }); |
| 214 | + |
| 215 | + const aggregateResult = new GraphQLUnionType({ |
| 216 | + name: 'SearchableAggregateGenericResult', |
| 217 | + types: [aggregateScalarResult, aggregateBucketResult], |
| 218 | + }); |
| 219 | + |
| 220 | + const aggregateItemsObject = new GraphQLObjectType({ |
| 221 | + name: 'SearchableAggregateResult', |
| 222 | + fields: { |
| 223 | + name: { type: GraphQLString }, |
| 224 | + result: { type: aggregateResult }, |
| 225 | + }, |
| 226 | + }); |
| 227 | + |
| 228 | + const schema = new GraphQLSchema({ |
| 229 | + query: new GraphQLObjectType({ |
| 230 | + name: 'Query', |
| 231 | + fields: { |
| 232 | + aggregateItems: { type: aggregateItemsObject }, |
| 233 | + }, |
| 234 | + }), |
| 235 | + }); |
| 236 | + |
| 237 | + it('aggregateItems property should traverse two additional levels to generate required fields with default depth 2', () => { |
| 238 | + const maxDepth = 2; |
| 239 | + const getPossibleTypeSpy = jest.spyOn(schema, 'getPossibleTypes'); |
| 240 | + getFields(schema.getQueryType().getFields().aggregateItems, schema, maxDepth, { useExternalFragmentForS3Object: false }); |
| 241 | + expect(getPossibleTypeSpy).toHaveBeenCalled(); |
| 242 | + expect(getFragment).toHaveBeenCalled(); |
| 243 | + |
| 244 | + const commonField = []; // unions don't have to have common field |
| 245 | + |
| 246 | + expect(getFragment.mock.calls[0][0]).toEqual(aggregateScalarResult); |
| 247 | + expect(getFragment.mock.calls[0][1]).toEqual(schema); |
| 248 | + expect(getFragment.mock.calls[0][2]).toEqual(maxDepth - 1); |
| 249 | + expect(getFragment.mock.calls[0][3]).toEqual(commonField); |
| 250 | + |
| 251 | + expect(getFragment.mock.calls[1][0]).toEqual(aggregateBucketResult); |
| 252 | + expect(getFragment.mock.calls[1][1]).toEqual(schema); |
| 253 | + expect(getFragment.mock.calls[1][2]).toEqual(maxDepth - 1); |
| 254 | + expect(getFragment.mock.calls[1][3]).toEqual(commonField); |
| 255 | + }); |
| 256 | + }); |
188 | 257 | });
|
0 commit comments