Skip to content

Commit

Permalink
Modified: src/buildGqlQuery.test.ts src/buildGqlQuery.ts src/buildVar…
Browse files Browse the repository at this point in the history
…iables.test.ts src/buildVariables.ts src/types.d.ts
  • Loading branch information
mrnkr committed Oct 4, 2024
1 parent 8084329 commit feb8f30
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/buildGqlQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ describe(buildGqlQuery.name, () => {
);
});

it('returns the correct query', () => {
it('returns the correct query with sparse fieldset', () => {
expect(
print(
buildGqlQuery(introspectionResult)(
Expand Down
2 changes: 1 addition & 1 deletion src/buildGqlQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
118 changes: 118 additions & 0 deletions src/buildVariables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand Down Expand Up @@ -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, () => {
Expand Down Expand Up @@ -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, () => {
Expand Down
6 changes: 6 additions & 0 deletions src/buildVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -68,6 +69,7 @@ export default (introspectionResults: IntrospectionResult) =>
case GET_ONE:
return {
id: preparedParams.id,
meta: preparedParams.meta,
};
case DELETE:
return buildDeleteOneVariables(introspectionResults)(
Expand Down Expand Up @@ -259,6 +261,10 @@ const getListVariables = (
];
}

if (params.meta) {
variables.meta = params.meta;
}

return variables;
};

Expand Down
7 changes: 7 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { SparseField } from './buildGqlQuery';

export type FilterComparison<T> = {
eq?: T;
gt?: T;
Expand Down Expand Up @@ -33,10 +35,15 @@ export type Filter = {
[key: string]: FilterComparison<any>;
};

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 = {
Expand Down

0 comments on commit feb8f30

Please sign in to comment.