prisma-examples icon indicating copy to clipboard operation
prisma-examples copied to clipboard

[javascript/graphql-apollo-server]: Unable to start example

Open brianbancroft opened this issue 5 years ago • 10 comments

Hi there,

When I attempt to run this pared example as per this set of directions, I run into the following error in the command line:

  Missing required typegen import: Context

This also manifests in the GraphQL Playground when I attempt to run any query through the following:

Expand me for graphql response

{
  "error": {
    "errors": [
      {
        "message": "Cannot query field \"users\" on type \"Query\".",
        "locations": [
          {
            "line": 2,
            "column": 3
          }
        ],
        "extensions": {
          "code": "GRAPHQL_VALIDATION_FAILED",
          "exception": {
            "stacktrace": [
              "GraphQLError: Cannot query field \"users\" on type \"Query\".",
              "    at Object.Field (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/validation/rules/FieldsOnCorrectType.js:53:31)",
              "    at Object.enter (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/language/visitor.js:324:29)",
              "    at Object.enter (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/language/visitor.js:375:25)",
              "    at visit (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/language/visitor.js:242:26)",
              "    at Object.validate (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/validation/validate.js:73:24)",
              "    at validate (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/apollo-server-core/dist/requestPipeline.js:213:34)",
              "    at Object.<anonymous> (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/apollo-server-core/dist/requestPipeline.js:119:42)",
              "    at Generator.next (<anonymous>)",
              "    at fulfilled (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/apollo-server-core/dist/requestPipeline.js:5:58)",
              "    at process._tickCallback (internal/process/next_tick.js:68:7)"
            ]
          }
        }
      },
      {
        "message": "Cannot query field \"user\" on type \"Query\".",
        "locations": [
          {
            "line": 9,
            "column": 3
          }
        ],
        "extensions": {
          "code": "GRAPHQL_VALIDATION_FAILED",
          "exception": {
            "stacktrace": [
              "GraphQLError: Cannot query field \"user\" on type \"Query\".",
              "    at Object.Field (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/validation/rules/FieldsOnCorrectType.js:53:31)",
              "    at Object.enter (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/language/visitor.js:324:29)",
              "    at Object.enter (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/language/visitor.js:375:25)",
              "    at visit (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/language/visitor.js:242:26)",
              "    at Object.validate (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/validation/validate.js:73:24)",
              "    at validate (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/apollo-server-core/dist/requestPipeline.js:213:34)",
              "    at Object.<anonymous> (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/apollo-server-core/dist/requestPipeline.js:119:42)",
              "    at Generator.next (<anonymous>)",
              "    at fulfilled (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/apollo-server-core/dist/requestPipeline.js:5:58)",
              "    at process._tickCallback (internal/process/next_tick.js:68:7)"
            ]
          }
        }
      },
      {
        "message": "Cannot query field \"createUser\" on type \"Mutation\". Did you mean \"createDraft\"?",
        "locations": [
          {
            "line": 17,
            "column": 3
          }
        ],
        "extensions": {
          "code": "GRAPHQL_VALIDATION_FAILED",
          "exception": {
            "stacktrace": [
              "GraphQLError: Cannot query field \"createUser\" on type \"Mutation\". Did you mean \"createDraft\"?",
              "    at Object.Field (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/validation/rules/FieldsOnCorrectType.js:53:31)",
              "    at Object.enter (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/language/visitor.js:324:29)",
              "    at Object.enter (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/language/visitor.js:375:25)",
              "    at visit (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/language/visitor.js:242:26)",
              "    at Object.validate (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/validation/validate.js:73:24)",
              "    at validate (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/apollo-server-core/dist/requestPipeline.js:213:34)",
              "    at Object.<anonymous> (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/apollo-server-core/dist/requestPipeline.js:119:42)",
              "    at Generator.next (<anonymous>)",
              "    at fulfilled (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/apollo-server-core/dist/requestPipeline.js:5:58)",
              "    at process._tickCallback (internal/process/next_tick.js:68:7)"
            ]
          }
        }
      },
      {
        "message": "Cannot query field \"updateUser\" on type \"Mutation\".",
        "locations": [
          {
            "line": 25,
            "column": 3
          }
        ],
        "extensions": {
          "code": "GRAPHQL_VALIDATION_FAILED",
          "exception": {
            "stacktrace": [
              "GraphQLError: Cannot query field \"updateUser\" on type \"Mutation\".",
              "    at Object.Field (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/validation/rules/FieldsOnCorrectType.js:53:31)",
              "    at Object.enter (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/language/visitor.js:324:29)",
              "    at Object.enter (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/language/visitor.js:375:25)",
              "    at visit (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/language/visitor.js:242:26)",
              "    at Object.validate (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/validation/validate.js:73:24)",
              "    at validate (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/apollo-server-core/dist/requestPipeline.js:213:34)",
              "    at Object.<anonymous> (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/apollo-server-core/dist/requestPipeline.js:119:42)",
              "    at Generator.next (<anonymous>)",
              "    at fulfilled (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/apollo-server-core/dist/requestPipeline.js:5:58)",
              "    at process._tickCallback (internal/process/next_tick.js:68:7)"
            ]
          }
        }
      },
      {
        "message": "Cannot query field \"deleteUser\" on type \"Mutation\". Did you mean \"deleteOnePost\"?",
        "locations": [
          {
            "line": 33,
            "column": 3
          }
        ],
        "extensions": {
          "code": "GRAPHQL_VALIDATION_FAILED",
          "exception": {
            "stacktrace": [
              "GraphQLError: Cannot query field \"deleteUser\" on type \"Mutation\". Did you mean \"deleteOnePost\"?",
              "    at Object.Field (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/validation/rules/FieldsOnCorrectType.js:53:31)",
              "    at Object.enter (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/language/visitor.js:324:29)",
              "    at Object.enter (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/language/visitor.js:375:25)",
              "    at visit (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/language/visitor.js:242:26)",
              "    at Object.validate (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/graphql/validation/validate.js:73:24)",
              "    at validate (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/apollo-server-core/dist/requestPipeline.js:213:34)",
              "    at Object.<anonymous> (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/apollo-server-core/dist/requestPipeline.js:119:42)",
              "    at Generator.next (<anonymous>)",
              "    at fulfilled (/Users/brianbancroft/programming/side-hustle/impressipies/graphql-apollo-server-api/node_modules/apollo-server-core/dist/requestPipeline.js:5:58)",
              "    at process._tickCallback (internal/process/next_tick.js:68:7)"
            ]
          }
        }
      }
    ]
  }
}

