diff --git a/src/buildGqlQuery.test.ts b/src/buildGqlQuery.test.ts index dae3be4..0f5fdd2 100644 --- a/src/buildGqlQuery.test.ts +++ b/src/buildGqlQuery.test.ts @@ -517,7 +517,7 @@ describe(buildGqlQuery.name, () => { ); }); - it('returns the correct query', () => { + it('returns the correct query with sparse fieldset', () => { expect( print( buildGqlQuery(introspectionResult)( diff --git a/src/buildGqlQuery.ts b/src/buildGqlQuery.ts index 9e5dedb..c0af390 100644 --- a/src/buildGqlQuery.ts +++ b/src/buildGqlQuery.ts @@ -25,7 +25,7 @@ import * as t from 'graphql-ast-types'; import getFinalType from './getFinalType'; import { getGqlType } from './getGqlType'; -type SparseField = string | { [k: string]: SparseField[] }; +export type SparseField = string | { [k: string]: SparseField[] }; type ExpandedSparseField = { linkedType?: string; fields: SparseField[] }; type ProcessedFields = { resourceFields: IntrospectionField[]; diff --git a/src/buildVariables.test.ts b/src/buildVariables.test.ts index c539d94..69e9fd6 100644 --- a/src/buildVariables.test.ts +++ b/src/buildVariables.test.ts @@ -116,6 +116,56 @@ describe(buildVariables.name, () => { ], }); }); + + it('returns correct variables with sparse fieldset', () => { + const params = { + filter: { + ids: ['foo1', 'foo2'], + title: 'Foo', + views: 100, + }, + pagination: { page: 10, perPage: 10 }, + sort: { field: 'sortField', order: 'DESC' }, + meta: { + sparseFields: ['id', 'name', 'location'], + }, + }; + + expect( + buildVariables(introspectionResult)( + { + type: { + kind: TypeKind.OBJECT, + name: 'Post', + fields: [ + { + name: 'title', + type: { + name: 'String', + kind: TypeKind.SCALAR, + } as IntrospectionScalarType, + } as IntrospectionField, + ], + interfaces: [], + }, + }, + GET_LIST, + params, + {}, + ), + ).toEqual({ + filter: { + id: { in: ['foo1', 'foo2'] }, + title: { iLike: 'Foo' }, + views: { eq: 100 }, + }, + paging: { limit: 10, offset: 90 }, + sorting: [{ field: 'sortField', direction: 'DESC' }], + meta: { + sparseFields: ['id', 'name', 'location'], + }, + }); + }); }); describe(CREATE, () => { @@ -219,6 +269,35 @@ describe(buildVariables.name, () => { }, }); }); + + it('returns correct variables with sparse fieldset', () => { + const params = { + ids: ['tag1', 'tag2'], + meta: { + sparseFields: ['id', 'name', 'location'], + }, + }; + + const resource = getResourceByName('Club'); + + expect( + buildVariables(introspectionResult)( + resource, + GET_MANY, + params, + resource[GET_MANY], + ), + ).toEqual({ + filter: { id: { in: ['tag1', 'tag2'] } }, + paging: { + limit: 2, + offset: 0, + }, + meta: { + sparseFields: ['id', 'name', 'location'], + }, + }); + }); }); describe(GET_MANY_REFERENCE, () => { @@ -338,6 +417,45 @@ describe(buildVariables.name, () => { ], }); }); + + it('returns correct variables with sparse fieldset', () => { + const params = { + target: 'club', + id: '103daae0-ef3f-4f1b-a114-528df8047cb4', + filter: { sport: 'Football' }, + pagination: { page: 1, perPage: 10 }, + sort: { field: 'name', order: 'ASC' }, + meta: { + sparseFields: ['id', 'name', 'location'], + }, + }; + + const resource = getResourceByName('Field'); + + expect( + buildVariables(introspectionResult)( + resource, + GET_MANY_REFERENCE, + params, + resource[GET_MANY_REFERENCE], + ), + ).toEqual({ + filter: { club: { id: { eq: params.id } }, sport: { eq: 'Football' } }, + paging: { + limit: 10, + offset: 0, + }, + sorting: [ + { + field: 'name', + direction: 'ASC', + }, + ], + meta: { + sparseFields: ['id', 'name', 'location'], + }, + }); + }); }); describe(DELETE, () => { diff --git a/src/buildVariables.ts b/src/buildVariables.ts index 2ff65fc..27b35a2 100644 --- a/src/buildVariables.ts +++ b/src/buildVariables.ts @@ -54,6 +54,7 @@ export default (introspectionResults: IntrospectionResult) => return { filter: { id: { in: preparedParams.ids } }, paging: { limit: preparedParams.ids.length, offset: 0 }, + meta: preparedParams.meta, }; case GET_MANY_REFERENCE: let variables = getListVariables(resource, preparedParams); @@ -68,6 +69,7 @@ export default (introspectionResults: IntrospectionResult) => case GET_ONE: return { id: preparedParams.id, + meta: preparedParams.meta, }; case DELETE: return buildDeleteOneVariables(introspectionResults)( @@ -259,6 +261,10 @@ const getListVariables = ( ]; } + if (params.meta) { + variables.meta = params.meta; + } + return variables; }; diff --git a/src/types.d.ts b/src/types.d.ts index 13248c5..d3c8738 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1,3 +1,5 @@ +import type { SparseField } from './buildGqlQuery'; + export type FilterComparison = { eq?: T; gt?: T; @@ -33,10 +35,15 @@ export type Filter = { [key: string]: FilterComparison; }; +export type QueryMeta = { + sparseFields?: SparseField[]; +}; + export type QueryArgs = { filter?: Filter; paging?: { limit: number; offset: number }; sorting?: { field: string; direction: SortDirection }[]; + meta?: QueryMeta; }; export type CreateOneInput = {