graphql-shield icon indicating copy to clipboard operation
graphql-shield copied to clipboard

error - ReferenceError: Cannot access 'nexusSchema' before initialization

Open odm275 opened this issue 2 years ago • 1 comments

Bug report

  • [x] I have checked other issues to make sure this is not a duplicate.

Describe the bug

Getting error ReferenceError: Cannot access 'nexusSchema' before initialization on start up when running my app locally. When I remove permissions from export const nexusSchema = applyMiddleware(baseSchema, permissions); the server runs fine. Hence, it seems like the error is coming from graphql-shield.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

Repo: https://github.com/odm275/ayurveda-pom

  1. This generates my GraphQL Schema. https://github.com/odm275/ayurveda-pom/blob/main/apollo/schema.ts
import { makeSchema } from "nexus";
import { join } from "path";
import path from "path";
import { shield } from "graphql-shield";
import * as types from "./schema/index";
import { applyMiddleware } from "graphql-middleware";

export const baseSchema = makeSchema({
  types: [types],
  outputs: {
    typegen: join(
      process.cwd(),
      "node_modules",
      "@types",
      "nexus-typegen",
      "index.d.ts"
    ),
    schema: join(process.cwd(), "apollo", "schema.graphql")
  },
  contextType: {
    export: "Context",
    module: join(process.cwd(), "apollo", "createContext.ts")
  },
  sourceTypes: {
    modules: [
      {
        module: path.join(process.cwd(), "/database/types.ts"),
        alias: "db"
      }
    ]
  }
});

export const permissions = shield({
  Query: {},
  Mutation: {}
});

export const nexusSchema = applyMiddleware(baseSchema, permissions);
  1. Here is where I start Apollo Server https://github.com/odm275/ayurveda-pom/blob/main/pages/api/graphql.ts
import { ApolloServer } from "apollo-server-micro";
import { PageConfig } from "next";
import { nexusSchema } from "../../apollo/schema";
import { createContext } from "../../apollo/createContext";

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

const startServer = apolloServer.start();
export default async (req, res) => {
  res.setHeader("Access-Control-Allow-Credentials", "true");
  res.setHeader(
    "Access-Control-Allow-Origin",
    "https://studio.apollographql.com"
  );
  res.setHeader(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  if (req.method === "OPTIONS") {
    res.end();
    return false;
  }
  await startServer;

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

// // Apollo Server Micro takes care of body parsing
export const config: PageConfig = {
  api: {
    bodyParser: false
  }
};

  1. This is the error I see
error - ReferenceError: Cannot access 'nexusSchema' before initialization

Expected behavior

I'm expecting my GraphQL server to be able to run and execute its mutations and queries as normal.

Actual behaviour

Any request to the server will return in the error error - ReferenceError: Cannot access 'nexusSchema' before initialization. The error error - unhandledRejection: Error: Type generator exists in middleware but is missing in Schema. pops up sometimes too.

Additional context

Ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Loaded env from /home/oscar/Documents/dev/ayurveda-pom/.env.local
info  - Loaded env from /home/oscar/Documents/dev/ayurveda-pom/.env
info  - Disabled SWC as replacement for Babel because of custom Babel configuration ".babelrc" https://nextjs.org/docs/messages/swc-disabled
info  - Using external babel configuration from /home/oscar/Documents/dev/ayurveda-pom/.babelrc
event - compiled client and server successfully in 3.4s (926 modules)
wait  - compiling /user (client and server)...
event - compiled client and server successfully in 3s (1867 modules)
error - unhandledRejection: Error: Type generator exists in middleware but is missing in Schema.
wait  - compiling /api/graphql...
event - compiled client and server successfully in 916 ms (1898 modules)
error - ReferenceError: Cannot access 'nexusSchema' before initialization

odm275 avatar Apr 13 '22 04:04 odm275

Hey @odm275 :wave:,

Thank you for opening an issue. We will get back to you as soon as we can. Have you seen our Open Collective page? Please consider contributing financially to our project. This will help us involve more contributors and get to issues like yours faster.

https://opencollective.com/graphql-shield

We offer priority support for all financial contributors. Don't forget to add priority label once you become one! :smile: