question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

/graphiql is 404 at Koa when wrapped by next

See original GitHub issue

When koa-router is wrapped by next, /graphiql become 404

  • [ x] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

/graphiql should not 404

Current Behavior

/graphiql 404

Steps to Reproduce (for bugs)

wrapped with next js, this code below will make /graphiql 404

const Koa = require('koa');
const Router = require('koa-router');
const koaBody = require('koa-bodyparser');
const next = require('next');
const { graphqlKoa, graphiqlKoa } = require('apollo-server-koa');
const { makeExecutableSchema } = require('graphql-tools');

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare()
  .then(() => {
    const server = new Koa();
    const router = new Router();

    router.get('*', async (ctx) => {
      await handle(ctx.req, ctx.res);
    });

    const typeDefs = `
      type Query {
        hello: String
      }
    `;

    const resolvers = {
      Query: {
        hello: () => 'Hello world!',
      },
    };

    // Put together a schema
    const schema = makeExecutableSchema({
      typeDefs,
      resolvers,
    });

    router.post('/graphql', koaBody(), graphqlKoa({ schema }));
    router.get('/graphql', graphqlKoa({ schema }));

    router.get('/graphiql', graphiqlKoa({ endpointURL: '/graphql' }));

    server.use(router.routes());
    server.use(router.allowedMethods());

    server.listen(3000, (err) => {
      if (err) throw err;
      console.log('> Ready on http://localhost:3000');
    });
  })
  .catch((ex) => {
    console.error(ex.stack);
    process.exit(1);
  });

but if we change the code like this, i can access /graphiql

const Koa = require('koa');
const Router = require('koa-router');
const koaBody = require('koa-bodyparser');

const { graphqlKoa, graphiqlKoa } = require('apollo-server-koa');
const { makeExecutableSchema } = require('graphql-tools');

const app = new Koa();
const router = new Router();
const PORT = 3000;

const typeDefs = `
  type Query {
    hello: String
  }
`;

const resolvers = {
  Query: {
    hello: () => 'Hello world!',
  },
};

const myGraphQLSchema = makeExecutableSchema({
  typeDefs,
  resolvers,
});

// koaBody is needed just for POST.
router.post('/graphql', koaBody(), graphqlKoa({ schema: myGraphQLSchema }));
router.get('/graphql', graphqlKoa({ schema: myGraphQLSchema }));

router.get('/graphiql', graphiqlKoa({ endpointURL: '/graphql' }));

app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000, (err) => {
  if (err) throw err;
  console.log('> Ready on http://localhost:3000');
});

response console

 DONE  Compiled successfully in 2128ms                                                                                                                                        22:59:14

> Ready on http://localhost:3000
Client pings, but there's no entry for page: /graphiql
Client pings, but there's no entry for page: /graphiql
Client pings, but there's no entry for page: /graphiql
Client pings, but there's no entry for page: /graphiql
Client pings, but there's no entry for page: /graphiql
Client pings, but there's no entry for page: /graphiql
Client pings, but there's no entry for page: /graphiql
Client pings, but there's no entry for page: /graphiql

Your Environment

Tech Version
next 5.1.0
node 8.9.4
OS ubuntu 17
browser chrome

Here’s my complete dependencies

 "dependencies": {
    "apollo-server-koa": "^1.3.4",
    "bcrypt": "^2.0.0",
    "bluebird": "^3.5.1",
    "dotenv": "^5.0.1",
    "googleapis": "^28.1.0",
    "graphql": "^0.13.2",
    "graphql-tools": "^2.24.0",
    "isomorphic-unfetch": "^2.0.0",
    "koa": "^2.5.0",
    "koa-bodyparser": "^4.2.0",
    "koa-router": "^7.4.0",
    "lodash": "^4.17.5",
    "next": "^5.1.0",
    "pg": "^7.4.1",
    "pg-hstore": "^2.3.2",
    "react": "^16.3.1",
    "react-dom": "^16.3.1",
    "reactstrap": "^5.0.0",
    "sequelize": "^4.37.6",
    "styled-jsx": "^2.2.6",
    "xlsx": "^0.12.8"
  },

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
ghengeveldcommented, Apr 29, 2018

Next’s route handler must be last because it does a catch-all to it’s 404 page. I’ve previously commented on this behavior and provided a workaround here: https://github.com/zeit/next.js/issues/4042

I’m still not happy with this behavior, but at least there’s a reasonably simple workaround.

1reaction
fallioncommented, Apr 23, 2018

Next handler should go last.

Read more comments on GitHub >

github_iconTop Results From Across the Web

type-graphql/Lobby - Gitter
Hey, I'm getting an error while generating the schema "GraphQLError: Type Query must define one or more fields." I'm using the recipe resolver...
Read more >
Koa - next generation web framework for node.js
By default outputs all errors to stderr unless app.silent is true . The default error handler also won't output errors when err.status is...
Read more >
Responses | Strapi Documentation
Get response status. By default, response.status is set to 404 unlike node's res.statusCode which defaults to 200 .
Read more >
Everything You Need to Know About Koa - Morioh
Koa - next generation web framework for Node.js. ... The default error handler also won't output errors when err.status is 404 or err.expose...
Read more >
Building an end-to-end typesafe API — without GraphQL
Shortly thereafter GraphQL was announced and quickly became the de facto way to ... return res.status(404).send('Endpoint not found'); });.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found