Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS error when using graphql-yoga plugin #5

Open
datsfilipe opened this issue Oct 7, 2023 · 0 comments
Open

TS error when using graphql-yoga plugin #5

datsfilipe opened this issue Oct 7, 2023 · 0 comments

Comments

@datsfilipe
Copy link

datsfilipe commented Oct 7, 2023

Context

I've updated my dependencies and the error started happening.

My package.json:

{
  "name": "server",
  "version": "1.0.50",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "bun run --watch src/index.ts"
  },
  "dependencies": {
    "@elysiajs/cors": "^0.7.1",
    "@elysiajs/graphql-yoga": "^0.6.3",
    "@elysiajs/swagger": "^0.7.3",
    "elysia": "latest"
  },
  "devDependencies": {
    "bun-types": "latest",
    "eslint-config-standard-with-typescript": "latest",
    "eslint-plugin-import": "^2.25.2",
    "eslint-plugin-n": "^15.0.0 || ^16.0.0",
    "eslint-plugin-promise": "^6.0.0"
  },
  "module": "src/index.js"
}

Description

I think my code is following the plugin usage examples and, before the update, it was working without errors.

I'm getting this error in terminal when running the server:

[0] 38 |               ? void 0
[0] 39 |               : _value$constructor.name;
[0] 40 |
[0] 41 |           if (className === valueClassName) {
[0] 42 |             const stringifiedValue = (0, _inspect.inspect)(value);
[0] 43 |             throw new Error(`Cannot use ${className} "${stringifiedValue}" from another module or realm.
[0]                       ^
[0] error: Cannot use GraphQLObjectType "User" from another module or realm.

It says me to verify my node_modules for duplicate graphql instances, I have only one:

> ls
...
drwxr-xr-x - dtsf dtsf  7 Oct 11:01 graphql
drwxr-xr-x - dtsf dtsf  7 Oct 11:01 graphql-mobius
drwxr-xr-x - dtsf dtsf  7 Oct 11:01 graphql-yoga

The code

const app = new Elysia()
  .use(
    yoga({
      typeDefs: /* GraphQL */`
        type User {
          id: ID!
          name: String!
          email: String!
          banned: Boolean!
        }
        
        type Query {
          user(id: ID): [User]
          createUser(name: String!, email: String!): User
          banUser(id: ID!): User
        }
      `,
      resolvers: {
        Query: {
          user: (_, args) => {
            if (args?.id) {
              const query = db.query("SELECT * FROM users WHERE id = $id")
              const result = query.all({ $id: args.id })
              return result as User[]
            } else {
              const query = db.query("SELECT * FROM users")
              const result = query.all()
              return result as User[]
            }
          },
          createUser: (_, args) => {
            const id = Math.random().toString(36).substring(7)
            const user = { id, name: args.name, email: args.email, banned: false }
            const query = db.prepare("INSERT INTO users (id, name, email, banned) VALUES ($id, $name, $email, $banned)")
            query.run({
              $id: user.id,
              $name: user.name,
              $email: user.email,
              $banned: user.banned
            })
            return user
          },
          banUser: (_, args) => {
            const user = db.query("SELECT * FROM users WHERE id = $id").get({ $id: args.id }) as User | undefined
            if (!user) return null

            const query = db.prepare("UPDATE users SET banned = $banned WHERE id = $id")
            if (user.banned) query.run({ $id: args.id, $banned: false })
            else query.run({ $id: args.id, $banned: true })

            return {
              ...user,
              banned: !user.banned
            }
          }
        }
      }
    })
  )

The no overload matches this call ts error:

The last overload gave the following error:
 Argument of type '(app: Elysia) => Elysia<"", { request: {}; store: {}; schema: {}; error: {}; meta: Record<"defs", {}> & Record<"exposed", {}> & Record<"schema", { [x: string]: { get: { body: unknown; headers: undefined; query: undefined; params: undefined; response: { ...; }; }; } & { ...; }; }>; }>' is not assignable to parameter of type 'Promise<{ default: Elysia<any, any, any, any, any, any>; }>'.

Observation:

I'm not very good at reading those types. I don't know if I did anything wrong here. Sorry to bother if it's the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant