Skip to main content

Decorators

Overview of the decorators provided by the @jenyus-org/nestjs-graphql-utils package.

@FieldMap(deep: boolean = true, parent: string | string[] = []): FieldMap#

New in v1.4.0

This decorator wraps the resolveFieldMap utility from graphql-utils including the direct passing of arguments fed to the decorator. It returns a raw FieldMap instance which takes on the form of nested objects, where the keys represent the selected fields from the GraphQLResolveInfo. Keys with no sub-selections are assigned an empty object as their value.

QueryLive

Code

@Query(() => [PostObject])
async posts(
@FieldMap() fieldMap: FieldMap,
) {
console.log(fieldMap);
}

Output

{
"posts": {
"id": {},
"title": {},
"body": {},
"author": {
"id": {},
"username": {},
"firstName": {},
"lastName": {}
},
"comments": {
"id": {},
"body": {},
"author": {
"id": {},
"username": {},
"firstName": {},
"lastName": {}
}
}
}
}

@FieldNodeAt(path: string | string[]): FieldNode | undefined#

New in v1.5.0

@FieldNodeAt wraps the getFieldNode() utility from graphql-utils, which takes a path and then tries to find a field node at the specified location in the GraphQLResolveInfo. If none was found, undefined is returned instead.

@Fields(deep: boolean = true, parent: string | string[] = []): string[]#

New in v1.4.0

@Fields is a wrapper over the resolveFields utility from graphql-utils, which itself is a light wrapper of the resolveFieldMaps utility that remaps the FieldMap return by the function with fieldMapToDot.

Instead of returning a FieldMap result, it returns a string[] which are a list of all the requested fields in dot notation.

QueryLive

Code

@Query(() => [PostObject])
async posts(
@Fields() fields: string[],
) {
console.log(fields);
}

Output

["posts","posts.id","posts.title","posts.body","posts.author","posts.author.id","posts.author.username","posts.author.firstName","posts.author.lastName","posts.comments","posts.comments.id","posts.comments.body","posts.comments.author","posts.comments.author.id","posts.comments.author.username","posts.comments.author.firstName","posts.comments.author.lastName"]

@HasFields(...fields: (string | string[])[]): boolean#

@HasFields() uses the received GraphQLResolveInfo from the incoming request, to run hasFields for every argument passed. Just like hasFields it supports array syntax and dot notation, and uses AND bitwise operations ensuring that every field passed resolves to true.

QueryLive

Code

@Query(() => [PostObject])
async posts(
@HasFields("posts.id") wantsId: boolean,
) {
console.log(wantsId);
}

Output

true

@Selections(fieldSelections: string | string[] | FieldSelections[], fields?: string[], asParent: boolean = true): string[]#

Updated in v1.6.0

Similar to @HasFields(), @Selections acts as a layer on top of resolveSelections, and additionally contains some logic to solve the most common use-case.

fieldSelections can be the same type of argument as accepted by resolveSelections, and will return an array of selectors which were found in the query.

Additionally, instead of passing the field selection array, users may pass a simple string, acting as the root object for subsequent fields. In this case the second argument, fields is required, and must be an array of strings to search within the parent selector.

QueryLive

Code

@Query(() => [PostObject])
async posts(
@Selections("posts", ["author", "comments"]) fieldSelections: string[]
) {
console.log(fieldSelections);
}

Output

["author","comments"]

A final argument that may be specified, withParent, allows @Selections() to automatically remap the fields specified in the second argument and prepend them with the parent. This can be useful to do more fine-grained checks, especially when searching for subselection of relationships like so:

QueryLive

Code

@Query(() => [PostObject])
async posts(
@Selections("posts", ["comments"], true) fieldSelections: string[]
) {
console.log(fieldSelections);
}

Output

["posts.comments"]