How can I use express middleware with only Apollo Server 2 graphql endpoints
There are some 'Hacky' ways to achieve what you desire. You can use express.Route to register middlewares on each route instead, but I think that you may want more specific logs about GraphQL rather than the request in particular.
context()
Available as a callback inside ApolloServer, it receives an object with the request and response.
const myServer = new ApolloServer({ schema: ..., context:({ req, res }) => { // log here }});
fortmatResponse()
Available as a callback inside ApolloServer, it receives the response and the query.
const server = new Apollo.ApolloServer({ schema: ..., formatResponse: (res, query) => { // log here // notice you must return the response return res; },});
Sources:formatResponse, context
Edit
Another thing you can do is on morgan callback check if the req.path matches with the /graphQL
path, and log only in that situation but this is much the same as log an Express.Route with morgan
With apollo server v2, its really simple to use it only on a single route. apply it as middleware. i.e
const app = require('express')();const apolloServer = new ApolloServer ({ typeDefs, resolvers}) // then use it on a particular routeapolloServer.applyMiddleware({ app, path: '/specialUrl' });
const express = require("express");const router = express.Router();const { ApolloServer, gql } = require('apollo-server-express');const server = new ApolloServer({ schema: schema, introspection: true}); server.applyMiddleware({ app:router });module.exports = router;