Skip to content

Commit

Permalink
Modified: src/buildGqlQuery.test.ts src/buildGqlQuery.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnkr committed Oct 3, 2024
1 parent 1f80118 commit 8084329
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 1 deletion.
163 changes: 163 additions & 0 deletions src/buildGqlQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand Down Expand Up @@ -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, () => {
Expand Down Expand Up @@ -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, () => {
Expand Down Expand Up @@ -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, () => {
Expand Down
2 changes: 1 addition & 1 deletion src/buildGqlQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 8084329

Please sign in to comment.