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

Apollo Server ALWAYS returns 404 when queried from node.js #3476

Closed
tomitrescak opened this issue Nov 6, 2019 · 5 comments
Closed

Apollo Server ALWAYS returns 404 when queried from node.js #3476

tomitrescak opened this issue Nov 6, 2019 · 5 comments

Comments

@tomitrescak
Copy link

tomitrescak commented Nov 6, 2019

"apollo-server": "^2.9.7",

When querying my Apollo Server from Node.js I always receive 404 response with no further information. The Postman and curl works fine. I tried all possible options and nothing seems to work. As a result I cannot download introspection queries and my VSCODE plugins do not work.

This is my server code:

export function start(port = '4000') {
  const server = new ApolloServer({
    schema,
    cors: {
      origin: '*',
      methods: 'GET,HEAD,POST'
    },
    context: req => initContext(req)
  });

  server.listen({ port }).then(() => console.log('Server is running on localhost:4000'));
}

This is WORKING curl query:

curl -X POST http://localhost:4000/graphql \
-H "Content-Type: application/json" \
-d '{"query": "query Roles { roles { description } }"}'

This is NOT working from node.js

const { createApolloFetch } = require('apollo-fetch');

const fetch = createApolloFetch({
  uri: 'http://127.0.0.1:4000'
});

fetch.useAfter(({ response }, next) => {
  console.log(response); // <--- WILL BE 404
  next();
});

fetch({
  query: 'query Roles { roles { description } }'
})
  .then(res => {
    console.log(res);
  })
  .catch(err => {
    console.log(err);
  });
@tomitrescak
Copy link
Author

tomitrescak commented Nov 6, 2019

Here is repro for you using latest apollo-server and example from apollo page:

// server.js
const { ApolloServer, gql } = require('apollo-server');

// A schema is a collection of type definitions (hence "typeDefs")
// that together define the "shape" of queries that are executed against
// your data.
const typeDefs = gql`
  # Comments in GraphQL strings (such as this one) start with the hash (#) symbol.

  # This "Book" type defines the queryable fields for every book in our data source.
  type Book {
    title: String
    author: String
  }

  # The "Query" type is special: it lists all of the available queries that
  # clients can execute, along with the return type for each. In this
  # case, the "books" query returns an array of zero or more Books (defined above).
  type Query {
    books: [Book]
  }
`;

const books = [
  {
    title: 'Harry Potter and the Chamber of Secrets',
    author: 'J.K. Rowling'
  },
  {
    title: 'Jurassic Park',
    author: 'Michael Crichton'
  }
];

const resolvers = {
  Query: {
    books: () => books
  }
};

const server = new ApolloServer({ typeDefs, resolvers });

// The `listen` method launches a web server.
server.listen().then(({ url }) => {
  console.log(`🚀  Server ready at ${url}`);
});

Run

node server.js

This works:

curl -X POST http://localhost:4000/graphql \   
-H "Content-Type: application/json" \
-d '{"query": "query Books { books { title } }"}'

This gives 404

const { createApolloFetch } = require('apollo-fetch');

const fetch = createApolloFetch({
  uri: 'http://localhost:4000/graphql'
});

fetch.useAfter(({ response }, next) => {
  console.log(response); // <--- WILL BE 404
  next();
});

fetch({
  query: 'query Books { books { title } }'
})
  .then(res => {
    console.log(res); // <--- WILL BE "{}"
  })

@abernix
Copy link
Member

abernix commented Nov 7, 2019

I just tried the reproductions you provided in the second comment and they work perfectly for me, with the second returning the result of the query as expected:

image

You can also see them working in these two glitches which work with each other:

Server: https://glitch.com/edit/#!/better-chime
Client: https://glitch.com/edit/#!/paint-bloom

Perhaps make sure that you have no other servers running on port 4000. If you continue to have problems, please share a runnable reproduction, preferably in an isolated environment like Glitch or CodeSandbox — rather than copy/pasted code samples which don't portray your package.json, etc. with the Apollo Server channel of Apollo's Spectrum.chat prior to reporting it as a bug. Thanks!

@abernix abernix closed this as completed Nov 7, 2019
@tomitrescak
Copy link
Author

@abernix thanks for your reply, although there is no need to be condescending. I did try spectrum. No response there for few days.

Moreover the examples you are showing .. if I understand it right do the query to server from the browser (alas your request for a demo in code sandbox).

As I mentioned in my description I am trying to query the server from a node.js environment. So you need to run my query example from node. I tried with 2 machines with the same result. My package.json contains only Apollo-server and Apollo-fetch packages.

I’ll prepare a repo for it and test on more machines.

@abernix
Copy link
Member

abernix commented Nov 18, 2019

@tomitrescak I wasn't aiming to be or trying to be condescending (Further, I don't believe I said anything condescending, but that's obviously subjective.) What I am demonstrating is what I observed with your provided reproduction — which behaved the same in both environments I tested: prior to uploading them them to Glitch, I'd created and ran both examples locally using Node.js (Glitch is also using Node.js to do the querying, so there should be no effective difference).

If your results still differ, there must be other local environmental circumstances which are affecting the result (as I alluded to). To figure out exactly what those differences are, we'll need more to work with — which is why I was suggesting the preference of a runnable reproduction. Feel free to ping me on Spectrum if nobody from the community is able to help you and I'll do my best to jump in!

@Xunnamius
Copy link

Xunnamius commented Feb 7, 2021

I also encountered something like this, where sending a request to my Next.js graphql endpoint responded correctly in the terminal with curl (and also when run with Next.js) but not when called in my node unit tests by jest.

Turns out my problem came from here (more specifically, here). For various reasons, the url of my request object didn't end up being equal to the this.graphqlPath that it was expecting. Ensuring url was set properly fixed everything :)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
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

3 participants