Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// @flow
const koaBody = require('koa-body');
import { graphqlKoa, graphiqlKoa } from 'apollo-server-koa';
import { graphqlSchema } from './graphql/graphql';
import UserController from './controller/UserController';
import { wrappingKoaRouter } from '../src/transform/router/koa-router';
import AnnouncementController from './controller/AnnouncementController';
const Router = require('koa-router');
const router = new Router();
// koaBody is needed just for POST.
router.post('/graphql', koaBody(), graphqlKoa({ schema: graphqlSchema }));
router.get('/graphql', graphqlKoa({ schema: graphqlSchema }));
router.post('/graphiql', graphiqlKoa({ endpointURL: '/graphql' }));
router.get('/graphiql', graphiqlKoa({ endpointURL: '/graphql' }));
// 定义默认的根路由
router.get('/', async function(ctx, next) {
ctx.body = { msg: 'Node Server Boilerplate' };
});
// 封装原有的 koa-router 对象
wrappingKoaRouter(router, 'localhost:8080', '/api', {
title: 'Node Server Boilerplate',
version: '0.0.1',
description: 'Koa2, koa-router,Webpack'
});
const gQlParam = (ctx) => {
ctx.state.authRequired = false;
ctx.state.includesMutation = false;
return {
schema,
context: {
ctx,
accessToken: ctx.state.accessToken
}
};
};
// koaBody is needed just for POST.
gqlKoaRouter.post(this.graphQlConfig.endpoint, koaBody(), setCacheHeaders, graphqlKoa(gQlParam));
gqlKoaRouter.get(this.graphQlConfig.endpoint, setCacheHeaders, graphqlKoa(gQlParam));
// graphiql
if (this.graphQlConfig.graphiQlEndpointActive) {
gqlKoaRouter.get(this.graphQlConfig.graphiQlEndpoint, graphiqlKoa({ endpointURL: this.graphQlConfig.endpoint }));
}
const app = this.server.getApp();
app.use(gqlKoaRouter.routes());
app.use(gqlKoaRouter.allowedMethods());
}
const router = new koaRouter();
const db = mongoose.createConnection(['mongodb://', configs.mongodb.ip, '/', configs.mongodb.dbname].join(''));
if(db) {
console.log('mongodb connected successfully');
global.db = db;
}else {
console.log('mongodb connected failed');
}
import schemaRouters from './routers/schemaRouters';
const schemas = schemaRouters().default;
router.post('/graphql', koaBody(), graphqlKoa({ schema: schemas.HelloSchema }));
router.get('/graphql', graphqlKoa({ schema: schemas.HelloSchema }));
router.get('/graphiql', graphiqlKoa({ endpointURL: '/graphql' }));
app.use(convert(cors(configs.cors)));
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(configs.port, () => {
console.log('app started successfully, listening on port ' + configs.port);
});
const db = mongoose.createConnection(['mongodb://', configs.mongodb.ip, '/', configs.mongodb.dbname].join(''));
if(db) {
console.log('mongodb connected successfully');
global.db = db;
}else {
console.log('mongodb connected failed');
}
import schemaRouters from './routers/schemaRouters';
const schemas = schemaRouters().default;
router.post('/graphql', koaBody(), graphqlKoa({ schema: schemas.HelloSchema }));
router.get('/graphql', graphqlKoa({ schema: schemas.HelloSchema }));
router.get('/graphiql', graphiqlKoa({ endpointURL: '/graphql' }));
app.use(convert(cors(configs.cors)));
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(configs.port, () => {
console.log('app started successfully, listening on port ' + configs.port);
});
import { graphqlKoa, graphiqlKoa } from 'apollo-server-koa'
import schema from './graphql/schema'
import config from './config'
const isProd = process.env.NODE_ENV === 'production'
const app = new Koa()
const router = new Router()
const port = process.env.PORT || config.port
const host = process.env.HOST || config.host
app.use(bodyParser())
app.use(KoaStatic(path.resolve(__dirname, '../build')))
router.all('/graphql', graphqlKoa({ schema }))
!isProd && router.get('/graphiql', graphiqlKoa({ endpointURL: '/graphql' }))
router.get('*', (ctx, next) => {
ctx.body = fs.readFileSync('../build/index.html', 'utf-8')
})
app
.use(router.routes())
.use(router.allowedMethods())
app.listen(port, () => console.log(`server running on http://${host}:${port}`))
import * as Koa from 'koa'
import * as KoaRouter from 'koa-router'
import * as KoaBody from 'koa-bodyparser'
import * as cors from 'kcors'
import { graphqlKoa, graphiqlKoa } from 'apollo-server-koa'
import schema from './graphql'
const app = new Koa()
const router = new KoaRouter()
const PORT = process.env.PORT || 4020
router.post('/graphql', KoaBody(), graphqlKoa({ schema }))
router.get('/graphql', graphqlKoa({ schema }))
router.post('/graphiql', graphiqlKoa({ endpointURL: '/graphql' }))
router.get(
'/graphiql',
graphiqlKoa({
endpointURL: '/graphql'
})
)
app.use(cors())
app.use(router.routes())
app.use(router.allowedMethods())
app.listen(PORT, () => {
console.log(`GraphQL API Server is now running on http://localhost:${PORT}`)
})
router.all(config.path, async (ctx, next) => {
if (launchGui(config, ctx.req, gui, ctx, next)) {
return;
}
return graphqlKoa(request)(ctx, next);
});
import Koa from 'koa'
import KoaRouter from 'koa-router'
import config from 'config'
import * as graphql from 'graphql'
import { graphqlKoa, graphiqlKoa } from 'apollo-server-koa'
import querySchema from '../schema/query'
import mutationSchema from '../schema/mutation'
const router = new KoaRouter({
prefix: '/api'
})
let graphqlHander = graphqlKoa((ctx: Koa.Context) => ({
schema: new graphql.GraphQLSchema({
query: querySchema,
mutation: mutationSchema
}),
context: {
session: ctx.session
}
}))
router.get('/graphql', graphqlHander)
router.post('/graphql', graphqlHander)
if (config.get('debug')) {
router.get('/graphiql', graphiqlKoa({
endpointURL: '/api/graphql'
}))
module.exports = function (endpoint, ...middlewares) {
const options = middlewares.pop()
return mount(endpoint, compose(
[ ...middlewares, graphqlKoa(options) ]
))
}
type Query {
hello: String
}
`,
resolvers: {
Query: {
hello: (root, args, context) => 'Hello world!',
},
}
});
app.use(logger());
app.use(bodyParser());
router.get(GRAPHQL_ENDPOINT, graphqlKoa({ schema }));
router.post(GRAPHQL_ENDPOINT, graphqlKoa({ schema }));
router.get('/graphiql', graphiqlKoa({ endpointURL: GRAPHQL_ENDPOINT }));
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(PORT);