Skip to content

Commit fad476d

Browse files
authored
fix(graphql): avoid errors with sort as empty string (#14594)
#14486 introduced a regression when you pass `sort` parameter as `""` to a GraphQL query, for example: ```graphql query { Sorts(sort: "") { docs { id title number } } } ``` In this case, MongoDB gives an error: ``` "FieldPath must not end with a '.'." ``` This PR fixes that by avoiding passing `sort: ['']` to the Local API.
1 parent ad8f0b2 commit fad476d

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

packages/graphql/src/resolvers/collections/find.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export function findResolver(collection: Collection): Resolver {
6161
pagination: args.pagination,
6262
req,
6363
select,
64-
sort: typeof sort === 'string' ? sort.split(',') : undefined,
64+
sort: sort && typeof sort === 'string' ? sort.split(',') : undefined,
6565
trash: args.trash,
6666
where: args.where,
6767
}

packages/graphql/src/resolvers/collections/findVersions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function findVersionsResolver(collection: Collection): Resolver {
5858
pagination: args.pagination,
5959
req,
6060
select,
61-
sort: typeof sort === 'string' ? sort.split(',') : undefined,
61+
sort: sort && typeof sort === 'string' ? sort.split(',') : undefined,
6262
trash: args.trash,
6363
where: args.where,
6464
}

packages/graphql/src/resolvers/globals/findVersions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function findVersions(globalConfig: SanitizedGlobalConfig): Resolver {
4646
pagination: args.pagination,
4747
req,
4848
select,
49-
sort: typeof sort === 'string' ? sort.split(',') : undefined,
49+
sort: sort && typeof sort === 'string' ? sort.split(',') : undefined,
5050
where: args.where,
5151
}
5252

test/collections-graphql/int.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,25 @@ describe('collections-graphql', () => {
135135
expect(docs.map((doc) => doc.id)).toEqual([doc3.id, doc1.id, doc4.id, doc2.id])
136136
})
137137

138+
it('should not fail with sort as empty string', async () => {
139+
const query = `query {
140+
Sorts(sort: "") {
141+
docs {
142+
id
143+
title
144+
number
145+
}
146+
}
147+
}`
148+
149+
const { data } = await restClient
150+
.GRAPHQL_POST({ body: JSON.stringify({ query }) })
151+
.then((res) => res.json())
152+
const { docs } = data.Sorts
153+
154+
expect(docs).toBeTruthy()
155+
})
156+
138157
it('should count', async () => {
139158
const query = `query {
140159
countPosts {

0 commit comments

Comments
 (0)