{"id":27198435,"url":"https://github.com/slaypni/type-graphql-dataloader","last_synced_at":"2025-04-09T20:39:33.388Z","repository":{"id":41336743,"uuid":"285437764","full_name":"slaypni/type-graphql-dataloader","owner":"slaypni","description":"TypeGraphQL + DataLoader + TypeORM made easy","archived":false,"fork":false,"pushed_at":"2024-01-10T08:36:43.000Z","size":396,"stargazers_count":154,"open_issues_count":20,"forks_count":16,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-18T08:15:21.248Z","etag":null,"topics":["apollo-server","dataloader","graphql","type-graphql","typeorm","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/type-graphql-dataloader","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/slaypni.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-06T00:52:26.000Z","updated_at":"2025-03-07T23:41:43.000Z","dependencies_parsed_at":"2024-06-18T15:36:55.732Z","dependency_job_id":"b81c0736-2a8c-4736-b26c-e1aa12bb52d0","html_url":"https://github.com/slaypni/type-graphql-dataloader","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slaypni%2Ftype-graphql-dataloader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slaypni%2Ftype-graphql-dataloader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slaypni%2Ftype-graphql-dataloader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slaypni%2Ftype-graphql-dataloader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/slaypni","download_url":"https://codeload.github.com/slaypni/type-graphql-dataloader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248109382,"owners_count":21049302,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apollo-server","dataloader","graphql","type-graphql","typeorm","typescript"],"created_at":"2025-04-09T20:39:32.913Z","updated_at":"2025-04-09T20:39:33.366Z","avatar_url":"https://github.com/slaypni.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TypeGraphQL-DataLoader\n\nTypeGraphQL-DataLoader is an utility to use DataLoader with TypeGraphQL without fuss.\n\n## Install\n\n```\nnpm install type-graphql-dataloader\n```\n\nThe latest build is tested with the following packages:\n\n- type-graphql 1.1.1\n- apollo-server-express 3.4.0\n- (optional) typeorm 0.2.38\n\n## Getting Started\n\nApollo Server is the first-class supported server. If your application uses Apollo Server, pass `ApolloServerLoaderPlugin()` as a plugin when instantiating the server. This plugin is for set-up and clean-up against each request.\n\n```ts\nimport { ApolloServerLoaderPlugin } from \"type-graphql-dataloader\";\nimport { getConnection } from \"typeorm\";\n\n...\n\nconst apollo = new ApolloServer({\n  schema,\n  plugins: [\n    ApolloServerLoaderPlugin({\n      typeormGetConnection: getConnection,  // for use with TypeORM\n    }),\n  ],\n});\n\n...\n```\n\n### With TypeORM\n\nTypeORM is the first-class supported ORM. If your application uses TypeORM with TypeGraphQL, adding `@TypeormLoader` decorator to relation properties will solve N + 1 problem. When the fields are accessed by graphQL, batch loading will be performed using DataLoader under the hood.\n\n```ts\nimport { ObjectType, Field, ID } from \"type-graphql\";\nimport { TypeormLoader } from \"type-graphql-dataloader\";\nimport { Entity, PrimaryGeneratedColumn, ManyToOne, RelationId } from \"typeorm\";\nimport { User } from \"./User\";\n\n@ObjectType()\n@Entity()\nexport class Photo {\n  @Field((type) =\u003e ID)\n  @PrimaryGeneratedColumn()\n  id: number;\n\n  @Field((type) =\u003e User)\n  @ManyToOne((type) =\u003e User, (user) =\u003e user.photos)\n  @TypeormLoader()\n  user: User;\n}\n```\n\n```ts\nimport { ObjectType, Field, ID } from \"type-graphql\";\nimport { TypeormLoader } from \"type-graphql-dataloader\";\nimport { Entity, PrimaryGeneratedColumn, OneToMany, RelationId } from \"typeorm\";\nimport { Photo } from \"./Photo\";\n\n@ObjectType()\n@Entity()\nexport class User {\n  @Field((type) =\u003e ID)\n  @PrimaryGeneratedColumn()\n  id: number;\n\n  @Field((type) =\u003e [Photo])\n  @OneToMany((type) =\u003e Photo, (photo) =\u003e photo.user)\n  @TypeormLoader()\n  photos: Photo[];\n}\n```\n\n`@TypeormLoader` does not need arguments since `v0.4.0`. In order to pass foeign key explicitly, arguments are still supported. Take a look at previous [README](https://github.com/slaypni/type-graphql-dataloader/blob/v0.3.7/README.md#with-typeorm) for details.\n\n### With Custom DataLoader\n\nIt is possible to assign custom DataLoader to a field by adding `@Loader` decorator to the corresponding method with `@FieldResolver`. `@Loader` takes a batch load function which is passed to the DataLoader constructor. The decorated method should return a function which takes a DataLoader instance and returns Promise of loaded value(s).\n\n```ts\nimport DataLoader from \"dataloader\";\nimport { groupBy } from \"lodash\";\nimport { Resolver, Query, FieldResolver, Root } from \"type-graphql\";\nimport { Loader } from \"type-graphql-dataloader\";\nimport { getRepository, In } from \"typeorm\";\nimport { Photo } from \"./Photo\";\nimport { User } from \"./User\";\n\n@Resolver((of) =\u003e User)\nexport default class UserResolver {\n\n  ...\n\n  @FieldResolver()\n  @Loader\u003cnumber, Photo[]\u003e(async (ids, { context }) =\u003e {  // batchLoadFn\n    const photos = await getRepository(Photo).find({\n      where: { user: { id: In([...ids]) } },\n    });\n    const photosById = groupBy(photos, \"userId\");\n    return ids.map((id) =\u003e photosById[id] ?? []);\n  })\n  photos(@Root() root: User) {\n    return (dataloader: DataLoader\u003cnumber, Photo[]\u003e) =\u003e\n      dataloader.load(root.id);\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslaypni%2Ftype-graphql-dataloader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fslaypni%2Ftype-graphql-dataloader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslaypni%2Ftype-graphql-dataloader/lists"}