awesome-links icon indicating copy to clipboard operation
awesome-links copied to clipboard

Nexus and ApolloServer types discrepancy

Open milovangudelj opened this issue 2 years ago • 3 comments

When passing a Nexus-generated schema to ApolloServer like this:

const apolloServer = new ApolloServer({
   schema,
   resolvers,
   context: createContext,
});

the editor starts screaming at me saying Type 'NexusGraphQLSchema' is missing the following properties from type 'GraphQLSchema': _queryType, _mutationType, _subscriptionType, _directives, and 3 more..

Here is my graphql.ts file:

import { ApolloServer } from "apollo-server-micro";
import { schema } from "../../graphql/schema";
import { resolvers } from "../../graphql/resolvers";
import { createContext } from "../../graphql/context";
import Cors from "micro-cors";

const cors = Cors();

const apolloServer = new ApolloServer({
   schema,
   resolvers,
   context: createContext,
});

const startServer = apolloServer.start();

const handler = async (req, res) => {
   if (req.method === "OPTIONS") {
      res.end();
      return false;
   }

   await startServer;

   await apolloServer.createHandler({
      path: "/api/graphql",
   })(req, res);
};

export default cors(handler);

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

And my schema.ts file:

import { makeSchema } from "nexus";
import { join } from "path";
import * as types from "./types";

export const schema = makeSchema({
   types,
   outputs: {
      typegen: join(
         process.cwd(),
         "node_modules",
         "@types",
         "nexus-typegen",
         "index.d.ts"
      ),
      schema: join(process.cwd(), "graphql", "schema.graphql"),
   },
   contextType: {
      export: "Context",
      module: join(process.cwd(), "graphql", "context.ts"),
   },
});

So far I haven't been able to solve the issue nor find any reference of it online, apart from a comment under your Building a GraphQL schema using Nexus, Prisma and Nextjs video mentioning the same error message.

milovangudelj avatar Nov 07 '21 15:11 milovangudelj

Ok, so... I was using GraphQL v16.0.1 and reverting back to v15.7.2 fixed the problem.

Should I close the issue? (I'm new to the issues part of GitHub...)

milovangudelj avatar Nov 07 '21 20:11 milovangudelj

Hey! So I'm not aware of the error but I'll look into it when I update the project's dependencies. I'll keep the issue open in case someone else runs into the same issue :)

m-abdelwahab avatar Nov 09 '21 12:11 m-abdelwahab

A little update...

If you need to use a GraphQL version greater than v15.7.2 you can still do it but you will have to assert the type of schema yourself and not let the compiler infer it.

The updated schema.ts file looks like this:

import { join } from "path";
import { GraphQLSchema } from "graphql";
import { makeSchema } from "nexus";

import * as types from "./types";

const schema = makeSchema({
   types,
   outputs: {
      typegen: join(
         process.cwd(),
         "node_modules",
         "@types",
         "nexus-typegen",
         "index.d.ts"
      ),
      schema: join(process.cwd(), "graphql", "schema.graphql"),
   },
   contextType: {
      export: "Context",
      module: join(process.cwd(), "graphql", "context.ts"),
   },
}) as unknown as GraphQLSchema;

export default schema;

The important bit here is the last line, where the as keyword is used to tell the compiler that the type of schema should be GraphQLSchema and not NexusGraphQLSchema.

Note that since the two types are not directly interchangeable the expression needs to be converted to unknown first before asserting the desired type.

milovangudelj avatar Feb 23 '22 23:02 milovangudelj

Hi 👋🏽

We've updated the tutorial to use GraphQL Yoga and Pothos for the GraphQL API. Check it out and if you have any issues, let us know.

I'll go ahead and close this issue. If it persists, you can re-open the issue.

ruheni avatar Jan 17 '23 15:01 ruheni