Other pertinent details:

This was attempted with the latest LTS versions of Node, 12.16.2 and 10.20.0

brianbancroft avatar Apr 18 '20 03:04 brianbancroft

I run into the same error (Missing required typegen import: Context) on Node 14.0.0
The error message is preceded by another that might contain useful information:

> node dist/server

GraphQL Nexus Typegen: Expected module /data/dist/context.js to be an absolute path to a TypeScript module, skipping.

      🚀 Server ready at: http://localhost:3001
      ⭐️ See sample queries: http://pris.ly/e/ts/graphql-apollo-server#using-the-graphql-api
      
Missing required typegen import: Context

HendrikJan avatar Apr 24 '20 10:04 HendrikJan

In case you all haven't already fixed this, you can resolve the issue by renaming context.js to context.ts (you may need to support typescript imports in your main app)

Benjaminryejones avatar May 13 '20 08:05 Benjaminryejones

@Benjaminryejones Thank you for the reply.

When I run npm run build the code runs without an error.
This is in line with your comment, because there is a file named "context.ts" in the /src folder.
This then generates a "context.js" file in the /dist folder.

When I then fun npm run start (which runs node dist/server), that is when the error appears.
It does not feel right to rename this file to "context.ts" at this point.

I guess the problem is in the schema.ts file at this piece of code:

typegenAutoConfig: {
    contextType: 'Context.Context',
    sources: [
      {
        source: '@prisma/client',
        alias: 'prisma',
      },
      {
        source: require.resolve('./context'), // <-- problem here, I guess
        alias: 'Context',
      },
    ],
  },

Is there a way to make npm run start (node dist/server) to run without the error?

HendrikJan avatar May 14 '20 19:05 HendrikJan

Facing the Same Problem. Nexus Schema Typegen: Expected module dist\context.js to be an absolute path to a TypeScript module, skipping. Server started, listening on port 4444 for incoming requests. Missing required typegen import: Context

imran-ib avatar Jul 31 '20 06:07 imran-ib

I am Not Sure Weather its good or bad but i fixed by removing Context type and source from schema.ts Before typegenAutoConfig: { contextType: 'Context.Context', sources: [ { source: '@prisma/client', alias: 'prisma', }, { source: require.resolve('./context'), alias: 'Context', }, ], },

after typegenAutoConfig: { sources: [ { source: '@prisma/client', alias: 'prisma', }, ], },

imran-ib avatar Jul 31 '20 07:07 imran-ib

I'm having this same issue using the nexus / prisma / apollo template.

kierangilliam avatar Aug 18 '20 06:08 kierangilliam

In case you all haven't already fixed this, you can resolve the issue by renaming context.js to context.ts (you may need to support typescript imports in your main app)

This doesn't seem to be the right approach. Prisma should theoretically support both .js and .ts and should be language agnostic. The right approach would be to fix the code to accept both .js and .ts files.

jamesguan avatar Oct 11 '20 16:10 jamesguan

@jamesguan The problem is that the Context file should be the ts type definition for the context. This enables auto completion and code checking, which makes life much easier. It's completely worth going through the effort to write the server in TS.

fullStackDataSolutions avatar Oct 20 '20 18:10 fullStackDataSolutions

Make a file:

// context.d.ts
export type Context = {}

And make typegen point to it instead:

// change...
// source: require.resolve('./context'),
// to...
source: require.resolve('./context.d.ts'),

ibash avatar Oct 26 '20 00:10 ibash

Instead of creating a new file, you can just point to the correct folder using:

source: `${__dirname.replace(/\/dist$/, '/src')}/context.ts`,

chrisbull avatar Nov 16 '20 13:11 chrisbull

Hey, we've made a few updates to the example which should fix this. Feel free to re-open the issue if you're still running into it. :)

ruheni avatar Feb 01 '23 14:02 ruheni