Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] FeathersJS + Apollo Server 2.0 #929

Closed
sxwebdev opened this issue Aug 12, 2018 · 13 comments
Closed

[Question] FeathersJS + Apollo Server 2.0 #929

sxwebdev opened this issue Aug 12, 2018 · 13 comments

Comments

@sxwebdev
Copy link

sxwebdev commented Aug 12, 2018

Hey. As you know, recently, the apollo server was updated to version 2.0.
How to use it with current version of feathers?
I generated a new custom service and put next code.

graphql.service.js

const { ApolloServer, gql } = require('apollo-server-express');
module.exports = function (app) {
	const typeDefs = gql`
	type Query {
		hello: String
	}
	`;

	const resolvers = {
		Query: {
			hello: () => 'hello world'
		}
	};
	const server = new ApolloServer({
		typeDefs,
		resolvers,
		context: ({req,res}) => ({
			provider: req.feathers.provider,
			headers: req.feathers.headers
		})
	});
	server.applyMiddleware({ app });
};

When I try to use /graphql path in browser, I get this error
message=Page not found, type=FeathersError, name=NotFound, code=404, className=not-found, url=/graphql, , stack=NotFound: Page not found
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

How to resolve it? Thanks.

@daffl
Copy link
Member

daffl commented Aug 12, 2018

The order when it is called matters and has to happen before the notFound middleware (FAQ reference).

@sxwebdev
Copy link
Author

sxwebdev commented Aug 12, 2018

image

I registered a custom middleware before app.use(express.notFound());

Custom middleware exist in services

@sxwebdev
Copy link
Author

sxwebdev commented Aug 12, 2018

If I delete this line app.use(express.notFound());, everything works as expected

image

@daffl
Copy link
Member

daffl commented Aug 12, 2018

There is a chance that server.applyMiddleware probably runs asynchronously. You could try adding the notFound handler after the server has been started and is listening.

@sxwebdev
Copy link
Author

You could try adding the notFound handler after the server has been started and is listening.

It did not help

@daffl
Copy link
Member

daffl commented Aug 12, 2018

I would test if a standard Express application with a similar notFound handler like

const app = express();

// Apollo server setup here
server.applyMiddleware({ app });

app.use(function (req, res, next) {
  next(new Error('Not found'));
});

Has the same issue.

@sxwebdev
Copy link
Author

sxwebdev commented Aug 12, 2018

if set content-type to application/json in Postman - apollo server works correctly.

I think the problem is here:
image
https://github.com/apollographql/apollo-server/blob/master/packages/apollo-server-express/src/ApolloServer.ts#L147

@sxwebdev
Copy link
Author

I would test if a standard Express application with a similar notFound handler like

error: message=Not found, stack=Error: Not found

and then

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

@sxwebdev
Copy link
Author

If comment this line in ApolloServer.js
image
everything works fine

@daffl
Copy link
Member

daffl commented Aug 13, 2018

That actually looks like a bug in the Apollo server. In Express next should never be called once the middleware calls res.end().

@daffl
Copy link
Member

daffl commented Aug 15, 2018

Closing since it wasn't a Feathers issue and has been fixed in Apollo via apollographql/apollo-server#1436

@daffl daffl closed this as completed Aug 15, 2018
@lock
Copy link

lock bot commented Feb 7, 2019

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue with a link to this issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants