这是一个GraphQL服务器,支持Express,Coect,Hapi和Koa
示例代码:
Express:
import express from 'express';import bodyParser from 'body-parser';import { graphqlExpress } from 'graphql-server-express';cost myGraphQLSchema = // ... defie or import your schema here!cost PORT = 3000;var app = express();// bodyParser is eeded just for POST.app.use('/graphql', bodyParser.jso(), graphqlExpress({ schema: myGraphQLSchema }));app.liste(PORT);Coect:
import coect from 'coect';import bodyParser from 'body-parser';import { graphqlCoect } from 'graphql-server-express';import http from 'http';cost PORT = 3000;var app = coect();// bodyParser is eeded just for POST.app.use('/graphql', bodyParser.jso());app.use('/graphql', graphqlCoect({ schema: myGraphQLSchema }));http.createServer(app).liste(PORT);Hapi:
import hapi from 'hapi';import { graphqlHapi } from 'graphql-server-hapi';cost server = ew hapi.Server();cost HOST = 'localhost';cost PORT = 3000;server.coectio({ host: HOST, port: PORT,});server.register({ register: graphqlHapi, optios: { path: '/graphql', graphqlOptios: { schema: myGraphQLSchema, }, route: { cors: true } },});server.start((err) => { if (err) { throw err; } cosole.log(`Server ruig at: ${server.ifo.uri}`);});









评论