Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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());
}
// GraphQL Voyager?
if (process.env.VOYAGER) {
router.all(`/${paths.VOYAGER_PATH}`, koaMiddleware({
endpointUrl: GRAPHQL_ENDPOINT,
displayOptions: {
sortByAlphabet: true,
},
}));
}
// GraphiQL?
if (process.env.GRAPHIQL) {
router.get(
`/${paths.GRAPHIQL_PATH}`,
graphiqlKoa({ endpointURL: GRAPHQL_ENDPOINT }),
);
}
// GraphQL Playground?
if (process.env.PLAYGROUND) {
router.all(
`/${paths.PLAYGROUND_PATH}`,
koaPlayground({ endpoint: GRAPHQL_ENDPOINT }),
);
}
// Koa Heartbeat
app.use(koaHeartbeat({ path: `/${paths.LIVENESS_PATH}`, body: 'ok' }));
app.use(router.routes());
app.use(router.allowedMethods());
import logger from 'koa-logger';
import koaRouter from 'koa-router';
import koaBody from 'koa-bodyparser';
import { graphqlKoa, graphiqlKoa } from 'apollo-server-koa';
import schema from './schema';
/* eslint new-cap: ["error", { "newIsCap": false }] */
const app = new koa();
const router = new koaRouter();
// koaBody is needed just for POST.
router.post('/graphql', koaBody(), graphqlKoa({ schema }));
router.get('/graphql', graphqlKoa({ schema }));
// GraphiQL tool
router.get('/graphiql', graphiqlKoa({ endpointURL: '/graphql' }));
if (process.env.NODE_ENV === 'development') {
const corsOptions = {
credentials: true,
origin: '*',
};
app.use(cors(corsOptions));
}
app.use(logger());
app.use(router.routes());
app.use(router.allowedMethods());
export default app;
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}`)
})
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);
});
}
`,
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);
const app = new Koa()
const router = new Router()
app.use(bodyParser())
app.use(injectContext({
connection,
}))
app.use(validateToken)
router.post('/graphql', graphqlKoa((ctx) => ({
schema: graphQLSchema,
context: new GlobalContext(ctx),
})))
router.get(
'/graphiql',
graphiqlKoa({
endpointURL: '/graphql',
}),
)
app.use(router.routes())
app.use(router.allowedMethods())
app.listen(config.port)
logger.info(`Server is running on port ${config.port}`)
}).catch((error) => logger.error(error))
constructor() {
this.app.use(koaBody())
this.router.post(
'/graphql',
graphqlKoa({
schema
})
)
this.router.get(
'/graphiql',
graphiqlKoa({
endpointURL: '/graphql'
})
)
this.app.use(this.router.routes())
this.app.use(this.router.allowedMethods())
this.app.use(
serve(path.resolve(__dirname, '../static'), {
index: 'index.html'
})
)
}
export default function createGraphQlKoaRouter(
schema: GraphQLSchema,
{ endpoint, graphiQlEndpointActive, graphiQlEndpoint }: IGraphQlConfig
): KoaRouter {
const koaGraphQlOptionsFunction = getKoaGraphQLOptionsFunction(schema);
const gqlKoaRouter = new KoaRouter();
gqlKoaRouter.post(endpoint, koaBody(), enforceOriginMatch, setCacheHeaders, apolloServer.graphqlKoa(koaGraphQlOptionsFunction));
gqlKoaRouter.get(endpoint, enforceOriginMatch, setCacheHeaders, apolloServer.graphqlKoa(koaGraphQlOptionsFunction));
if (graphiQlEndpointActive === true) {
gqlKoaRouter.get(graphiQlEndpoint, apolloServer.graphiqlKoa({ endpointURL: endpoint }));
}
return gqlKoaRouter;
}
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'
}))
}
export default router