【发布时间】:2019-05-02 18:06:43
【问题描述】:
我想对我的所有路由使用morgan 的tiny 日志语句,graphql 端点除外。我正在使用 express 和 Apollo 2,但无法让中间件与 express 一起使用。如代码示例所示,我可以为整个 express 应用安装中间件,但我想限制范围。
我的第一次尝试是创建一个express.router() 并将路由器传递给apolloServer.applyMiddleware,但这似乎不起作用。
我想使用morgan--但我也想使用express-jwt 中间件。
import morgan from 'morgan'
import { mergeSchemas } from 'graphql-tools'
import { ApolloServer } from 'apollo-server-express'
import assessmentSchema from './assessment/schema'
import AssessmentAPI from './assessment/dataSource'
import userSchema from './user/schema'
import UserAPI from './user/dataSource'
/**
* Installs apollo-server to handle requests under `path`
* @param {*} app Express instance
* @param {*} path route path, like '/graphql'
*/
export const createApi = (app, path) => {
const dataSources = () => ({
assessmentAPI: new AssessmentAPI({ store: 'intentionally undefined' }),
userAPI: new UserAPI()
})
const schema = mergeSchemas({
schemas: [assessmentSchema, userSchema]
})
morgan.token('graphql-query', req => {
const { operationName } = req.body
return `GRAPHQL: Operation Name: ${operationName}`
})
// TODO: Add custom logging middleware for GraphQL queries/mutations
// The next line would add middleware to all of express, but I only want this style of logging for graphQL
/*** Question is about the following line ***/
// app.use(morgan(':graphql-query'))
const apolloServer = new ApolloServer({ schema, dataSources })
apolloServer.applyMiddleware({ app, path })
}
谢谢!
【问题讨论】:
-
在GitHub上添加相关评论:github.com/apollographql/apollo-server/issues/…
-
我只是在尝试使用快速路由器,它正在工作。 gist.github.com/rohitharkhani/45d3111807a7094b73bea71c124c742b。不确定这是否特定于其他模块。您可以仔细检查一下您是否已将路由器添加到应用程序中?
标签: node.js express graphql apollo apollo-server