maticzav / graphql-shield

🛡 A GraphQL tool to ease the creation of permission layer.
https://graphql-shield.com
MIT License
3.56k stars 172 forks source link

error - ReferenceError: Cannot access 'nexusSchema' before initialization #1404

Open odm275 opened 2 years ago

odm275 commented 2 years ago

Bug report

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);

2. 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 } };


4. This is the error I see

```ts
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
open-collective-bot[bot] commented 2 years ago

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: