{"action":"created","issue":{"url":"https://api.github.com/repos/graphql-dotnet/graphql-dotnet/issues/1153","repository_url":"https://api.github.com/repos/graphql-dotnet/graphql-dotnet","labels_url":"https://api.github.com/repos/graphql-dotnet/graphql-dotnet/issues/1153/labels{/name}","comments_url":"https://api.github.com/repos/graphql-dotnet/graphql-dotnet/issues/1153/comments","events_url":"https://api.github.com/repos/graphql-dotnet/graphql-dotnet/issues/1153/events","html_url":"https://github.com/graphql-dotnet/graphql-dotnet/issues/1153","id":447441563,"node_id":"MDU6SXNzdWU0NDc0NDE1NjM=","number":1153,"title":"How to load many items with DataLoader?","user":{"login":"acelot","id":1065215,"node_id":"MDQ6VXNlcjEwNjUyMTU=","avatar_url":"https://avatars1.githubusercontent.com/u/1065215?v=4","gravatar_id":"","url":"https://api.github.com/users/acelot","html_url":"https://github.com/acelot","followers_url":"https://api.github.com/users/acelot/followers","following_url":"https://api.github.com/users/acelot/following{/other_user}","gists_url":"https://api.github.com/users/acelot/gists{/gist_id}","starred_url":"https://api.github.com/users/acelot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/acelot/subscriptions","organizations_url":"https://api.github.com/users/acelot/orgs","repos_url":"https://api.github.com/users/acelot/repos","events_url":"https://api.github.com/users/acelot/events{/privacy}","received_events_url":"https://api.github.com/users/acelot/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":8,"created_at":"2019-05-23T04:08:04Z","updated_at":"2019-09-02T19:25:21Z","closed_at":null,"author_association":"NONE","body":"I have the following scheme:\r\n```gql\r\ntype Query {\r\n  articles: [Article!]!\r\n}\r\n\r\ntype Article {\r\n  id: ID!,\r\n  title: String!,\r\n  authors: [Author!]!\r\n}\r\n\r\ntype Author {\r\n  id: ID!,\r\n  name: String!\r\n}\r\n```\r\n\r\nTypical N+1 problem when you need to load authors for each article. Authors are stored in `articles` table as an array of integers in `authors` column in PostgreSQL.\r\n\r\nArticles table example:\r\n\r\n|id|title|authors|\r\n|--|----|---------|\r\n|`1`|Article 1|`[1,2,3]`|\r\n|`2`|Article 2|`[1,2]`|\r\n|`3`|Article 3|`[3,1]`|\r\n\r\nWhat I need to get from graphql:\r\n\r\n```json\r\n{\r\n  \"data\": {\r\n    \"articles\": [\r\n      {\r\n        \"id\": 1,\r\n        \"title\": \"Article 1\",\r\n        \"authors\": [\r\n          {\r\n            \"id\": 1,\r\n            \"name\": \"John Snow\"\r\n          },\r\n          {\r\n            \"id\": 2,\r\n            \"name\": \"Daenerys Targaryen\"\r\n          },\r\n          {\r\n            \"id\": 3,\r\n            \"name\": \"Tyrion Lannister\"\r\n          }\r\n        ]\r\n      },\r\n      {\r\n        \"id\": 2,\r\n        \"title\": \"Article 2\",\r\n        \"authors\": [\r\n          {\r\n            \"id\": 1,\r\n            \"name\": \"John Snow\"\r\n          },\r\n          {\r\n            \"id\": 2,\r\n            \"name\": \"Daenerys Targaryen\"\r\n          }\r\n        ]\r\n      },\r\n      {\r\n        \"id\": 3,\r\n        \"title\": \"Article 3\",\r\n        \"authors\": [\r\n          {\r\n            \"id\": 3,\r\n            \"name\": \"Tyrion Lannister\"\r\n          },\r\n          {\r\n            \"id\": 1,\r\n            \"name\": \"John Snow\"\r\n          }\r\n        ]\r\n      }\r\n    ]\r\n  }\r\n}\r\n```\r\n\r\nIt is expected that a single request must be executed to get all the authors.\r\nWhat I am trying to do:\r\n```cs\r\npublic class ArticleType : ObjectGraphType<Article>\r\n{\r\n    private NpgsqlConnection conn;\r\n\r\n    public ArticleType(IDataLoaderContextAccessor accessor)\r\n    {\r\n        Field(a => a.Id).Name(\"id\");\r\n\r\n        Field(a => a.Title).Name(\"title\");\r\n\r\n        Field<ListGraphType<AuthorType>>().Name(\"authors\")\r\n            .ResolveAsync(async (ctx) =>\r\n            {\r\n                var authorsLoader = accessor.Context.GetOrAddCollectionBatchLoader<int, Author>(\"getAuthorsByIds\", GetAuthorsByIds);\r\n                int[] authorsIds = ctx.Source.AuthorsIds;\r\n                var authors = await authorsLoader.LoadAsync(authorsIds);\r\n                ...\r\n            });\r\n    }\r\n\r\n    public Task<ILookup<int, Author>> GetAuthorsByIds(IEnumerable<int> ids)\r\n    {\r\n        ...\r\n    }\r\n}\r\n```\r\n\r\nBut unfortunately the `LoadAsync()` method takes only single key as parameter. How to load many keys simultaneously? Or should I get them one by one with awaits?"},"comment":{"url":"https://api.github.com/repos/graphql-dotnet/graphql-dotnet/issues/comments/527228778","html_url":"https://github.com/graphql-dotnet/graphql-dotnet/issues/1153#issuecomment-527228778","issue_url":"https://api.github.com/repos/graphql-dotnet/graphql-dotnet/issues/1153","id":527228778,"node_id":"MDEyOklzc3VlQ29tbWVudDUyNzIyODc3OA==","user":{"login":"benmccallum","id":157336,"node_id":"MDQ6VXNlcjE1NzMzNg==","avatar_url":"https://avatars0.githubusercontent.com/u/157336?v=4","gravatar_id":"","url":"https://api.github.com/users/benmccallum","html_url":"https://github.com/benmccallum","followers_url":"https://api.github.com/users/benmccallum/followers","following_url":"https://api.github.com/users/benmccallum/following{/other_user}","gists_url":"https://api.github.com/users/benmccallum/gists{/gist_id}","starred_url":"https://api.github.com/users/benmccallum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benmccallum/subscriptions","organizations_url":"https://api.github.com/users/benmccallum/orgs","repos_url":"https://api.github.com/users/benmccallum/repos","events_url":"https://api.github.com/users/benmccallum/events{/privacy}","received_events_url":"https://api.github.com/users/benmccallum/received_events","type":"User","site_admin":false},"created_at":"2019-09-02T19:25:21Z","updated_at":"2019-09-02T19:25:21Z","author_association":"CONTRIBUTOR","body":"> No. Because it is not a logic inside dataloader but around it existing method.\r\nWhat ever did people did before extension methods... 😅\r\n\r\nI meant that adding it to the interface was necessary in my current implementation as an instance method. Obviously my above snippet is against the interface and will be fine when it's an extension method."}}
{"actor":{"display_login":"benmccallum"}}