Skip to content

Commit

Permalink
FAI-6292: Don't include redundant _and (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
eskrm authored May 17, 2023
1 parent df0cd4b commit ecc49c7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 39 deletions.
86 changes: 47 additions & 39 deletions src/graphql/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,48 +496,56 @@ export function paginateWithKeysetV2(query: string): PaginatedQuery {
return false;
}
edgesPath.push(node.name.value);
return {
...node,
arguments: [
mergeWhereClauses([
...(node.arguments?.filter((n) => n.name.value === 'where') ??
[]),
{
kind: 'Argument',
name: {kind: 'Name', value: 'where'},
value: {
kind: 'ObjectValue',
fields: [
{
kind: 'ObjectField',
name: {
kind: 'Name',
value: 'id',
},
value: {
kind: 'ObjectValue',
fields: [
{
kind: 'ObjectField',
name: {
kind: 'Name',
value: '_gt',
},
value: {
kind: 'Variable',
name: {
kind: 'Name',
value: 'id',
},
},

const existingWhereArgs = node.arguments?.filter(
(n) => n.name.value === 'where'
) ?? [];
let whereArgs: gql.ArgumentNode[] = [
...existingWhereArgs,
{
kind: gql.Kind.ARGUMENT,
name: {kind: gql.Kind.NAME, value: 'where'},
value: {
kind: gql.Kind.OBJECT,
fields: [
{
kind: gql.Kind.OBJECT_FIELD,
name: {
kind: gql.Kind.NAME,
value: 'id',
},
value: {
kind: gql.Kind.OBJECT,
fields: [
{
kind: gql.Kind.OBJECT_FIELD,
name: {
kind: gql.Kind.NAME,
value: '_gt',
},
value: {
kind: gql.Kind.VARIABLE,
name: {
kind: gql.Kind.NAME,
value: 'id',
},
],
},
},
},
],
],
},
},
},
]),
],
},
},
];
if (whereArgs.length > 1) {
whereArgs = [mergeWhereClauses(whereArgs)];
}

return {
...node,
arguments: [
...whereArgs,
{
kind: 'Argument',
name: {kind: 'Name', value: 'order_by'},
Expand Down
11 changes: 11 additions & 0 deletions test/graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ describe('graphql', () => {
});

test('paginated keyset v2 query', async () => {
const query = await loadQueryFile('incidents-v2.gql');
const paginatedQuery = sut.paginateWithKeysetV2(query);
const expectedQuery =
await loadQueryFile('paginated-incidents-keyset-v2.gql');
expect(paginatedQuery.query).toEqual(expectedQuery);
expect(paginatedQuery.edgesPath).toEqual(['ims_Incident']);
expect(paginatedQuery.edgeIdPath).toEqual(['_id']);
expect(paginatedQuery.pageInfoPath).toBeEmpty();
});

test('paginated keyset v2 query with existing where clause', async () => {
const query = await loadQueryFile('commits-v2.gql');
const paginatedQuery = sut.paginateWithKeysetV2(query);
const expectedQuery =
Expand Down
11 changes: 11 additions & 0 deletions test/resources/queries/paginated-incidents-keyset-v2.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
query paginatedQuery($id: String, $limit: Int) {
ims_Incident(where: {id: {_gt: $id}}, order_by: {id: asc}, limit: $limit) {
_id: id
incidentUid: uid
impactedApplications {
application {
applicationName: name
}
}
}
}

0 comments on commit ecc49c7

Please sign in to comment.