{"id":14976158,"url":"https://github.com/figozhu/easy-graphql","last_synced_at":"2025-10-27T18:30:43.361Z","repository":{"id":57218456,"uuid":"108370742","full_name":"figozhu/easy-graphql","owner":"figozhu","description":null,"archived":false,"fork":false,"pushed_at":"2017-10-30T11:04:37.000Z","size":30,"stargazers_count":101,"open_issues_count":0,"forks_count":1,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-09-28T21:41:17.362Z","etag":null,"topics":["express-graphql","graphql","graphql-query"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/figozhu.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}},"created_at":"2017-10-26T06:28:46.000Z","updated_at":"2023-08-13T08:51:17.000Z","dependencies_parsed_at":"2022-08-28T21:41:05.656Z","dependency_job_id":null,"html_url":"https://github.com/figozhu/easy-graphql","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/figozhu%2Feasy-graphql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/figozhu%2Feasy-graphql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/figozhu%2Feasy-graphql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/figozhu%2Feasy-graphql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/figozhu","download_url":"https://codeload.github.com/figozhu/easy-graphql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219860762,"owners_count":16556011,"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":["express-graphql","graphql","graphql-query"],"created_at":"2024-09-24T13:53:24.452Z","updated_at":"2025-10-27T18:30:38.083Z","avatar_url":"https://github.com/figozhu.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# easy-graphql\n\n[![npm version](https://badge.fury.io/js/easy-graphql.svg)](https://badge.fury.io/js/easy-graphql)\n\nEasy way for using GraphQL.\n\nStar is welcome.\n\n## Using easy-graphql\n\nInstall easy-graphql from npm\n\nWith yarn:\n\n```sh\nyarn add easy-graphql\n```\n\nor alternatively using npm:\n\n```sh\nnpm install --save easy-graphql\n```\n\n### Step 1\nChoose a base directory\n\n### Step 2\nCreate schemas directory\n1. Create a sub directory named `schemas` under the base directory\n2. Each entity must create a file named `xxx_schema.graphqls` under the above sub directory\n\n### Step 3\nDefine your query\nAll entities' query must create a file named `query.graphqls` under the base directory\n\n### Step 4\nCreate resolvers directory named `resolvers` under the base directory\nEach entity's resolver must create a file named `xxx_resolvers.js` under the above sub directory\n\n## API\n\n- Init\n\nnew an easy-graphql object:\n\n```javascript\nconst path = require('path');\n\nconst easyGraphqlModule = require('easy-graphql');\n\nconst basePath = path.join(__dirname, 'graphql');\nconst easyGraphqlObj = new easyGraphqlModule(basePath);\n```\n\n- Get the GraphQL Schema Object:\n\n```getSchema()```\n\nUsing with `express-graphql` middleware:\n\n```javascript\nconst express = require('express');\nconst graphqlHTTP = require('express-graphql');\n\nconst allSchema = easyGraphqlObj.getSchema();\n\n// using with express-graphql middleware\napp.use('/graphql', graphqlHTTP({\n    schema : allSchema,\n    graphiql : true,\n}));\n```\n\n- Execute the GraphQL query\n\n```javascript\n/**\n * do the GraphQL query execute\n * @param {*} requestObj -  GraphQL query object {query: \"...\"}\n * @param {*} context - [optional] query context\n * @returns {Promise} - GraphQL execute promise \n */\nqueryGraphQLAsync(requestObj, {context})\n```\n\nFor example, in your system, the response format is:\n\n```\n{\n    \"code\" : 0,\n    \"reason\" : \"success\",\n    \"data\" : {...}\n}\n```\n\nSo you cannot use `express-graphql` middleware, then you need an function can do the GraphQL query.\n\n```javascript\nconst bodyParser = require('body-parser');\napp.use(bodyParser.json()); // for parsing application/json\napp.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded\n\napp.post('/restful', async (req, res) =\u003e {\n    let queryObj = req.body;\n    \n    let result;\n    try {\n        // using with your restful service\n        result = await easyGraphqlObj.queryGraphQL(queryObj, {context: req});\n    } catch (err) {\n        console.error(err);\n        res.json({code : -1, reason : \"GraphQL error\"});\n        return;\n    }\n    \n    res.json({\n        code : 0,\n        reason : \"success\",\n        data : result.data,\n    });\n});\n```\n\n## Example\n\nAn example for using easy-graphql with Express in `test` directory(`express-test.js`):\n\n```\ntest\n├── express-test.js\n├── fakeDB.js\n└── graphql\n    ├── query.graphqls\n    ├── resolvers\n    │   ├── post_resolver.js\n    │   └── user_resolver.js\n    └── schemas\n        ├── post_schema.graphqls\n        └── user_schema.graphqls\n```\n\nFor example, we have some simple blog system with two entities (`post` and `user`)\n\n`user`'s data:\n\n| uid | name | avatar |\n| :-- | :-- | :-- |\n| 1 | Tom | https://pre00.deviantart.net/2930/th/pre/i/2014/182/a/2/tom_cat_by_1997ael-d7ougoa.png |\n| 2 | Jerry | https://vignette.wikia.nocookie.net/tomandjerry/images/2/29/Jerry_2.png |\n\n`post`'s data:\n\n| pid | title | content | authorId |\n| --- | --- | --- | --- |\n| 1 | foo | xxx | 1 |\n| 2 | bar | yyy | 2 |\n\n### Step 1\n\nCreate the base directory `graphql`\n\n### Step 2\n\nCreate two sub directory `user` and `post`, then define the entities' schema by create two schema file:\n\n- `schemas/user_schema.graphqls` \n\n```\n# user schema\ntype User {\n    uid : ID!\n    name : String!\n    avatar : String!\n}\n```\n\n- `schemas/post_schema.graphqls`\n\n```\n# post schema\ntype Post {\n    pid : ID!\n    title : String!\n    content : String!\n    auhtor : User!\n}\n```\n\n\n### Step 3\n\nDefine the Query, create the `query.graphqls` under the base directory (`graphql`)\n\n```\ntype Query {\n    user(id: ID): User\n\n    post(id: ID): Post\n}\n```\n\n### Step 4\n\nImplement two resolvers:\n\n- `resolvers/user_resolver.js`\n\n```javascript\n'use strict'\n\nconst fakeDB = require('../../fakeDB');\n\nfunction fetchUserById (root, {id}, ctx) {\n    let uid = parseInt(id);\n    return fakeDB.getUserById(uid);\n}\n\nconst userReolvers = {\n    Query : {\n        user : fetchUserById,\n    },\n    \n};\nmodule.exports = userReolvers;\n```\n\n- `resolvers/post_resolver.js`\n\n```javascript\n'use strict'\n\nconst fakeDB = require('../../fakeDB');\n\nfunction fetchPostById (root, {id}, ctx) {\n    let pid = parseInt(id);\n    return fakeDB.getPostById(pid);\n}\n\nfunction fetchUserByAuthorId (root, args, ctx) {\n    let uid = root.authorId;\n    return fakeDB.getUserById(uid);\n}\n\nconst postReolvers = {\n    Query : {\n        post : fetchPostById,\n    },\n\n    Post : {\n        auhtor : fetchUserByAuthorId,\n    },\n};\nmodule.exports = postReolvers;\n```\n\n### Done\n\nNow we can run the example under `test` directory:\n\n```sh\n# under test directory\nnode express-test.js\nopen http://127.0.0.1:8000/graphql in your browser\n```\n\nThen open the above url in your browser, and test with GraphQL query:\n\n```\nquery {\n  user(id:1) {\n    uid\n    name\n    avatar\n  }\n  \n  post(id:2) {\n    pid\n    title\n    content\n    auhtor {\n      uid\n      name\n      avatar\n    }\n  }\n}\n```\n\nThe result is:\n\n```\n{\n  \"data\": {\n    \"user\": {\n      \"uid\": \"1\",\n      \"name\": \"Tom\",\n      \"avatar\": \"https://pre00.deviantart.net/2930/th/pre/i/2014/182/a/2/tom_cat_by_1997ael-d7ougoa.png\"\n    },\n    \"post\": {\n      \"pid\": \"2\",\n      \"title\": \"bar\",\n      \"content\": \"yyy\",\n      \"auhtor\": {\n        \"uid\": \"2\",\n        \"name\": \"Jerry\",\n        \"avatar\": \"https://vignette.wikia.nocookie.net/tomandjerry/images/2/29/Jerry_2.png\"\n      }\n    }\n  }\n}\n```\n\nAnd also you can test the restful service by following curl command:\n\n```sh\ncurl -i -H \"Content-Type: application/json\" -X POST -d '{\"query\":\"\\nquery {\\n    user(id:1) {\\n      uid\\n      name\\n      avatar\\n    }\\n    \\n    post(id:2) {\\n      pid\\n      title\\n      content\\n      auhtor {\\n        uid\\n        name\\n        avatar\\n      }\\n    }\\n  }\\n\",\"variables\":null}' http://127.0.0.1:8000/restful\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffigozhu%2Feasy-graphql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffigozhu%2Feasy-graphql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffigozhu%2Feasy-graphql/lists"}