Giter VIP home page Giter VIP logo

elysia-graphql-yoga's People

Contributors

bogeychan avatar renhyl avatar saltyaom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

elysia-graphql-yoga's Issues

Yoga options does not accept schema or yoga property

Example from: https://www.npmjs.com/package/@elysiajs/graphql-yoga

import { Elysia } from 'elysia'
import { yoga } from '@elysiajs/graphql-yoga'

import { createYoga, createSchema } from 'graphql-yoga'

const app = new Elysia()
    .use(
        yoga({
           // yoga property doesn't exist
            yoga: createYoga({
                schema: createSchema({
                    typeDefs: `
                        type Query {
                            hi: String
                        }
                `,
                resolvers: {
                    Query: {
                        hi: () => 'Hi from Elysia'
                    }
                }
            })
        })
    )
    .listen(8080)

Error

Object literal may only specify known properties, and 'yoga' does not exist in type 'ElysiaYogaConfig<string, MaybePromise<Record<string, unknown>> | ((initialContext: YogaInitialContext) => unknown) | undefined>'

Desired, yoga accepts schema property createSchema() or from makeExecutableSchema(), can only do typeDefs and resolvers separately.

  app.use(yoga({ schema }));

Pass response to context

Is there currently a way to pass the Response into the current context from Bun/Elysia?

I'm migrating from Node + fastify and this was possible as outlined here

With the context signature in elysia-graphql-yoga this doesn't seem possible or is there a different way?

Context extends
	| undefined
	| MaybePromise<Record<string, unknown>>
	| ((initialContext: YogaInitialContext) => MaybePromise<unknown>),

I currently use it like:

import { context } from "./myCustomContext";

elysia
    .use(
        yoga({ schema, context }),
    )
    .listen(port);

My use case is that I want to set or unset an HttpOnly cookie in a GraphQL resolver

Yoga - Type grapqhl

When using injection independence in any resolver not working, example

@Resolver()
export class UserResolver {
  constructor(private userService: UserService) {}

  @Query(() => [UserModelGQL])
  async getUsers(): Promise<UserModelGQL[]> {
    return await this.userService.getAll();
  }
}

this.userService is null !!!

Subscriptions with elysia graphql yoga don't work

I needed subscriptions in my project and in the end had to make a different server for graphql yoga because graphql-ws subscriptions wouldn't work with Elysia.

The main issues are:

  1. Elysia websocket handlers have a separate type than bun websocket handlers, so this example from the graphql ws library doesn't work out of the box: https://the-guild.dev/graphql/ws/get-started#with-bun
  2. The handler from the example for uWebSocket.js satisfies the type, but functionally also doesn't work, I think that's designed for uWebSocket servers, and not bun servers.
  3. GraphQL SSE also doesn't quite work, in all the examples for GraphQL SSE with the graphql-sse client, the url that the client connects to is /graphql/stream, a URL that elysia graphql yoga don't expose: https://the-guild.dev/graphql/sse/recipes#client-usage

Because of all these limitations, I had to literally create a separate server and then handle metrics collection, logs collection, etc. for that server again.

I think this is something important, and shouldn't be too hard to add? I'm going to attempt a PR for this @SaltyAom I will try to make the websocket handlers work first, then expose the /graphql/stream url and make it work

Related to: #11

Resolvers not working for field schema definition

TS Error

Type '() => { id: string; email: string; name: string; password: string; posts: null; }' is not assignable to type '{ id: string; email: string; name: string | null; password: string; posts: { id: string; title: string; authorId: string; content: string | null; user: ... | null; }[] | null; }'.ts(2322)

Example code:
I took looks like callbacks are wrong argument for field schema

import { Elysia } from "elysia";
import { yoga } from "@elysiajs/graphql-yoga";

const typeDefs = /* GraphQL */ `
  type User {
    id: ID!
    email: String!
    name: String
    password: String!
    posts: [Post!]
  }

  type Post {
    id: ID!
    title: String!
    authorId: String!
    content: String
    user: User
  }
`;

export const graphqlYogaPlugin = new Elysia().use(
  yoga({
    typeDefs,
    resolvers: {
      Post: {
        user: () => {
          return {
            id: "1",
            email: "test",
            name: "test",
            password: "test",
            posts: null,
          };
        },
      },
    },
  })
);

Error with missing ‘Subscription’ property in Elysia GraphQL Yoga

I am new to both Elysia and GraphQL.

I am trying to learn GraphQL with Elysia, but I am encountering an error. In my typeDef, I did not declare a Subscription, but the error message is indicating that the Subscription property is missing. Is this a regular GraphQL issue or is it specific to Elysia GraphQL Yoga?

Here is my typeDef:

// schema.ts

export const typeDefs = `
    type Query {
        authors: [Author!]!
        author(id: ID, name: String): Author!
        books(page: Int, perPage: Int): [Book!]!
        book(name: String!): Book
    }

    type Mutation {
        addBook(name: String!, author: ID!): Book!
    }

    type Author {
        id: ID!
        name: String!
        books: [Book!]!
    }
    
    type Book {
        id: ID!
        name: String!
        author: Author!
    }

And here is the error message that I am receiving:

// error

Property 'Subscription' is missing in type '{ Query: { authors: () => Promise<Record[]>; author: (_parent: unknown, args: {    id?: string;    name?: string;} | undefined) => Promise<Record>; books: (_parent: unknown, args: {    page?: number;    perPage?: number;} | undefined) => Promise<Record[]>; book: (_parent: unknown, { name }: { ...; }) => Promise<...>...' but required in type '{ Query: { authors: (parent: unknown, args: {} | null | undefined, context: { request: Request; } | { [x: string]: unknown; request: Request; } | { request: Request; }, info: unknown) => MaybePromise<...>; author: (parent: unknown, args: { ...; } | undefined, context: { ...; } | ... 1 more ... | { ...; }, info: unkn...'.

Can someone help me understand what is causing this error and how to fix it? Thank you.

Subsciption

how can use graphql-ws config for subscription the official documentation very short and have hot full details

TS error when using `graphql-yoga` plugin

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.