diff --git a/src/buildGqlQuery.test.ts b/src/buildGqlQuery.test.ts index 91433ba..dae3be4 100644 --- a/src/buildGqlQuery.test.ts +++ b/src/buildGqlQuery.test.ts @@ -324,6 +324,50 @@ describe(buildGqlQuery.name, () => { `), ); }); + + it('returns the correct query with sparse fieldset', () => { + expect( + print( + buildGqlQuery(introspectionResult)( + resource, + GET_LIST, + resource[GET_LIST], + { + filter: {}, + paging: {}, + sorting: {}, + meta: { + sparseFields: ['id', 'name', 'location'], + }, + }, + ), + ), + ).toEqual( + print(gql` + query clubs( + $paging: OffsetPaging! + $filter: ClubFilter! + $sorting: [ClubSort!]! + ) { + data: clubs(paging: $paging, filter: $filter, sorting: $sorting) { + nodes { + id + name + location { + type + coordinates + } + } + pageInfo { + hasNextPage + hasPreviousPage + } + totalCount + } + } + `), + ); + }); }); describe(GET_MANY, () => { @@ -376,6 +420,50 @@ describe(buildGqlQuery.name, () => { `), ); }); + + it('returns the correct query with sparse fieldset', () => { + expect( + print( + buildGqlQuery(introspectionResult)( + resource, + GET_MANY, + resource[GET_MANY], + { + filter: {}, + paging: {}, + sorting: {}, + meta: { + sparseFields: ['id', 'name', 'location'], + }, + }, + ), + ), + ).toEqual( + print(gql` + query clubs( + $paging: OffsetPaging! + $filter: ClubFilter! + $sorting: [ClubSort!]! + ) { + data: clubs(paging: $paging, filter: $filter, sorting: $sorting) { + nodes { + id + name + location { + type + coordinates + } + } + pageInfo { + hasNextPage + hasPreviousPage + } + totalCount + } + } + `), + ); + }); }); describe(GET_MANY_REFERENCE, () => { @@ -428,6 +516,50 @@ describe(buildGqlQuery.name, () => { `), ); }); + + it('returns the correct query', () => { + expect( + print( + buildGqlQuery(introspectionResult)( + resource, + GET_MANY_REFERENCE, + resource[GET_MANY_REFERENCE], + { + filter: {}, + paging: {}, + sorting: {}, + meta: { + sparseFields: ['id', 'name', 'location'], + }, + }, + ), + ), + ).toEqual( + print(gql` + query clubs( + $paging: OffsetPaging! + $filter: ClubFilter! + $sorting: [ClubSort!]! + ) { + data: clubs(paging: $paging, filter: $filter, sorting: $sorting) { + nodes { + id + name + location { + type + coordinates + } + } + pageInfo { + hasNextPage + hasPreviousPage + } + totalCount + } + } + `), + ); + }); }); describe(GET_ONE, () => { @@ -465,6 +597,37 @@ describe(buildGqlQuery.name, () => { `), ); }); + + it('returns the correct query with sparse fieldset', () => { + expect( + print( + buildGqlQuery(introspectionResult)( + resource, + GET_ONE, + resource[GET_ONE], + { + id: 'cc76f014-7dc7-4cc3-bb5b-a7222b9b727f', + meta: { + sparseFields: ['id', 'name', 'location'], + }, + }, + ), + ), + ).toEqual( + print(gql` + query club($id: ID!) { + data: club(id: $id) { + id + name + location { + type + coordinates + } + } + } + `), + ); + }); }); describe(UPDATE, () => { diff --git a/src/buildGqlQuery.ts b/src/buildGqlQuery.ts index 034f25f..9e5dedb 100644 --- a/src/buildGqlQuery.ts +++ b/src/buildGqlQuery.ts @@ -44,7 +44,7 @@ function processSparseFields( const permittedSparseFields: ProcessedFields = sparseFields.reduce( (permitted: ProcessedFields, sparseField: SparseField) => { let expandedSparseField: ExpandedSparseField; - if (typeof sparseField == 'string') + if (typeof sparseField === 'string') expandedSparseField = { fields: [sparseField] }; else { const [linkedType, linkedSparseFields] = Object.entries(sparseField)[0];