-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
134 lines (118 loc) · 3.71 KB
/
server.ts
File metadata and controls
134 lines (118 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Elysia } from 'elysia'
import { node } from '@elysiajs/node' // Node.js adapter for Elysia
import { html } from '@elysiajs/html' // Middleware to serve HTML easily
import { graphql, buildSchema } from 'graphql'
// ---------------------------
// GraphQL Schema
// ---------------------------
// Define GraphQL types, queries, and mutations using SDL (Schema Definition Language)
const schema = buildSchema(`
type User {
id: ID!
name: String!
email: String!
}
type Message {
message: String!
timestamp: String!
}
type Query {
hello: String!
users: [User!]!
user(id: ID!): User
}
type Mutation {
sendMessage(message: String!): Message!
}
`)
// ---------------------------
// In-memory database
// ---------------------------
// Simple array to simulate a database for demo purposes
const USERS = [
{ id: '1', name: 'Alice', email: 'alice@example.com' },
{ id: '2', name: 'Bob', email: 'bob@example.com' },
{ id: '3', name: 'Carol', email: 'carol@example.com' },
]
// ---------------------------
// Root resolvers
// ---------------------------
// Define functions to handle GraphQL queries and mutations
const root = {
hello: () => 'Hello from GraphQL!',
users: () => USERS,
user: ({ id }: { id: string }) => USERS.find(u => u.id === id),
sendMessage: ({ message }: { message: string }) => ({
message,
timestamp: new Date().toISOString(),
}),
}
// ---------------------------
// GraphiQL HTML
// ---------------------------
// Embedded GraphiQL UI to test GraphQL queries in-browser
const graphiqlHTML = `
<!DOCTYPE html>
<html>
<head>
<title>Elysia GraphQL Server</title>
<link href="https://unpkg.com/graphiql@2.2.0/graphiql.min.css" rel="stylesheet" />
</head>
<body style="margin:0; height:100vh;">
<div id="graphiql" style="height:100vh;"></div>
<script src="https://unpkg.com/react@18.2.0/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@18.2.0/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/graphiql@2.2.0/graphiql.min.js"></script>
<script>
// GraphQL fetcher used by the GraphiQL interface
const graphQLFetcher = graphQLParams =>
fetch('/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(graphQLParams)
}).then(r => r.json());
// Render GraphiQL UI
ReactDOM.render(
React.createElement(GraphiQL, { fetcher: graphQLFetcher }),
document.getElementById('graphiql')
);
</script>
</body>
</html>
`
// ---------------------------
// Create Elysia app with Node adapter
// ---------------------------
// `adapter: node()` makes Elysia run on Node.js runtime
// `.use(html())` enables easy serving of HTML responses
const app = new Elysia({ adapter: node() })
.use(html())
// ---------------------------
// Routes
// ---------------------------
// GET / → Serve GraphiQL UI
app.get('/', () => graphiqlHTML)
// POST /graphql → Execute GraphQL queries
// - `body` is automatically parsed JSON
// - TypeScript casting ensures proper typing
app.post('/graphql', async ({ body }) => {
const { query, variables } = body as { query: string; variables?: Record<string, unknown> }
// Execute GraphQL query using schema and resolvers
const result = await graphql({
schema,
source: query,
rootValue: root,
variableValues: variables,
})
return result
})
// ---------------------------
// Port configuration
// ---------------------------
const PORT = process.env.PORT ? Number(process.env.PORT) : 8080
// ---------------------------
// Start server
// ---------------------------
app.listen(PORT, () => {
console.log(`🚀 Elysia GraphQL server running at http://localhost:${PORT}/`)
})