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

object of arguments passed

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) applyBeforeAfterToFilter(args) → {Object}

Adjusts a MongoDB filter based on GraphQL before and after params

Source:
Parameters:
Name Type Description
args Object

object of arguments passed

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) applyOffsetPaginationToMongoCursor(cursor, args, options) → {Promise.<Object>}

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

Source:

Inspired by https://www.reindex.io/blog/relay-graphql-pagination-with-mongodb/

Parameters:
Name Type Description
cursor Cursor

MongoDB cursor

args Object

An object with offset or last but not both.

options Object

Options

Properties
Name Type Attributes Description
includeHasNextPage Boolean <optional>

Whether to return the pageInfo.hasNextPage. Default is true. Set this to false if you don't need it to avoid an extra database command.

Returns:

{ hasNextPage, hasPreviousPage }

Type
Promise.<Object>

(static) applyOffsetPaginationToMongoCursor(cursor, args, options) → {Promise.<Object>}

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

Source:

Inspired by https://www.reindex.io/blog/relay-graphql-pagination-with-mongodb/

Parameters:
Name Type Description
cursor Cursor

MongoDB cursor

args Object

An object with offset or last but not both.

options Object

Options

Properties
Name Type Attributes Description
includeHasNextPage Boolean <optional>

Whether to return the pageInfo.hasNextPage. Default is true. Set this to false if you don't need it to avoid an extra database command.

Returns:

{ hasNextPage, hasPreviousPage }

Type
Promise.<Object>

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

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

Source:

Inspired by https://www.reindex.io/blog/relay-graphql-pagination-with-mongodb/

Parameters:
Name Type Description
cursor Cursor

MongoDB cursor

args Object

An object with first or last but not both.

options Object

Options

Properties
Name Type Attributes Description
includeHasPreviousPage Boolean <optional>

Whether to return the pageInfo.hasPreviousPage. Default is true. Set this to false if you don't need it to avoid an extra database command.

includeHasNextPage Boolean <optional>

Whether to return the pageInfo.hasNextPage. Default is true. Set this to false if you don't need it to avoid an extra database command.

Returns:

{ hasNextPage, hasPreviousPage }

Type
Promise.<Object>

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

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

Source:

Inspired by https://www.reindex.io/blog/relay-graphql-pagination-with-mongodb/

Parameters:
Name Type Description
cursor Cursor

MongoDB cursor

args Object

An object with first or last but not both.

options Object

Options

Properties
Name Type Attributes Description
includeHasPreviousPage Boolean <optional>

Whether to return the pageInfo.hasPreviousPage. Default is true. Set this to false if you don't need it to avoid an extra database command.

includeHasNextPage Boolean <optional>

Whether to return the pageInfo.hasNextPage. Default is true. Set this to false if you don't need it to avoid an extra database command.

Returns:

{ hasNextPage, hasPreviousPage }

Type
Promise.<Object>

(static) getCollectionFromCursor(cursor) → {Object}

Get the associated Mongo Collection instance for a given Cursor instance

Source:
Parameters:
Name Type Description
cursor Cursor

MongoDB cursor

Returns:

database collection

Type
Object

(static) getCollectionFromCursor(cursor) → {Object}

Get the associated Mongo Collection instance for a given Cursor instance

Source:
Parameters:
Name Type Description
cursor Cursor

MongoDB cursor

Returns:

database collection

Type
Object

(static) getConnectionTypeResolvers(name) → {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:
Parameters:
Name Type Description
name String

name of connection / edge

Returns:

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

Type
Object

(static) getConnectionTypeResolvers(name) → {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:
Parameters:
Name Type Description
name String

name of connection / edge

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) 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, options) → {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

options Object

Options

Properties
Name Type Attributes Description
includeTotalCount Boolean <optional>

Whether to return the totalCount. Default is true. Set this to false if you don't need it to avoid an extra database command.

includeHasPreviousPage Boolean <optional>

Whether to return the pageInfo.hasPreviousPage. Default is true. Set this to false if you don't need it to avoid an extra database command.

includeHasNextPage Boolean <optional>

Whether to return the pageInfo.hasNextPage. Default is true. Set this to false if you don't need it to avoid an extra database command.

Returns:

{ nodes, pageInfo, totalCount }

Type
Promise.<Object>

(static) getPaginatedResponse(mongoCursor, args, options) → {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

options Object

Options

Properties
Name Type Attributes Description
includeTotalCount Boolean <optional>

Whether to return the totalCount. Default is true. Set this to false if you don't need it to avoid an extra database command.

includeHasPreviousPage Boolean <optional>

Whether to return the pageInfo.hasPreviousPage. Default is true. Set this to false if you don't need it to avoid an extra database command.

includeHasNextPage Boolean <optional>

Whether to return the pageInfo.hasNextPage. Default is true. Set this to false if you don't need it to avoid an extra database command.

Returns:

{ nodes, pageInfo, totalCount }

Type
Promise.<Object>

(static) optimizeIdOnly(_id, info, queryFunc) → {Object}

  • 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.

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.
Parameters:
Name Type Description
_id String

id to optimize

info Object

info on fields

queryFunc function

query function

Returns:

{ nodes, pageInfo, totalCount }

Type
Object

(static) optimizeIdOnly(_id, info, queryFunc) → {Object}

  • 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.

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.
Parameters:
Name Type Description
_id String

id to optimize

info Object

info on fields

queryFunc function

query function

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) 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, _, 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

_ Object

unused

context Object

an object containing the per-request state

Returns:

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

Type
Promise.<Object>

(static) resolveShopFromShopId(parent, _, 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

_ Object

unused

context Object

an object containing the per-request state

Returns:

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

Type
Promise.<Object>