{"id":16485279,"url":"https://github.com/pa-bru/graphql-server-demo","last_synced_at":"2026-04-24T20:35:20.283Z","repository":{"id":91667089,"uuid":"121386662","full_name":"pa-bru/graphql-server-demo","owner":"pa-bru","description":"A simple GraphQL server demo step by step","archived":false,"fork":false,"pushed_at":"2018-09-26T19:45:39.000Z","size":13,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-17T03:06:58.216Z","etag":null,"topics":["apollo-server-express","dataloader","demo","graphql","graphql-server"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/pa-bru.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":"2018-02-13T13:38:37.000Z","updated_at":"2018-07-12T02:47:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"3ec42894-05fa-47f1-b04c-2fb20d194a48","html_url":"https://github.com/pa-bru/graphql-server-demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pa-bru/graphql-server-demo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pa-bru%2Fgraphql-server-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pa-bru%2Fgraphql-server-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pa-bru%2Fgraphql-server-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pa-bru%2Fgraphql-server-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pa-bru","download_url":"https://codeload.github.com/pa-bru/graphql-server-demo/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pa-bru%2Fgraphql-server-demo/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32240027,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-24T13:21:15.438Z","status":"ssl_error","status_checked_at":"2026-04-24T13:21:15.005Z","response_time":64,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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-express","dataloader","demo","graphql","graphql-server"],"created_at":"2024-10-11T13:25:09.125Z","updated_at":"2026-04-24T20:35:20.268Z","avatar_url":"https://github.com/pa-bru.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GraphQL Server\n\n\u003e This project is used as a demo to introduce GraphQL and how to use it on the server side.\n\nTo see the progression checkout to the following branches.\n\n## 01-introducing-graphql-js\n\nUse of the `graphql()` \u0026 `buildSchema()` functions of [GraphQL.js].\nThe GraphQL query is defined statically in the server.\n\n## 02-expose-the-graphql-endpoint\n\nUse of [Express.js] to expose the GraphQL API on a `/graphql` endpoint.\nThis time we pass the GraphQL query with the body request.\n\n## 03-graphql-http-server-middleware\n\nWe stopped using directly the `graphql()` function but started using the [apollo-server-express] middleware which execute the `graphql()` function for us but manage other things like HTTP issues, [GraphiQL], GraphQL validation rules, extensions, etc.\n\nGraphiQL is an in-browser IDE to test GraphQL queries, see what is available in the schema, autocomplete queries...\n\nWe also added a fake users list to simulate a user API.\n\n## 04-setup-a-modular-architecture\n\nIn this branch we decided to dispatch our GraphQL server into several modules. The purpose is to have a maintainable architecture and to make it easy to work with several developers on the same project.\nNow there is a `/modules` folder where we can put some files or folders in which each module has to return an object containing `{schema, resolvers}`. Then theses modules will be merged by `makeExecutableSchema()`.\n\nTo achieve that we used the [graphql-tools] package of Apollo. It's a utility package which enable the generation of a schema from several substrings.\n\n## 05-aggregate-an-api\n\nNow that we have a modular architecture, I replace the `fakeDatabase` object written statically in `src/modules/user.js` by a fake API thanks to [json-server]. So I defined a `db.json` file in the root directory and then added some commands in `package.json` to serve data from `db.json` in API exposed at http://localhost:5000/users.\n\nI used [npm-run-all] to serve the GraphQL API and the user API in parallel.\n\nThen, I added a mutation in the GraphQL server to enable adding new users to the user API.\nA mutation is one the the 3 GraphQL operations (Query, Mutation, Subscription):\n\n* Query: use it to perform read operations (like a GET in REST APIs). If no operation Type is specified in the client request, the Query operation is the default one.\n\n* Mutation: any operations that performs writes should be sent explicitly via a mutation.(like a POST/PUT/PATCH in REST APIs)\n\n* Subscription: used to subscribe to event based updates (web-socket). \u003cbr\u003eE.g: When someone likes your post on Facebook, you get a notification.\n\n## 06-optimizing-queries-with-dataloader\n\nI you run the GraphQL server and try a query like this...\n\n```graphql\n{\n  users {\n    id\n    username\n    friends {\n      id\n      username\n    }\n  }\n}\n```\n\nYou can see that it works. You retrieve users and users's friends indeed.\n\nHowever if you look at your terminal console you can figure out that you call same API endpoints several times.\nThis is because some users have friends that are users you already get in the query.\n\nE.g: user #2 and #1 are each called 2 times\n\n```shell\nGET /users/ 200 0.585 ms - 390\nGET /users/2 200 2.227 ms - 104\nGET /users/3 200 1.163 ms - 111\nGET /users/1 200 1.161 ms - 109\nGET /users/1 200 1.185 ms - 109\nGET /users/2 200 1.216 ms - 104\n```\n\nWe can't have a production ready GraphQL server if we don't optimize GraphQL queries. So we need to batch API calls.\n\n[Dataloader] is a Facebook package which can achieve that.\n\nTo make the batching work you have to instanciate a new Dataloader and pass to it a function to resolve your API calls by keys. Then instead of calling directly the user API in your resolver functions you need to call the `load(key)` function of your dataloader instance.\n\nE.g\n\n```js\nimport Dataloader from 'dataloader'\n\nconst userLoader = new Dataloader(ids =\u003e Promise.all(ids.map(getById)))\n\nconst user = userLoader.load(1) // returns user #1\n```\n\nThen if you run the query again, each API endpoint will be called juste once\n\n```shell\nGET /users/ 200 1.353 ms - 390\nGET /users/2 200 1.612 ms - 104\nGET /users/3 200 0.923 ms - 111\nGET /users/1 200 0.948 ms - 109\n```\n\n[graphql.js]: https://github.com/graphql/graphql-js\n[express.js]: http://expressjs.com/\n[apollo-server-express]: https://github.com/apollographql/apollo-server\n[graphiql]: https://github.com/graphql/graphiql\n[graphql-tools]: https://github.com/apollographql/graphql-tools\n[json-server]: https://github.com/typicode/json-server\n[npm-run-all]: https://www.npmjs.com/package/npm-run-all\n[dataloader]: https://github.com/facebook/dataloader\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpa-bru%2Fgraphql-server-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpa-bru%2Fgraphql-server-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpa-bru%2Fgraphql-server-demo/lists"}