Sorting GraphQL results
Solution
You can sort GraphQL query results using an index and a user-defined function.
The following example uses the following components:
-
A GraphQL schema named
schema.gql
-
A collection named
Posts
-
An index named
all_posts_sorted_by_title
-
A user-defined function named
sort_by_title
The GraphQL schema schema.gql
looks like this:
type Posts {
title: String!
content: String!
}
type Query {
allPostsSortedByTitle: [Posts!]! @resolver(name: "sort_by_title", paginated: true)
}
When you import the above schema, the GraphQL API automatically
creates a collection named Posts
and a UDF named sort_by_title
.
For more information about importing GraphQL schemas, see the GraphQL quick start. |
We need an index to sort our documents:
{
ref: Index("all_posts_sorted_by_title"),
ts: 1635289589810000,
active: true,
serialized: true,
name: 'all_posts_sorted_by_title',
source: Collection("Posts"),
values: [ { field: [ 'data', 'title' ] }, { field: [ 'ref' ] } ],
partitions: 8
}
We need some documents to sort. We can do this in either FQL or GraphQL. In FQL, the document creation command looks like this:
[
{
ref: Ref(Collection("Posts"), "305856982752952832"),
ts: 1627946798960000,
data: { title: 'my second post', content: 'more placeholder content' }
},
{
ref: Ref(Collection("Posts"), "305856982752953856"),
ts: 1627946798960000,
data: { title: 'my first post', content: 'placeholder content' }
},
{
ref: Ref(Collection("Posts"), "305856982752954880"),
ts: 1627946798960000,
data: {
title: 'my third post',
content: 'even more placeholder content'
}
}
]
Or to create the documents with GraphQL, use the following mutation:
mutation {
post2: createPosts( data:{
title: "my second post"
content: "more placeholder content"
}) {
_id
},
post1: createPosts( data:{
title: "my first post"
content: "placeholder content"
}) {
_id
},
post3: createPosts( data:{
title: "my third post"
content: "even more placeholder content"
}) {
_id
}
}
Next, update the stub function sort_by_title
:
{
ref: Function("sort_by_title"),
ts: 1635289852000000,
name: 'sort_by_title',
body: Query(Lambda(["size", "after", "before"], Let({match: Match(Index("all_posts_sorted_by_title")), page: If(Equals(Var("before"), null), If(Equals(Var("after"), null), Paginate(Var("match"), {size: Var("size")}), Paginate(Var("match"), {after: Var("after"), size: Var("size")})), Paginate(Var("match"), {before: Var("before"), size: Var("size")}))}, Map(Var("page"), Lambda("values", Get(Select(1, Var("values")))))))),
data: { gql: 'not included for brevity' }
}
Now you have a function which gets results from the
all_posts_sorted_by_title
index and arranges them in alphabetical
order.
The following GraphQL query uses the allPostsSortedByTitle
query
specified in the schema:
query FindAllPosts {
allPostsSortedByTitle {
data {
title
content
}
}
}
The above query generates the following results, in alphabetical order by
title
:
{
"data": {
"allPostsSortedByTitle": {
"data": [
{
"title": "my first post",
"content": "placeholder content"
},
{
"title": "my second post",
"content": "more placeholder content"
},
{
"title": "my third post",
"content": "even more placeholder content"
}
]
}
}
}
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!