elysiajs / elysia-graphql-yoga Goto Github PK
View Code? Open in Web Editor NEWPlugin for Elysia for using graphql-yoga
License: MIT License
Plugin for Elysia for using graphql-yoga
License: MIT License
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 }));
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
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 !!!
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:
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
I want to be able to use the @graphql-tools to load definitions from files, or code, or just be able to merge multiple definitions and pass them as, Source, DocumentNode, GraphQLSchema, DefinitionNode etc.
Accepting only string limits us
elysia-graphql-yoga/src/index.ts
Line 91 in f47ec0e
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,
};
},
},
},
})
);
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.
how can use graphql-ws config for subscription the official documentation very short and have hot full details
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"
}
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
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.