{"id":13514278,"url":"https://github.com/vektah/dataloaden","last_synced_at":"2025-04-08T12:08:09.467Z","repository":{"id":40341471,"uuid":"120907554","full_name":"vektah/dataloaden","owner":"vektah","description":"go generate based DataLoader","archived":false,"fork":false,"pushed_at":"2024-04-09T10:05:18.000Z","size":59,"stargazers_count":532,"open_issues_count":28,"forks_count":78,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-01T10:17:05.910Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","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/vektah.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2018-02-09T13:14:51.000Z","updated_at":"2025-02-11T21:33:51.000Z","dependencies_parsed_at":"2024-06-18T12:38:50.426Z","dependency_job_id":null,"html_url":"https://github.com/vektah/dataloaden","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vektah%2Fdataloaden","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vektah%2Fdataloaden/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vektah%2Fdataloaden/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vektah%2Fdataloaden/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vektah","download_url":"https://codeload.github.com/vektah/dataloaden/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247838444,"owners_count":21004580,"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":[],"created_at":"2024-08-01T05:00:51.367Z","updated_at":"2025-04-08T12:08:09.441Z","avatar_url":"https://github.com/vektah.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"### The DATALOADer gENerator [![CircleCI](https://circleci.com/gh/Vektah/dataloaden.svg?style=svg)](https://circleci.com/gh/vektah/dataloaden) [![Go Report Card](https://goreportcard.com/badge/github.com/vektah/dataloaden)](https://goreportcard.com/report/github.com/vektah/dataloaden) [![codecov](https://codecov.io/gh/vektah/dataloaden/branch/master/graph/badge.svg)](https://codecov.io/gh/vektah/dataloaden)\n\nRequires golang 1.11+ for modules support.\n\nThis is a tool for generating type safe data loaders for go, inspired by https://github.com/facebook/dataloader.\n\nThe intended use is in graphql servers, to reduce the number of queries being sent to the database. These dataloader\nobjects should be request scoped and short lived. They should be cheap to create in every request even if they dont\nget used.\n\n#### Getting started\n\nFrom inside the package you want to have the dataloader in:\n```bash\ngo run github.com/vektah/dataloaden UserLoader string *github.com/dataloaden/example.User\n```\n\nThis will generate a dataloader called `UserLoader` that looks up `*github.com/dataloaden/example.User`'s objects \nbased on a `string` key. \n\nIn another file in the same package, create the constructor method:\n```go\nfunc NewUserLoader() *UserLoader {\n\treturn \u0026UserLoader{\n\t\twait:     2 * time.Millisecond,\n\t\tmaxBatch: 100,\n\t\tfetch: func(keys []string) ([]*User, []error) {\n\t\t\tusers := make([]*User, len(keys))\n\t\t\terrors := make([]error, len(keys))\n\n\t\t\tfor i, key := range keys {\n\t\t\t\tusers[i] = \u0026User{ID: key, Name: \"user \" + key}\n\t\t\t}\n\t\t\treturn users, errors\n\t\t},\n\t}\n}\n```\n\nThen wherever you want to call the dataloader\n```go\nloader := NewUserLoader()\n\nuser, err := loader.Load(\"123\")\n```\n\nThis method will block for a short amount of time, waiting for any other similar requests to come in, call your fetch\nfunction once. It also caches values and wont request duplicates in a batch.\n\n#### Returning Slices\n\nYou may want to generate a dataloader that returns slices instead of single values. Both key and value types can be a \nsimple go type expression: \n\n```bash\ngo run github.com/vektah/dataloaden UserSliceLoader string []*github.com/dataloaden/example.User\n```\n\nNow each key is expected to return a slice of values and the `fetch` function has the return type `[][]*User`.\n\n#### Using with go modules\n\nCreate a tools.go that looks like this:\n```go\n// +build tools\n\npackage main\n\nimport _ \"github.com/vektah/dataloaden\"\n```\n\nThis will allow go modules to see the dependency.\n\nYou can invoke it from anywhere within your module now using `go run github.com/vektah/dataloaden` and \nalways get the pinned version.\n\n#### Wait, how do I use context with this?\n\nI don't think context makes sense to be passed through a data loader. Consider a few scenarios:\n1. a dataloader shared between requests: request A and B both get batched together, which context should be passed to the DB? context.Background is probably more suitable.\n2. a dataloader per request for graphql: two different nodes in the graph get batched together, they have different context for tracing purposes, which should be passed to the db? neither, you should just use the root request context.\n\n\nSo be explicit about your context:\n```go\nfunc NewLoader(ctx context.Context) *UserLoader {\n\treturn \u0026UserLoader{\n\t\twait:     2 * time.Millisecond,\n\t\tmaxBatch: 100,\n\t\tfetch: func(keys []string) ([]*User, []error) {\n\t\t\t// you now have a ctx to work with\n\t\t},\n\t}\n}\n```\n\nIf you feel like I'm wrong please raise an issue.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvektah%2Fdataloaden","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvektah%2Fdataloaden","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvektah%2Fdataloaden/lists"}