apollo-server-koa 简单使用
发布时间:2020-12-15 06:46:35 所属栏目:百科 来源:网络整理
导读:server.js import koa from 'koa'; // koa@2 npm install --save koa@2import koaRouter from 'koa-router'; // koa-router@nextimport koaBody from 'koa-bodyparser'; // koa-bodyparser@nextimport { graphqlKoa,graphiqlKoa } from 'apollo-server-koa';
server.jsimport koa from 'koa'; // koa@2 npm install --save koa@2 import koaRouter from 'koa-router'; // koa-router@next import koaBody from 'koa-bodyparser'; // koa-bodyparser@next import { graphqlKoa,graphiqlKoa } from 'apollo-server-koa'; import schema from './src/graphql/schema.js'; import executableSchema from './src/graphql/executableSchema'; const app = new koa(); const router = new koaRouter(); const PORT = 3000; // koaBody is needed just for POST. router.post('/graphql',koaBody(),graphqlKoa({ schema: executableSchema })); router.get('/graphql',graphqlKoa({ schema: executableSchema })); router.get('/graphiql',graphiqlKoa({ endpointURL: '/graphql' })); app.use(router.routes()); app.use(router.allowedMethods()); app.listen(PORT,()=>console.log(`localhost:${PORT}`)); package.json{ "name": "mcbeath","version": "1.0.0","description": "my mcbeath network","main": "index.js","scripts": { "test": "echo "Error: no test specified" && exit 1","start": "babel-node server.js" },"author": "","license": "MIT","dependencies": { "apollo-server-koa": "^1.1.2","babel-cli": "^6.26.0","babel-preset-env": "^1.6.0","graphql": "^0.11.5","koa": "^2.3.0","koa-bodyparser": "^3.2.0","koa-router": "^7.1.1" } } 必备文件说明
apollo-server-koa:apollo graphql服务
babel-cli:是一个工具,可以帮助你在JavaScript的最新版本编写代码。当你的环境支持不支持某些功能本身,Babel会帮助你编译的功能到一个支持的版本。
babel-preset-env:babel的环境变量,可以支持你的浏览器
graphql:
The GraphQL specification is edited in the markdown files found in
/spec
the latest release of which is published at
http://facebook.github.io/graphql/
.
koa:中间件使用异步函数表达es2017
Expressive HTTP middleware framework for node.js to make web applications and APIs more enjoyable to write. Koa's middleware stack flows in a stack-like manner,allowing you to perform actions downstream then filter and manipulate the response upstream.
koa-bodyparser:A body parser for koa,base on co-body. support json,form and text type body.
koa-router:Router middleware forkoa
.babelrc配置 (必不可少否则最新语法无法编译){ "presets": [ ["env",{ "targets": { "node": true } }] ] } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |