GraphQL/ResolverUtilities

GraphQL/ResolverUtilities

Source:

Utility functions for use by GraphQL resolvers

Methods

(static) applyBeforeAfterToFilter(args) → {Object}

Adjusts a MongoDB filter based on GraphQL before and after params

Source:
Parameters:
Name Type Description
args Object
Properties
Name Type Attributes Description
after Object <optional>

A document that all results should be filtered to be after.

baseFilter Object <optional>

The MongoDB filter object to extend.

before Object <optional>

A document that all results should be filtered to be before.

sortBy String <optional>

The name of the field we are sorting by. Default _id

sortOrder String <optional>

The sort order, "asc" or "desc". Default "asc"

Returns:

The potentially-modified filter object

Type
Object

(static) applyPaginationToMongoCursor(cursor, args, totalCount) → {Promise.<Object>}

Adds skip and limit to a MongoDB cursor as necessary, based on GraphQL first and last params

Source:
Parameters:
Name Type Description
cursor Cursor

MongoDB cursor

args Object

GraphQL query arguments

totalCount Number

Total count of docs that match the query, after applying the before/after filter

Returns:

{ pageInfo }

Type
Promise.<Object>

(static) getCollectionFromCursor()

Get the associated Mongo Collection instance for a given Cursor instance

Source:

(static) getConnectionTypeResolvers() → {Object}

Makes resolvers for connection and edge types, based on the parent resolver returning an object that has a nodes property that is an array of the items.

Source:
Returns:

An object with ${name}Connection and ${name}Edge properties, to be included in the resolvers object.

Type
Object

(static) getMongoSort() → {Object}

Converts GraphQL sortBy and sortOrder params to the sort object format that MongoDB uses.

Source:

Note that this uses the object format rather than the array format because our in-memory db for tests expects object format. The Node mongodb package allows either. Technically an array would be better because JS does not guarantee preservation of object key order, but this seems to work fine.

Returns:

Sort object

Type
Object

(static) getPaginatedResponse(mongoCursor, args) → {Promise.<Object>}

Given a MongoDB cursor, adds skip, limit, sort, and other filters as necessary based on GraphQL resolver arguments.

Source:

Resolvers that return multiple documents in the form of a connection should construct a MongoDB query, pass that cursor to this function, and then return the result.

Parameters:
Name Type Description
mongoCursor Cursor

Node MongoDB Cursor instance. Will be mutated.

args Object

Connection arguments from GraphQL query

Returns:

{ nodes, pageInfo, totalCount }

Type
Promise.<Object>

(static) optimizeIdOnly() → {Object}

Wraps a query function, and calls it only if fields other than _id were requested in the GraphQL query.

Source:

This is an optimization so that we can avoid a database lookup if, for example, the client has requested only shop { _id } and we already have shopId prop from the parent resolver.

Returns:

{ nodes, pageInfo, totalCount }

Type
Object

(static) resolveAccountFromAccountId(parent, _, context) → {Promise.<Object>}

A generic resolver that gets the account object for the provided parent result, assuming it has a accountId property

Source:
Parameters:
Name Type Description
parent Object

result of the parent resolver

_ Object

unused param

context Object

an object containing the per-request state

Returns:

The account having ID parent.accountId, in GraphQL schema format

Type
Promise.<Object>

(static) resolveShopFromShopId(parent, args, context) → {Promise.<Object>}

A generic resolver that gets the shop object for the provided parent result, assuming it has a shopId property

Source:
Parameters:
Name Type Description
parent Object

result of the parent resolver

args Object

an object of all arguments that were sent by the client

context Object

an object containing the per-request state

Returns:

The shop having ID parent.shopId, in GraphQL schema format

Type
Promise.<Object>