navis.js 5.4.0 → 5.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -437,6 +437,7 @@ exports.handler = async (event, context) => {
437
437
 
438
438
  ### GraphQL Support (v5.4)
439
439
 
440
+ **JavaScript Example:**
440
441
  ```javascript
441
442
  const { NavisApp, graphql, createResolver } = require('navis.js');
442
443
 
@@ -478,6 +479,56 @@ app.use(graphql({
478
479
  app.listen(3000);
479
480
  ```
480
481
 
482
+ **TypeScript Example:**
483
+ ```typescript
484
+ import {
485
+ NavisApp,
486
+ graphql,
487
+ createResolver,
488
+ GraphQLContext,
489
+ } from 'navis.js';
490
+
491
+ interface User {
492
+ id: string;
493
+ name: string;
494
+ email: string;
495
+ }
496
+
497
+ const app = new NavisApp();
498
+
499
+ // Define resolvers with TypeScript types
500
+ const resolvers = {
501
+ Query: {
502
+ users: createResolver<User[]>(async (variables, context: GraphQLContext) => {
503
+ return [{ id: '1', name: 'Alice', email: 'alice@example.com' }];
504
+ }),
505
+
506
+ user: createResolver<User | null>(async (variables, context: GraphQLContext) => {
507
+ const { id } = variables as { id: string };
508
+ return { id, name: 'Alice', email: 'alice@example.com' };
509
+ }),
510
+ },
511
+
512
+ Mutation: {
513
+ createUser: createResolver<User>(async (variables, context: GraphQLContext) => {
514
+ const { name, email } = variables as { name: string; email: string };
515
+ return { id: '3', name, email };
516
+ }),
517
+ },
518
+ };
519
+
520
+ // Add GraphQL middleware
521
+ app.use(graphql({
522
+ path: '/graphql',
523
+ resolvers,
524
+ context: (req) => ({
525
+ userId: (req.headers['x-user-id'] as string) || null,
526
+ }),
527
+ }));
528
+
529
+ app.listen(3000);
530
+ ```
531
+
481
532
  **GraphQL Query Example:**
482
533
  ```bash
483
534
  curl -X POST http://localhost:3000/graphql \
@@ -510,7 +561,8 @@ See the `examples/` directory:
510
561
  - `v5-features-demo.js` - v5 features demonstration (caching, CORS, security, compression, health checks, etc.)
511
562
  - `v5.1-features-demo.js` - v5.1 features demonstration (Swagger, versioning, upload, testing)
512
563
  - `v5.2-features-demo.js` - v5.2 features demonstration (WebSocket, SSE, database)
513
- - `graphql-demo.js` - GraphQL server example with queries and mutations (v5.4)
564
+ - `graphql-demo.js` - GraphQL server example with queries and mutations (v5.4) - JavaScript
565
+ - `graphql-demo.ts` - GraphQL server example with TypeScript types (v5.4) - TypeScript
514
566
  - `service-client-demo.js` - ServiceClient usage example
515
567
 
516
568
  ## Roadmap
@@ -0,0 +1,158 @@
1
+ /**
2
+ * GraphQL Demo - Navis.js (TypeScript)
3
+ * Demonstrates GraphQL query and mutation support with TypeScript
4
+ */
5
+
6
+ import {
7
+ NavisApp,
8
+ graphql,
9
+ createResolver,
10
+ GraphQLResolver,
11
+ GraphQLContext,
12
+ response,
13
+ } from '../src/index';
14
+
15
+ interface User {
16
+ id: string;
17
+ name: string;
18
+ email: string;
19
+ age?: number;
20
+ }
21
+
22
+ interface Post {
23
+ id: string;
24
+ title: string;
25
+ content: string;
26
+ authorId: string;
27
+ }
28
+
29
+ // Sample data store
30
+ const users: User[] = [
31
+ { id: '1', name: 'Alice', email: 'alice@example.com', age: 30 },
32
+ { id: '2', name: 'Bob', email: 'bob@example.com', age: 25 },
33
+ { id: '3', name: 'Charlie', email: 'charlie@example.com', age: 35 },
34
+ ];
35
+
36
+ const posts: Post[] = [
37
+ { id: '1', title: 'First Post', content: 'Content 1', authorId: '1' },
38
+ { id: '2', title: 'Second Post', content: 'Content 2', authorId: '2' },
39
+ { id: '3', title: 'Third Post', content: 'Content 3', authorId: '1' },
40
+ ];
41
+
42
+ // Define resolvers with TypeScript types
43
+ const resolvers = {
44
+ Query: {
45
+ // Get all users
46
+ users: createResolver(async (variables, context: GraphQLContext): Promise<User[]> => {
47
+ return users;
48
+ }),
49
+
50
+ // Get user by ID
51
+ user: createResolver(async (variables, context: GraphQLContext): Promise<User | null> => {
52
+ const { id } = variables as { id: string };
53
+ const user = users.find(u => u.id === id);
54
+ if (!user) {
55
+ throw new Error(`User with id ${id} not found`);
56
+ }
57
+ return user;
58
+ }),
59
+
60
+ // Get all posts
61
+ posts: createResolver(async (variables, context: GraphQLContext): Promise<Post[]> => {
62
+ return posts;
63
+ }),
64
+
65
+ // Get posts by author
66
+ postsByAuthor: createResolver(async (variables, context: GraphQLContext): Promise<Post[]> => {
67
+ const { authorId } = variables as { authorId: string };
68
+ return posts.filter(p => p.authorId === authorId);
69
+ }),
70
+ },
71
+
72
+ Mutation: {
73
+ // Create user
74
+ createUser: createResolver(async (variables, context: GraphQLContext): Promise<User> => {
75
+ const { name, email, age } = variables as { name: string; email: string; age?: number };
76
+ const newUser: User = {
77
+ id: String(users.length + 1),
78
+ name,
79
+ email,
80
+ age: age || 0,
81
+ };
82
+ users.push(newUser);
83
+ return newUser;
84
+ }),
85
+
86
+ // Update user
87
+ updateUser: createResolver(async (variables, context: GraphQLContext): Promise<User> => {
88
+ const { id, name, email, age } = variables as {
89
+ id: string;
90
+ name?: string;
91
+ email?: string;
92
+ age?: number;
93
+ };
94
+ const user = users.find(u => u.id === id);
95
+ if (!user) {
96
+ throw new Error(`User with id ${id} not found`);
97
+ }
98
+ if (name) user.name = name;
99
+ if (email) user.email = email;
100
+ if (age !== undefined) user.age = age;
101
+ return user;
102
+ }),
103
+
104
+ // Delete user
105
+ deleteUser: createResolver(async (variables, context: GraphQLContext): Promise<User> => {
106
+ const { id } = variables as { id: string };
107
+ const index = users.findIndex(u => u.id === id);
108
+ if (index === -1) {
109
+ throw new Error(`User with id ${id} not found`);
110
+ }
111
+ const deleted = users.splice(index, 1)[0];
112
+ return deleted;
113
+ }),
114
+ },
115
+ };
116
+
117
+ const app = new NavisApp();
118
+
119
+ // Add GraphQL middleware with TypeScript types
120
+ app.use(
121
+ graphql({
122
+ path: '/graphql',
123
+ resolvers,
124
+ context: (req) => {
125
+ // Add custom context with TypeScript types
126
+ return {
127
+ userId: (req.headers['x-user-id'] as string) || null,
128
+ timestamp: new Date().toISOString(),
129
+ };
130
+ },
131
+ })
132
+ );
133
+
134
+ // Health check endpoint
135
+ app.get('/health', (req, res) => {
136
+ response.success(res, { status: 'ok', graphql: true, typescript: true });
137
+ });
138
+
139
+ // Start server
140
+ const PORT = process.env.PORT || 3000;
141
+ app.listen(PORT, () => {
142
+ console.log(`šŸš€ Navis.js GraphQL server (TypeScript) running on http://localhost:${PORT}`);
143
+ console.log(`šŸ“Š GraphQL endpoint: http://localhost:${PORT}/graphql`);
144
+ console.log('\nšŸ“ Example queries:');
145
+ console.log('\n1. Get all users:');
146
+ console.log(`curl -X POST http://localhost:${PORT}/graphql \\`);
147
+ console.log(` -H "Content-Type: application/json" \\`);
148
+ console.log(` -d '{"query": "query { users { id name email age } }"}'`);
149
+ console.log('\n2. Get user by ID:');
150
+ console.log(`curl -X POST http://localhost:${PORT}/graphql \\`);
151
+ console.log(` -H "Content-Type: application/json" \\`);
152
+ console.log(` -d '{"query": "query { user(id: \\"1\\") { id name email } }"}'`);
153
+ console.log('\n3. Create user:');
154
+ console.log(`curl -X POST http://localhost:${PORT}/graphql \\`);
155
+ console.log(` -H "Content-Type: application/json" \\`);
156
+ console.log(` -d '{"query": "mutation { createUser(name: \\"Dave\\", email: \\"dave@example.com\\", age: 28) { id name email } }"}'`);
157
+ });
158
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "navis.js",
3
- "version": "5.4.0",
3
+ "version": "5.4.1",
4
4
  "description": "A lightweight, serverless-first, microservice API framework designed for AWS Lambda and Node.js",
5
5
  "main": "src/index.js",
6
6
  "types": "types/index.d.ts",
package/types/index.d.ts CHANGED
@@ -807,7 +807,7 @@ export function createGraphQLServer(options?: GraphQLServerOptions): GraphQLServ
807
807
  export function graphql(options?: GraphQLServerOptions): Middleware;
808
808
  export function createSchema(): GraphQLSchema;
809
809
  export function type(name: string, definition: any): GraphQLSchema;
810
- export function createResolver(resolverFn: GraphQLResolver, options?: GraphQLResolverOptions): GraphQLResolver;
810
+ export function createResolver<T = any>(resolverFn: GraphQLResolver, options?: GraphQLResolverOptions): GraphQLResolver;
811
811
  export function fieldResolver(fieldName: string, resolverFn: GraphQLResolver): Record<string, GraphQLResolver>;
812
812
  export function combineResolvers(...resolvers: any[]): any;
813
813
  export function createAsyncResolver(resolverFn: GraphQLResolver, options?: GraphQLAsyncResolverOptions): GraphQLResolver;