这是一个GraphQL服务器,支持Express,Connect,Hapi和Koa
示例代码:
Express:
import express from 'express';import bodyParser from 'body-parser';import { graphqlExpress } from 'graphql-server-express';const myGraphQLSchema = // ... define or import your schema here!const PORT = 3000;var app = express();// bodyParser is needed just for POST.app.use('/graphql', bodyParser.json(), graphqlExpress({ schema: myGraphQLSchema }));app.listen(PORT);Connect:
import connect from 'connect';import bodyParser from 'body-parser';import { graphqlConnect } from 'graphql-server-express';import http from 'http';const PORT = 3000;var app = connect();// bodyParser is needed just for POST.app.use('/graphql', bodyParser.json());app.use('/graphql', graphqlConnect({ schema: myGraphQLSchema }));http.createServer(app).listen(PORT);Hapi:
import hapi from 'hapi';import { graphqlHapi } from 'graphql-server-hapi';const server = new hapi.Server();const HOST = 'localhost';const PORT = 3000;server.connection({ host: HOST, port: PORT,});server.register({ register: graphqlHapi, options: { path: '/graphql', graphqlOptions: { schema: myGraphQLSchema, }, route: { cors: true } },});server.start((err) => { if (err) { throw err; } console.log(`Server running at: ${server.info.uri}`);});
评论