Skip to content

Commit

Permalink
feat: paginate offices query
Browse files Browse the repository at this point in the history
  • Loading branch information
tedraykov committed May 22, 2022
1 parent acd332e commit 502f0c1
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ export default async function register(app) {
version: pkg.version,
collections: {
EcontOffices: {
name: "EcontOffices"
name: "EcontOffices",
indexes: [
[{ code: 1 }],
[{ name: 1 }],
[{ nameEn: 1 }],
[{ "address.address1": 1 }]
]
}
},
queries,
Expand Down
28 changes: 26 additions & 2 deletions src/queries/econtOffices.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
/**
* Fetch all econt offices
* @param {Object} context - graphql context
* @param {Object} input - Request input
* @param {String} [query] - Regex match query string
* @returns {Promise<*>} - econt offices
*/
export default async function econtOffices(context) {
export default async function econtOffices(context, input) {
const { searchQuery } = input;

const { collections: { EcontOffices } } = context;
return EcontOffices.find({}).toArray();

const query = {};

if (searchQuery) {
query.$or = [
{
code: { $regex: searchQuery, $options: "i" }
},
{
name: { $regex: searchQuery, $options: "i" }
},
{
nameEn: { $regex: searchQuery, $options: "i" }
},
{
"address.address1": { $regex: searchQuery, $options: "i" }
}
];
}

return EcontOffices.find(query);
}
19 changes: 17 additions & 2 deletions src/resolvers/Query/econtOffices.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import getPaginatedResponse from "@reactioncommerce/api-utils/graphql/getPaginatedResponse.js";
import wasFieldRequested from "@reactioncommerce/api-utils/graphql/wasFieldRequested.js";

/**
* Fetch all econt offices
* @param {Object} parentResult - unused
* @param {ConnectionArgs} args - unused
* @param {Object} context - graphql execution context
* @param {Object} info Info about the GraphQL request
* @returns {Promise<Object>} - econt offices
*/
export default async function econtOffices(parentResult, args, context) {
return context.queries.econtOffices(context);
export default async function econtOffices(parentResult, args, context, info) {
const {
searchQuery,
...connectionArgs
} = args;

const query = await context.queries.econtOffices(context, { searchQuery });

return getPaginatedResponse(query, connectionArgs, {
includeHasNextPage: wasFieldRequested("pageInfo.hasNextPage", info),
includeHasPreviousPage: wasFieldRequested("pageInfo.hasPreviousPage", info),
includeTotalCount: wasFieldRequested("totalCount", info)
});
}
58 changes: 55 additions & 3 deletions src/schemas/schemas.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,68 @@ type EcontOfficeAddress {
region: String!
}

type EcontOffice {
type EcontOffice implements Node {
"The office ID"
_id: ID
_id: ID!
code: String!
name: String,
nameEn: String,
address: EcontOfficeAddress,
phones: [String],
}

"A connection edge in which each node is a `EcontOffice` object"
type EcontOfficeEdge {
"The cursor that represents this node in the paginated results"
cursor: ConnectionCursor!

"The econt office"
node: EcontOffice
}

"""
Wraps a list of `CatalogItem`s, providing pagination cursors and information.
For information about what Relay-compatible connections are and how to use them, see the following articles:
- [Relay Connection Documentation](https://facebook.github.io/relay/docs/en/graphql-server-specification.html#connections)
- [Relay Connection Specification](https://facebook.github.io/relay/graphql/connections.htm)
- [Using Relay-style Connections With Apollo Client](https://www.apollographql.com/docs/react/recipes/pagination.html)
"""
type EcontOfficeConnection {
"The list of nodes that match the query, wrapped in an edge to provide a cursor string for each"
edges: [EcontOfficeEdge]

"""
You can request the `nodes` directly to avoid the extra wrapping that `NodeEdge` has,
if you know you will not need to paginate the results.
"""
nodes: [EcontOffice]

"Information to help a client request the next or previous page"
pageInfo: PageInfo!

"The total number of nodes that match your query"
totalCount: Int!
}

extend type Query {
econtOffices: [EcontOffice]
econtOffices(
"Optional text search query"
searchQuery: String,

"Return only results that come after this cursor. Use this with `first` to specify the number of results to return."
after: ConnectionCursor,

"Return only results that come before this cursor. Use this with `last` to specify the number of results to return."
before: ConnectionCursor,

"Return at most this many results. This parameter may be used with either `after` or `offset` parameters."
first: ConnectionLimitInt,

"Return at most this many results. This parameter may be used with the `before` parameter."
last: ConnectionLimitInt,

"Return only results that come after the Nth result. This parameter may be used with the `first` parameter."
offset: Int,
): EcontOfficeConnection
}

0 comments on commit 502f0c1

Please sign in to comment.