0

I wanted to set-up a new Next JS project with graphql using TypeGraphQL. I thought it is a good idea to skip stand alone server and use API routes provided by Next JS. Here is an example I was inspired by. Unfortunately I can't get graphql up and running.

Next JS starts without errors but it throws one right after sending request to api/graphql

TypeError: resolver is not a function
    at apiResolver (/home/vavra/Projects/project-united/node_modules/next/dist/next-server/server/api-utils.js:6:7)
    at DevServer.handleApiRequest (/home/vavra/Projects/project-united/node_modules/next/dist/next-server/server/next-server.js:43:427)
    at async Object.fn (/home/vavra/Projects/project-united/node_modules/next/dist/next-server/server/next-server.js:35:764)
    at async Router.execute (/home/vavra/Projects/project-united/node_modules/next/dist/next-server/server/router.js:28:28)
    at async DevServer.run (/home/vavra/Projects/project-united/node_modules/next/dist/next-server/server/next-server.js:44:494)
    at async DevServer.handleRequest (/home/vavra/Projects/project-united/node_modules/next/dist/next-server/server/next-server.js:13:133)

Here is my file src/pages/api/graphql

import { Resolver, Query } from "type-graphql";
import { ApolloServer } from "apollo-server-micro";
import { buildSchema } from "type-graphql";
import "reflect-metadata";

@Resolver()
class HelloResolver {
  @Query(() => String)
  hello() {
    return "hello";
  }
}

export const config = {
  api: {
    bodyParser: false,
  },
};

export default (async () => {
  const schema = await buildSchema({
    resolvers: [HelloResolver],
  });

  const apolloServer = new ApolloServer({ schema });

  return apolloServer.createHandler({ path: "/api/graphql" });
})();

Any help with this?

2
  • 1
    Did you manage to work this out? I'm also trying to setup TypeGraphQL in a NextJS app and running into same problem. I'm wondering if I need to hack my next app to use express (bleh). Commented Jun 7, 2020 at 4:19
  • @CodeAndCats No luck I did not find any solution so I run my server with Express. Commented Jun 7, 2020 at 6:06

1 Answer 1

3

here they were talking about a similar problem, and with this form I work nextjs with type-graphql.

pages/api/graphql

import 'reflect-metadata';
import { ApolloServer } from 'apollo-server-micro';
import { buildSchema } from 'type-graphql';
import youResolver from '../resolvers/youResolver';
import { NextApiRequest, NextApiResponse } from 'next';

let apolloServerHandler: (req: any, res: any) => Promise<void>;

const getApolloServerHandler = async () => {
  if (!apolloServerHandler) {
    const schema = await buildSchema({
      resolvers: [youResolver],
    });
    apolloServerHandler = new ApolloServer({ schema }).createHandler({
      path: '/api/graphql',
    });
  }
  return apolloServerHandler;
};

export default async (req: NextApiRequest, res: NextApiResponse) => {
  const apolloServerHandler = await getApolloServerHandler();
  return apolloServerHandler(req, res);
};

export const config = { api: { bodyParser: false } };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.