What is the process for integrating Express middleware after implementing Apollo-Server v2 middleware?

I've encountered a unique issue while working with Express and Apollo Server. Despite searching online for solutions, it seems like this requirement is specific to me.

My dilemma arises when I attempt to integrate an Express middleware after using the Apollo Server middleware, but it doesn't seem to work as intended.

For instance:

const { ApolloServer } = require('apollo-server-express');
const express = require('express')

const app = express();
const server = new ApolloServer({typedefs, resolvers})

app.use(...); // Middleware-1
app.use(...); // Middleware-2
app.use(server.getMiddleware()); // Apollo server middleware
app.use(...); // Middleware-3

app.listen({ port: 4000 }, () =>
  console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`)
);

In the provided code snippet, Middleware-3 appears to never get executed. My attempts to find a solution have been fruitless thus far.

Is there a way to trigger Middleware-3 post the implementation of Apollo Server's Middleware?

Thank you.


Edit:1 I neglected to mention that I prefer not to alter the response from the ApolloServer. There are existing Express middlewares that I wish to use without reconfiguring them completely. Is there a workaround to maintain the app.use() order even after incorporating ApolloServer middleware?

Answer â„–1

One potential solution is to assign

res.end2 = res.end
res.end = ()=>{}
within any middleware function that is executed before the ApolloMiddleware, and then use res.end2 to send the response.

Answer â„–2

Once Apollo Server sends the results of your GraphQL request, it invokes res.end. This action finalizes the response and no additional middleware will be triggered.

If you want to customize the response format, you can make use of the formatResponse or formatErrors options, but Express middleware cannot be used for this purpose.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue: make command not located

I'm facing an issue while trying to install packages that require node-gyp. The error message I encounter is as follows: protobuf package resulted in the same outcome.

Despite common suggestions pointing towards a missing make command, I have co ...

Development of a node app along with several node modules happening simultaneously, utilizing npm-link for seamless integration

I've been immersed in building a node.js app while simultaneously working on the necessary modules. The challenge I'm facing involves dealing with peerDependencies and finding the optimal setup for my development environment. Here's what I ...

Retrieve information from an HTML webpage using Node.js

Currently, I am attempting to extract text from an HTML page using Node.js. Here is the URL. I am specifically looking to retrieve the string 0e783d248f0e27408d3a6c043f44f337c54235ce. Although I have tried a certain method, unfortunately, no data seems to ...

Ways to deliver a response in trunk using expressjs/nodejs

In a specific scenario I have, there is a requirement to make multiple API calls. I want to send the responses from these APIs to the client as soon as they are received, without terminating the process. Any suggestions on how this can be achieved using e ...

Assigned a gender to my _id using the first name and last name

Hello, I am attempting to generate an _id in mongodb using a combination of the first name and last name. However, I'm encountering a problem with this process. My desired format for the id is like this: '_id':'midoxnavas' where &a ...

After the for loop, I must gain access to all the data from Axios

I have created a basic word combination website and I am facing an issue in getting all possible words in one string. Here is the code snippet that I have written: const fs=require('fs'); const axios=require('axios') function test(want) ...

Utilizing RavenDB with NodeJS to fetch associated documents and generate a nested outcome within a query

My index is returning data in the following format: Company_All { name : string; id : string; agentDocumentId : string } I am wondering if it's possible to load the related agent document and then construct a nested result using selectFie ...

You are unable to use fetch() to make post requests to the specified URL

I'm encountering an issue while attempting to send data from my frontend React application to my Node backend. Every time I make a post request using the fetch() function, I encounter the following error in the Firefox Dev Tools console: TypeError: N ...

What is the reason for the absence of the $.ajax function in the jQuery package designed for node.js?

Here is my code sample, which I would like to use for practicing with jQuery's ajax function. Note that I have already installed the jQuery package using npm install jquery: var $ = require('jquery'); var remoteValue = false; var doSometh ...

Tips for utilizing code submitted by a user to extract meaningful errors

As I work on developing a mod for the online game Cookie Clicker, I've implemented a feature that allows users to input their own code to enhance the functionality of my mod. However, before users save their code using my custom editor, I want to run ...

Changing a 64-bit Steam ID to a 32-bit account ID

Is there a way to convert a 64-bit Steam ID to a 32-bit account ID in Node.js? According to Steam, you should take the first 32 bits of the number, but how exactly can this be done in Node? Would using BigNumber be necessary to handle the 64-bit integer? ...

Best practices for securing your App/API endpoints

I am currently in the process of developing a mobile app (using Ionic/Cordova) that will interact with an API built with NodeJS. As I navigate through this project, I find myself contemplating how to implement a user authentication method that serves dual ...

Is there an alternative to @bittrance/azure-function-express that supports the newest node versions?

I recently utilized the azure-function-express package, specifically the @bittrance/azure-function-express version, to establish a connection between an Express application and an Azure Function handler. This allowed for seamless integration of all familia ...

What is the process for setting up a Quill Editor within an Angular 2 Component?

I am currently working on creating my own Quill editor component for my Angular 2 project. To integrate Quill into my project, I utilized npm for installation. My goal is to develop a word counter application using this component and I am referring to the ...

Issue encountered when attempting to access data asynchronously from mongodb using node.js

Within the code below, I am retrieving data from MongoDB and storing it in a variable. My issue arises when attempting to use this variable outside of the mongodb.connect() function, as it consistently outputs undefined. I understand that this is related ...

Encountering an InternalOAuthError while attempting to access the passport google login API because the system failed to obtain

Having some trouble implementing Google login with the passport-google-oauth package and encountering the following error. InternalOAuthError: Failed to obtain access token at Strategy.OAuth2Strategy._createOAuthError (/Users/rishiraj/Documents/Projects/ad ...

Leveraging webpack for CSS bundling

I'm currently working on bundling some NPM modules using webpack instead of utilizing a CDN. While I've successfully managed to integrate the .js files, I'm facing challenges with including the .css files for these modules. One example is ...

Performing an API GET request in a header.ejs file using Node.js

Looking to fetch data from an endpoint for a header.ejs file that will be displayed on all routed files ("/", "/news" "/dogs"). Below is my app.js code: // GET API REQUEST var url = 'https://url.tld/api/'; request(url, function (error, response, ...

Access your local server from your smartphone by utilizing Express and Node.js to connect to localhost

I am facing an issue where I cannot access the server I host on my computer (using Node.js & Express) from my phone, even though both devices are on the same network. When I type localhost:3000 in the browser on my desktop PC, everything works perfectly f ...

Is there a way to automatically redirect the server URL when a file is modified?

I am currently experimenting with a function that is supposed to only display a message in the console without redirecting the actual URL of my server when a file is changed. watcher.add("/home/diegonode/Desktop/ExpressCart-master/routes/2.mk"); watche ...