Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
@agilie/nestjs-graphql-dataloader
Advanced tools
Based on https://github.com/krislefeber/nestjs-dataloader this small library assists in adding https://github.com/graphql/dataloader to a NestJS project.
This package also ensures that the ids are mapped to the dataloader in the correct sequence automatically and provides a helpful base class to simplify dataloader creation.
Requires NestJS 7+
npm:
npm i nestjs-graphql-dataloader --save
yarn:
yarn add nestjs-graphql-dataloader
First, register a NestJS interceptor in your applications root module(s) providers configuration. This can actually be placed in any of your modules and it will be available anywhere but I would recommend your root module(s). It only needs to be defined once.
Add:
{
provide: APP_INTERCEPTOR,
useClass: DataLoaderInterceptor,
}
For example:
import { DataLoaderInterceptor } from 'nestjs-graphql-dataloader'
...
@Module({
providers: [
{
provide: APP_INTERCEPTOR,
useClass: DataLoaderInterceptor,
},
],
...
imports: [
RavenModule,
ConfigModule.load(
path.resolve(__dirname, '../../config', '**/!(*.d).{ts,js}'),
),
Using the provided template method, OrderedNestDataLoader<KeyType, EntityType>
, you can easily implement DataLoaders for your types. Here is an example:
import { Injectable } from '@nestjs/common'
import { OrderedNestDataLoader } from 'nestjs-graphql-dataloader'
import { Location } from '../core/location.entity'
import { LocationService } from '../core/location.service'
@Injectable()
export class LocationLoader extends OrderedNestDataLoader<Location['id'], Location> {
constructor(private readonly locationService: LocationService) {
super()
}
protected getOptions = () => ({
query: (keys: Array<Location['id']>) => this.locationService.findByIds(keys),
})
}
Note: In these examples the usage of
Location['id']
is referring to the type of thelocation.id property
, which in this case isstring
. It would be perfectly acceptable to declare the generic type argument asstring
rather thanLocation['id']
.
Add these to your modules providers as usual. You will most likely want to include it in your modules exports so the loader can be imported by resolvers in other modules.
getOptions
takes a single options
argument which has the following interface:
interface IOrderedNestDataLoaderOptions<ID, Type> {
propertyKey?: string
query: (keys: readonly ID[]) => Promise<Type[]>
typeName?: string
}
Since the majority of the time a propertyKey
is 'id'
this is the default if not specified.
The typeName
for the above example is automatically assigned 'Location'
which is derived from the class name, this is just used for logging errors.
The query is the equivalent of a repository.findByIds(ids)
operation. It should return the same number of elements as requested. The order does not matter as the base loader implementation takes care of this.
To then use the resolver it just needs to be injected into the resolvers field resolver method. Here is an example:
import DataLoader from 'dataloader'
...
@ResolveField(returns => [Location])
public async locations(
@Parent() company: Company,
@Loader(LocationLoader)
locationLoader: DataLoader<Location['id'], Location>,
) {
return locationLoader.loadMany(company.locationIds)
}
[0.1.28] - 2020-05-11
FAQs
NestJS GraphQL Dataloader
We found that @agilie/nestjs-graphql-dataloader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.