{"id":18342079,"url":"https://github.com/vikstrous/dataloadgen-example","last_synced_at":"2025-09-12T14:32:04.307Z","repository":{"id":192292339,"uuid":"686386569","full_name":"vikstrous/dataloadgen-example","owner":"vikstrous","description":"An example of how to use https://github.com/vikstrous/dataloadgen with gqlgen","archived":false,"fork":false,"pushed_at":"2024-02-03T20:56:08.000Z","size":28,"stargazers_count":5,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-02T03:42:19.419Z","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/vikstrous.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":"2023-09-02T16:08:13.000Z","updated_at":"2024-08-30T09:40:09.000Z","dependencies_parsed_at":"2024-02-03T21:35:45.954Z","dependency_job_id":"1d921cf2-c304-4002-a2e6-627f531f5853","html_url":"https://github.com/vikstrous/dataloadgen-example","commit_stats":null,"previous_names":["vikstrous/dataloadgen-example"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vikstrous%2Fdataloadgen-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vikstrous%2Fdataloadgen-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vikstrous%2Fdataloadgen-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vikstrous%2Fdataloadgen-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vikstrous","download_url":"https://codeload.github.com/vikstrous/dataloadgen-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232747718,"owners_count":18570505,"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-11-05T20:29:27.362Z","updated_at":"2025-01-06T16:13:23.709Z","avatar_url":"https://github.com/vikstrous.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# dataloadgen-example\nThis is an example of how to use [vikstrous/dataloadgen](https://github.com/vikstrous/dataloadgen) with [gqlgen](https://gqlgen.com/) to improve graphql performance by batching and caching requests to the underlying storage system.\n\nThis example was created by following the [official tutorial](https://gqlgen.com/getting-started/). After completing the tutorial, an example in-memory storage package was created and a loader was created for the User object.\n\nThis file listing highlights the important files to look at to understand how loaders can be used with gqlgen.\n```\n.\n├── gqlgen.yml\n├── graph\n│   ├── generated.go\n│   ├── loader\n│   │   └── loader.go - the implementation of loaders\n│   ├── model\n│   │   ├── models_gen.go\n│   │   └── todo.go\n│   ├── resolver.go\n│   ├── schema.graphqls\n│   ├── schema.resolvers.go - the implementation of resolvers that use loaders\n│   └── storage\n│       └── storage.go - the underlying storage system with artificial delays and logging\n├── server.go - the wiring of the stoage system, loaders middleware and resolvers\n└── tools.go\n```\n\nTo use a loader, the recommended pattern is to create a new one for every HTTP request and inject it into the context using a middleware. [server.go](https://github.com/vikstrous/dataloadgen-example/blob/master/server.go) contains the wiring for this. Note the call to `loader.Middleware`. That allows resolvers to access the loader using `loader.Get(ctx)` and then call the methods on the loader objects.\n\n## Try it out\n\nStart the server by running:\n```\ngo run .\n```\n\nGo to http://localhost:8080 and execute the following query to populate the TODOs:\n\n```graphql\nmutation {\n  t1: createTodo(input:{text:\"todo1\",userId:\"alice\"}){\n    id\n    text\n    done\n    user{\n      id\n      name\n    }\n  }\n  t2: createTodo(input:{text:\"todo2\",userId:\"alice\"}){\n    id\n    text\n    done\n    user{\n      id\n      name\n    }\n  }\n  t3: createTodo(input:{text:\"todo3\",userId:\"bob\"}){\n    id\n    text\n    done\n    user{\n      id\n      name\n    }\n  }\n}\n```\nThe response should look like:\n```json\n{\n  \"data\": {\n    \"t1\": {\n      \"id\": \"9014147064985197323\",\n      \"text\": \"todo1\",\n      \"done\": false,\n      \"user\": {\n        \"id\": \"alice\",\n        \"name\": \"Alice\"\n      }\n    },\n    \"t2\": {\n      \"id\": \"763913058984159819\",\n      \"text\": \"todo2\",\n      \"done\": false,\n      \"user\": {\n        \"id\": \"alice\",\n        \"name\": \"Alice\"\n      }\n    },\n    \"t3\": {\n      \"id\": \"5828640345075959780\",\n      \"text\": \"todo3\",\n      \"done\": false,\n      \"user\": {\n        \"id\": \"bob\",\n        \"name\": \"Bob\"\n      }\n    }\n  }\n}\n```\nIt intentionally takes several seconds to execute the mutations. Every access to the storage package is artificially delayed by a second and accesses are logged to stdout. The output in the console after running this query should look like:\n```\nUserStorage.Get\nTodoStorage.Put\nUserStorage.Get\nTodoStorage.Put\nUserStorage.Get\nTodoStorage.Put\n```\n\nNote that there are only three calls to `UserStorage.Get` even though the user is fetched both in the execution of the mutation and later in the query for the user. This is because, in the root resolver, after accessing the user storage, the data loader cache is primed. See [schema.resolvers.go](https://github.com/vikstrous/dataloadgen-example/blob/master/graph/schema.resolvers.go) `mutationResolver.CreateTodo()` for how the cache is primed.\n\nNow make another, read-only query to fetch all the todos along with their associated users.\n\n```graphql\n{\n  todos{\n    id\n    text\n    done\n    user{\n      id\n      name\n    }\n  }\n}\n```\nThe response should look like:\n```json\n{\n  \"data\": {\n    \"todos\": [\n      {\n        \"id\": \"9014147064985197323\",\n        \"text\": \"todo1\",\n        \"done\": false,\n        \"user\": {\n          \"id\": \"alice\",\n          \"name\": \"Alice\"\n        }\n      },\n      {\n        \"id\": \"763913058984159819\",\n        \"text\": \"todo2\",\n        \"done\": false,\n        \"user\": {\n          \"id\": \"alice\",\n          \"name\": \"Alice\"\n        }\n      },\n      {\n        \"id\": \"5828640345075959780\",\n        \"text\": \"todo3\",\n        \"done\": false,\n        \"user\": {\n          \"id\": \"bob\",\n          \"name\": \"Bob\"\n        }\n      }\n    ]\n  }\n}\n```\nThe output in the console should look like:\n```\nTodoStorage.GetAll\nUserStorage.GetMulti 2\n```\n\nThere is no call to `UserStorage.Get` in this case. The use of a loader in [schema.resolvers.go](https://github.com/vikstrous/dataloadgen-example/blob/master/graph/schema.resolvers.go) in `todoResolver.User()` causes concurrent executions to be batched, deduplicated and cached, so only a single call to `GetMulti` is made instead and only with 2 user IDs.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvikstrous%2Fdataloadgen-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvikstrous%2Fdataloadgen-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvikstrous%2Fdataloadgen-example/lists"}