{"id":442,"date":"2019-12-20T15:52:35","date_gmt":"2019-12-20T09:52:35","guid":{"rendered":"https:\/\/open.zeba.academy\/?p=442"},"modified":"2019-12-20T15:52:35","modified_gmt":"2019-12-20T09:52:35","slug":"api-graphql-koa-mongodb","status":"publish","type":"post","link":"https:\/\/code.zeba.academy\/ru\/api-graphql-koa-mongodb\/","title":{"rendered":"\u041a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043c\u043e\u0449\u043d\u044b\u0439 API \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e GraphQL, Koa \u0438 MongoDB"},"content":{"rendered":"<p>\u0421\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c API \u0432\u0435\u0441\u0435\u043b\u043e! \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a Koa, GraphQL \u0438 MongoDB.<\/p>\n<p>Koa \u2013 \u044d\u0442\u043e \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a Node, \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0438 Express \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u043e\u043c Node. \u041c\u044b \u0437\u0430\u043c\u0435\u043d\u0438\u043c Express \u043d\u0430 Koa, \u0442\u0430\u043a \u043a\u0430\u043a Koa \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 async\/await \u0432\u043c\u0435\u0441\u0442\u043e \u201c\u043a\u043e\u043b\u043b\u0431\u044d\u043a\u043e\u0432\u201d.<\/p>\n<p>\u0417\u0430\u043c\u0435\u0442\u043a\u0430; \u0415\u0441\u043b\u0438 \u0432\u044b \u043d\u043e\u0432\u0438\u0447\u043e\u043a \u0432 API, \u044f \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u044d\u0442\u0443 <a href=\"https:\/\/www.amazon.com\/gp\/product\/1786469138\/ref=as_li_qf_asin_il_tl?ie=UTF8&amp;tag=thegeniusdev-20&amp;creative=9325&amp;linkCode=as2&amp;creativeASIN=1786469138&amp;linkId=a2882ca05f850981337873158552abe0\">\u043a\u043d\u0438\u0433\u0443<\/a>, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0447\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443.<\/p>\n<h3>\u041d\u0430\u0447\u0438\u043d\u0430\u0435\u043c<\/h3>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u0430\u0448\u0435\u0433\u043e API:<\/p>\n<ul>\n<li>Node \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d<\/li>\n<li>\u0422\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440 (\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u043a\u043e\u0434 Visual Studio)<\/li>\n<li>\u0422\u0435\u0440\u043c\u0438\u043d\u0430\u043b<\/li>\n<li>\u0411\u0440\u0430\u0443\u0437\u0435\u0440<\/li>\n<\/ul>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u0441\u0435, \u0447\u0442\u043e \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0439\u0442\u0435 \u2013 \u0435\u0441\u043b\u0438 \u043d\u0435\u0442, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u0438\u0445.<\/p>\n<p>\u041e\u0442\u043a\u0440\u043e\u0439\u0442\u0435 \u0441\u0432\u043e\u0439 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b \u0438 \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043f\u0440\u043e\u0435\u043a\u0442 node, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0430\u043a\u043e\u0439:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-444\" src=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/node-project-terminal.png\" alt=\"node project terminal\" width=\"1286\" height=\"1464\" srcset=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/node-project-terminal.png 1286w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/node-project-terminal-264x300.png 264w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/node-project-terminal-899x1024.png 899w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/node-project-terminal-768x874.png 768w\" sizes=\"auto, (max-width: 1286px) 100vw, 1286px\" \/><\/p>\n<p>\u0418\u0442\u0430\u043a, \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u043f\u0430\u043f\u043a\u0443 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 Node. \u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u043f\u0430\u043a\u0435\u0442\u044b NPM, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 Koa, Mongo \u0438 GraphQL.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c \u043a\u043e\u0430 \u0441 NPM.<\/p>\n<pre>npm i koa<\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u0435\u0440\u0432\u0435\u0440 Koa \u043f\u0440\u043e\u0441\u0442\u043e. \u0412\u0441\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u2013 \u044d\u0442\u043e \u0444\u0430\u0439\u043b server.js \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u044b\u043c:<\/p>\n<pre>const Koa = require('koa');\n\nconst app = new Koa();\n\napp.listen(9000);\n\napp.on('error', err =&gt; {\n  log.error('server error', err)\n});<\/pre>\n<p>\u041d\u0430\u0447\u0430\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442 \u0441 Node:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-445\" src=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/begin-node-project.png\" alt=\"begin node project\" width=\"1358\" height=\"70\" srcset=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/begin-node-project.png 1358w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/begin-node-project-300x15.png 300w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/begin-node-project-1024x53.png 1024w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/begin-node-project-768x40.png 768w\" sizes=\"auto, (max-width: 1358px) 100vw, 1358px\" \/><\/p>\n<h2>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 GraphQL<\/h2>\n<p>\u041d\u0430\u043c \u043d\u0443\u0436\u043d\u044b \u0434\u0432\u0430 \u043f\u0430\u043a\u0435\u0442\u0430 \u0434\u043b\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 GraphQL \u0441 Koa: koa-mount \u0438 koa-graphql<\/p>\n<pre>npm i koa-mount koa-graphql<\/pre>\n<p>GraphQL \u0442\u0440\u0435\u0431\u0443\u0435\u0442, \u0447\u0442\u043e\u0431\u044b \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u043d\u0430\u0448\u0443 \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0441\u0445\u0435\u043c\u0443 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 GraphQL. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0434\u0438\u043d.<\/p>\n<p>\u041c\u044b \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u043c \u0441\u0445\u0435\u043c\u0443 graphQL \u0432 graphql\/schema.js<\/p>\n<pre>const { buildSchema } = require('graphql');\n\nconst schema = buildSchema(`\n  type Query {\n    hello: String\n  }\n`);\n\nmodule.exports = schema;<\/pre>\n<p>\u041c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u043d\u0430\u0448 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 Query \u0444\u0443\u043d\u043a\u0446\u0438\u0438 buildSchema.<\/p>\n<pre>app.use(mount('\/graphql', graphqlHTTP({\n  schema: schema,\n  graphiql: true\n})))<\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435: \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f buildSchema \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438\u0442\u0435\u0440\u0430\u043b\u043e\u043c \u0448\u0430\u0431\u043b\u043e\u043d\u0430.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0441\u0445\u0435\u043c\u0443 \u043d\u0430 \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0435\u0440 GraphQL:<\/p>\n<pre>const mount = require('koa-mount');\nconst graphqlHTTP = require('koa-graphql');\nconst schema = require('.\/graphql\/schema')<\/pre>\n<p>\u041d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c koa-mount, koa-graphql \u0438, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, schema.js.<\/p>\n<p>\u0418 \u0437\u0430\u0442\u0435\u043c, \u0435\u0441\u043b\u0438 \u043c\u044b \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a localhost:9000\/graphql:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-446\" src=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/localhost-page.png\" alt=\"localhost page\" width=\"2872\" height=\"1408\" \/><\/p>\n<p>\u0412\u0443\u0430\u043b\u044f! \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430. \u041f\u043e\u043a\u0430 \u043e\u043d\u0430 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u2013 \u0432 \u0438\u0434\u0435\u0430\u043b\u0435, \u043c\u044b \u0445\u043e\u0442\u0435\u043b\u0438 \u0431\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0442\u044c GraphQL, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043d\u0430\u0448\u0435\u0439 MongoDB \u0438 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0438\u0445 \u043e\u0442\u0442\u0443\u0434\u0430.<\/p>\n<h3>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 MongoDB<\/h3>\n<p>\u0427\u0442\u043e\u0431\u044b \u0447\u0438\u0442\u0430\u0442\u044c \u0438 \u043f\u0438\u0441\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e GraphQL, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043c\u0435\u0441\u0442\u043e \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f. \u0417\u0434\u0435\u0441\u044c Mongo \u043a\u0430\u043a \u0440\u0430\u0437 \u0438 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0438 \u0447\u0438\u0442\u0430\u0442\u044c \u043d\u0430\u0448\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442\u0442\u0443\u0434\u0430.<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u043b\u0430\u0447\u043d\u044b\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u0434\u043b\u044f Mongo. \u0417\u0430\u0439\u0434\u0438\u0442\u0435 \u043d\u0430 mlab.com \u0438 \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u043c\u043e\u043d\u0433\u043e.<\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 MongoDB<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-447\" src=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/creating-MongoDB-database.png\" alt=\"creating MongoDB database\" width=\"2880\" height=\"1076\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-448\" src=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/creating-koa-graphql-database.png\" alt=\"creating koa graphql database\" width=\"2872\" height=\"122\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-449\" src=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/mongo-shell-connect.png\" alt=\"mongo shell connect\" width=\"2294\" height=\"672\" srcset=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/mongo-shell-connect.png 2294w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/mongo-shell-connect-300x88.png 300w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/mongo-shell-connect-1024x300.png 1024w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/mongo-shell-connect-768x225.png 768w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/mongo-shell-connect-1536x450.png 1536w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/mongo-shell-connect-2048x600.png 2048w\" sizes=\"auto, (max-width: 2294px) 100vw, 2294px\" \/><\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0434\u043b\u044f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f MongoDB<\/h4>\n<p>\u041d\u0430\u0436\u043c\u0438\u0442\u0435 \u043d\u0430 \u0432\u043a\u043b\u0430\u0434\u043a\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u043e\u0435 \u0438\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0441 \u043f\u0430\u0440\u043e\u043b\u0435\u043c.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-456\" src=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/add-new-database-user.png\" alt=\"add-new-database-user\" width=\"2170\" height=\"1496\" srcset=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/add-new-database-user.png 2170w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/add-new-database-user-300x207.png 300w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/add-new-database-user-1024x706.png 1024w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/add-new-database-user-768x529.png 768w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/add-new-database-user-1536x1059.png 1536w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/add-new-database-user-2048x1412.png 2048w\" sizes=\"auto, (max-width: 2170px) 100vw, 2170px\" \/><\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c mongoDB \u0432 \u043f\u0430\u0440\u0435 \u0441 Mongoose. \u0423\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0439 URL \u0434\u043b\u044f \u0432\u0430\u0448\u0435\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre>MongoDB: \/\/: @ ds213615.mlab.com: 13615 \/ \u041a\u043e\u0410-graphql<\/pre>\n<h5>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 mongoose<\/h5>\n<pre>npm i mongoose<\/pre>\n<h5>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 database.js<\/h5>\n<p>\u041c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre>const mongoose = require('mongoose');\n\nconst initDB = () =&gt; {\n\n  mongoose.connect(\n    'mongodb:\/\/indrek:graphql1@ds213615.mlab.com:13615\/koa-graphql',\n    { useNewUrlParser: true }\n  );\n\n  mongoose.connection.once('open', () =&gt; {\n    console.log('connected to database');\n  });\n\n}\n\nmodule.exports = initDB;<\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435. \u0423\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u0438\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u0443\u0447\u0435\u0442\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0441\u0432\u043e\u0435\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u042d\u0442\u043e\u0442 \u0431\u043b\u043e\u043a \u043a\u043e\u0434\u0430 \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c\u0441\u044f \u043a \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u043c\u0443 MongoDB..<\/p>\n<p>\u041e\u0442\u043a\u0440\u043e\u0439\u0442\u0435 server.js \u0438 require \u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0442\u0435 \u043c\u0435\u0442\u043e\u0434 initDB.<\/p>\n<pre>const initDB = require('.\/database');\n\ninitDB();<\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u0432\u0441\u0435 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e, \u043d\u0430\u0448\u0430 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u0434\u043e\u043b\u0436\u043d\u0430 \u0441\u043e\u043e\u0431\u0449\u0438\u0442\u044c \u043d\u0430\u043c, \u0447\u0442\u043e \u043c\u044b \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043b\u0438\u0441\u044c \u0443\u0441\u043f\u0435\u0448\u043d\u043e.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-450\" src=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/node-connected-to-database.png\" alt=\"node connected to database\" width=\"1688\" height=\"190\" srcset=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/node-connected-to-database.png 1688w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/node-connected-to-database-300x34.png 300w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/node-connected-to-database-1024x115.png 1024w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/node-connected-to-database-768x86.png 768w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/node-connected-to-database-1536x173.png 1536w\" sizes=\"auto, (max-width: 1688px) 100vw, 1688px\" \/><\/p>\n<p>\u0411\u0440\u0430\u0432\u043e!<\/p>\n<p>\u0417\u0430\u043c\u0435\u0442\u044c\u0442\u0435, \u043a\u0430\u043a \u0434\u043e\u0441\u0430\u0434\u043d\u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440? \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0435\u0448\u0438\u043c \u044d\u0442\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0430\u043a\u0435\u0442\u0430 pm2.<\/p>\n<p>PM2 \u2013 \u044d\u0442\u043e \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0434\u043b\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 Node.js \u0441\u043e \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u043c \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u043e\u043c \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u0430\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438, \u043f\u0435\u0440\u0435\u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0438\u0445 \u0431\u0435\u0437 \u043f\u0440\u043e\u0441\u0442\u043e\u0435\u0432 \u0438 \u043e\u0431\u043b\u0435\u0433\u0447\u0430\u0442\u044c \u043e\u0431\u0449\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0430\u0434\u043c\u0438\u043d\u0438\u0441\u0442\u0440\u0430\u0442\u043e\u0440\u0430.<\/p>\n<pre>npm install pm2 -g<\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0441\u043a\u0440\u0438\u043f\u0442 \u0441 \u0438\u043c\u0435\u043d\u0435\u043c start \u0432 \u043d\u0430\u0448 package.json:<\/p>\n<pre>\"scripts\": {\n  \"start\": \"pm2 start server.js\"\n},<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-451\" src=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/api-yarn-start.png\" alt=\"api yarn start\" width=\"2174\" height=\"1396\" srcset=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/api-yarn-start.png 2174w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/api-yarn-start-300x193.png 300w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/api-yarn-start-1024x658.png 1024w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/api-yarn-start-768x493.png 768w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/api-yarn-start-1536x986.png 1536w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/api-yarn-start-2048x1315.png 2048w\" sizes=\"auto, (max-width: 2174px) 100vw, 2174px\" \/><\/p>\n<p>Pm2 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u0444\u043e\u043d\u043e\u0432\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435, \u0447\u0442\u043e \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u0442 \u043d\u0430\u0448 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b. \u0415\u0441\u043b\u0438 \u0432\u044b \u043a\u043e\u0433\u0434\u0430-\u043d\u0438\u0431\u0443\u0434\u044c \u0437\u0430\u0445\u043e\u0442\u0438\u0442\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 pm2 kill. \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u043f\u0435\u0440\u0435\u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0435\u0440, pm2 \u0434\u0435\u043b\u0430\u0435\u0442 \u044d\u0442\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435: pm2 logs \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0436\u0443\u0440\u043d\u0430\u043b\u0430 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u043d\u0430 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b.<\/p>\n<h3>\u041c\u043e\u0434\u0435\u043b\u0438 MongoDB<\/h3>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043a\u043e\u0433\u0434\u0430-\u043b\u0438\u0431\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438 \u0441 Mongo, \u0432\u044b \u0437\u043d\u0430\u0435\u0442\u0435, \u0447\u0442\u043e MongoDB \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u043b\u044f \u043d\u0430\u0448\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u042d\u0442\u043e \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0448\u0438 \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043f\u0430\u043f\u043a\u0443 \u0434\u043b\u044f \u043c\u043e\u0434\u0435\u043b\u0435\u0439 MongoDB \u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0430\u0439\u043b\u0430 gadgets.js:<\/p>\n<pre>const mongoose = require('mongoose');\nconst Schema = mongoose.Schema;\n\n\/*\n  notice there is no ID. That's because Mongoose will assign\n  an ID by default to all schemas\n*\/\n\nconst GadgetSchema = new Schema({\n  name: String,\n  release_date: Date,\n  by_company: String,\n  price: Number,\n});\n\nmodule.exports = mongoose.model('Gadget', GadgetSchema);<\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435. \u0412 \u043d\u0430\u0448\u0435\u0439 \u0441\u0445\u0435\u043c\u0435 \u043d\u0435\u0442 \u043f\u043e\u043b\u044f \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430. \u042d\u0442\u043e \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e Mongoose \u043d\u0430\u0437\u043d\u0430\u0447\u0438\u0442 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0441\u0445\u0435\u043c.<\/p>\n<p>\u041e\u0442\u043b\u0438\u0447\u043d\u043e, \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0444\u0438\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0434\u043e\u043b\u0436\u043d\u043e \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c \u0438\u043c\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0433\u0430\u0434\u0436\u0435\u0442\u0430 \u0432\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c \u0447\u0438\u0441\u043b\u0435, \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 gadgets.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-452\" src=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/gadgets-collection-name.png\" alt=\"gadgets-collection-name\" width=\"2872\" height=\"538\" \/><\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0432\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 JSON \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-453\" src=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/create-document-json.png\" alt=\"create-document-json\" width=\"1562\" height=\"866\" srcset=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/create-document-json.png 1562w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/create-document-json-300x166.png 300w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/create-document-json-1024x568.png 1024w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/create-document-json-768x426.png 768w, https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/create-document-json-1536x852.png 1536w\" sizes=\"auto, (max-width: 1562px) 100vw, 1562px\" \/><\/p>\n<p>\u042d\u0442\u043e \u0432\u0441\u0435 \u0434\u043b\u044f Mongo, \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e GraphQL.<\/p>\n<h3>\u0417\u0430\u043f\u0440\u043e\u0441\u044b GraphQL<\/h3>\n<p>GraphQL \u0442\u0430\u043a\u0436\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043e\u0442 \u043d\u0430\u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u0438\u043f\u043e\u0432 (\u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439 \u0434\u043b\u044f \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043e\u0432).<\/p>\n<pre>graphql\/gadgetType.js<\/pre>\n<pre>const graphql = require('graphql');\n\nconst { GraphQLObjectType, GraphQLString } = graphql;\n\nconst GadgetType = new GraphQLObjectType({\n  name: 'Gadget',\n  fields: () =&gt; ({\n\n  })\n});\n\nmodule.exports = GadgetType;<\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u0442\u0438\u043f graphql. \u0412\u043d\u0443\u0442\u0440\u0438 \u043f\u043e\u043b\u0435\u0439 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430.<\/p>\n<pre>const graphql = require('graphql');\n\nconst { GraphQLObjectType, GraphQLObjectType } = graphql;\n\nconst GadgetType = new GraphQLObjectType({\n  name: 'Gadget',\n  fields: () =&gt; ({\n    id: { type: GraphQLString },\n    name: { type: GraphQLString },\n    release_date: { type: GraphQLString },\n    by_company: { type: GraphQLString },\n    price: { type: GraphQLString }\n  })\n});\n\nmodule.exports = GadgetType;<\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0442\u0438\u043f\u044b GraphQLObjectType \u0438 GraphQLObjectType, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0434\u0435\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u0443\u0435\u043c \u0438\u0437 graphQL. \u042d\u0442\u043e \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u043b\u044f graphQL.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0438\u043f\u043e\u0432 GraphQL \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0438 \u0442\u0438\u043f\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043d\u0430\u0448\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u2013 \u044d\u0442\u043e \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433 \u043d\u0430\u0448\u0435\u0433\u043e schema.js. \u041c\u044b \u0445\u043e\u0442\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0442\u044c \u0433\u0430\u0434\u0436\u0435\u0442 \u043f\u043e id.<\/p>\n<p>\u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0439\u0442\u0435 \u043c\u043e\u0434\u0435\u043b\u044c Gadget, gadgetGraphQLType graphql \u0442\u0438\u043f\u0430 \u0438 GraphQLSchema, GraphQLObjectType, GraphQLString \u0438\u0437 graphQL \u0432 schema.js.<\/p>\n<pre>const { GraphQLSchema, GraphQLObjectType, GraphQLString} = require('graphql');\nconst gadgetGraphQLType =  require('.\/gadgetType');\nconst Gadget = require('..\/models\/gadget');<\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441. \u041a\u0430\u0436\u0434\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 GraphQL \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \u0444\u0438\u0433\u0443\u0440\u043d\u044b\u0445 \u0441\u043a\u043e\u0431\u043e\u043a {} \u2013 \u044d\u0442\u043e \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441.<\/p>\n<pre>const RootQuery = new GraphQLObjectType({\n  name: 'RootQueryType',\n  fields: {\n\n  }\n})<\/pre>\n<p>\u0412\u0443\u0430\u043b\u044f! \u0412\u043d\u0443\u0442\u0440\u0438 \u043f\u043e\u043b\u0435\u0439 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u044c gadget \u0437\u0430\u043f\u0440\u043e\u0441.<\/p>\n<pre>const RootQuery = new GraphQLObjectType({\n  name: 'RootQueryType',\n  fields: {\n    gadget: {\n      type: gadgetGraphQLType,\n      args: { id: { type: GraphQLString }},\n      resolve(parent, args) {\n        return Gadget.findById(&lt;a href=\"http:\/\/args.id\" class=\"link link-url\" target=\"_blank\" rel=\"external nofollow noopener noreferrer\"&gt;args.id&lt;\/a&gt;)\n      }\n    }\n  }\n})<\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0442\u0440\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0432\u043d\u0443\u0442\u0440\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0433\u0430\u0434\u0436\u0435\u0442\u0430:<\/p>\n<ul>\n<li>\u0442\u0438\u043f \u2013 \u044d\u0442\u043e \u0442\u0438\u043f \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 gadgetGraphQLType.<\/li>\n<li>\u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u2013 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 graphql, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: gadgets (id: &#171;1&#187;)<\/li>\n<li>\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u2013 \u043a\u0430\u043a \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441? \u0427\u0442\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430? \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0433\u0430\u0434\u0436\u0435\u0442\u0430 \u043f\u043e id.<\/li>\n<\/ul>\n<p>\u0418 \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0439\u0442\u0435.<\/p>\n<pre>module.exports = new GraphQLSchema({\n  query: RootQuery\n});<\/pre>\n<p>\u0424\u0430\u0439\u043b schema.js \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c.<\/p>\n<pre>const { GraphQLSchema, GraphQLObjectType, GraphQLString} = require('graphql');\nconst gadgetGraphQLType =  require('.\/gadgetType');\nconst Gadget = require('..\/models\/gadget');\n\nconst RootQuery = new GraphQLObjectType({\n  name: 'RootQueryType',\n  fields: {\n    gadget: {\n      type: gadgetGraphQLType,\n      args: { id: { type: GraphQLString }},\n      resolve(parent, args) {\n        return Gadget.findById(&lt;a href=\"http:\/\/args.id\" class=\"link link-url\" target=\"_blank\" rel=\"external nofollow noopener noreferrer\"&gt;args.id&lt;\/a&gt;)\n      }\n    }\n  }\n})\n\nmodule.exports = new GraphQLSchema({\n  query: RootQuery\n});<\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u0439\u0434\u0438\u0442\u0435 \u043d\u0430 http:\/\/localhost:9000\/graphql \u0438 \u0441\u0434\u0435\u043b\u0430\u0439\u0442\u0435 \u0437\u0430\u043f\u0440\u043e\u0441.<\/p>\n<pre>{\n    gadget(id: \"5c4e188efb6fc05326ad9264\") {\n        name\n    price\n    by_company\n    release_date\n    id\n  }\n}<\/pre>\n<p>\u042d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0432 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0438\u0442\u043e\u0433\u0435:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-454\" src=\"https:\/\/code.zeba.academy\/ru\/wp-content\/uploads\/sites\/2\/2019\/12\/QraphQl-gadget-code.png\" alt=\"QraphQl-gadget-code\" width=\"2878\" height=\"1042\" \/><\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0447\u0442\u0435\u043d\u0438\u0435! \u2764<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0421\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c API \u0432\u0435\u0441\u0435\u043b\u043e! \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a Koa, GraphQL \u0438 MongoDB. Koa \u2013 \u044d\u0442\u043e \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a Node, \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0438 Express \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u043e\u043c Node. \u041c\u044b \u0437\u0430\u043c\u0435\u043d\u0438\u043c Express \u043d\u0430 Koa, \u0442\u0430\u043a \u043a\u0430\u043a Koa \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 async\/await \u0432\u043c\u0435\u0441\u0442\u043e \u201c\u043a\u043e\u043b\u043b\u0431\u044d\u043a\u043e\u0432\u201d. \u0417\u0430\u043c\u0435\u0442\u043a\u0430; \u0415\u0441\u043b\u0438 \u0432\u044b \u043d\u043e\u0432\u0438\u0447\u043e\u043a \u0432 API, \u044f \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u044d\u0442\u0443 \u043a\u043d\u0438\u0433\u0443, \u0447\u0442\u043e\u0431\u044b [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":455,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_seopress_titles_title":" %%post_title%% %%sep%% %%sitetitle%%","_seopress_titles_desc":"\u0421\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c API \u0432\u0435\u0441\u0435\u043b\u043e! \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a Koa, GraphQL \u0438 MongoDB.","_seopress_robots_index":"","_seopress_robots_follow":"","_seopress_robots_imageindex":"","_seopress_robots_snippet":"","_seopress_robots_primary_cat":"none","_seopress_robots_breadcrumbs":"","_seopress_robots_freeze_modified_date":"","_seopress_robots_custom_modified_date":"","_seopress_robots_canonical":"","_seopress_social_fb_title":"","_seopress_social_fb_desc":"","_seopress_social_fb_img":"","_seopress_social_fb_img_attachment_id":0,"_seopress_social_fb_img_width":0,"_seopress_social_fb_img_height":0,"_seopress_social_twitter_title":"","_seopress_social_twitter_desc":"","_seopress_social_twitter_img":"","_seopress_social_twitter_img_attachment_id":0,"_seopress_social_twitter_img_width":0,"_seopress_social_twitter_img_height":0,"_seopress_redirections_value":"","_seopress_redirections_enabled":"","_seopress_redirections_enabled_regex":"","_seopress_redirections_logged_status":"","_seopress_redirections_param":"","_seopress_redirections_type":301,"_seopress_analysis_target_kw":"","footnotes":""},"categories":[4],"tags":[25,58,78],"class_list":["post-442","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-coding","tag-graphql","tag-mongodb"],"_links":{"self":[{"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/posts\/442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/comments?post=442"}],"version-history":[{"count":0,"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/posts\/442\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/media\/455"}],"wp:attachment":[{"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/media?parent=442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/categories?post=442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code.zeba.academy\/ru\/wp-json\/wp\/v2\/tags?post=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}