Skip to content

Commit 68d84e2

Browse files
docs: add examples for using generated TypeScript types in client and server code
1 parent cc380c1 commit 68d84e2

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,52 @@ The module automatically generates TypeScript types for you:
336336

337337
Your IDE will automatically provide type safety and autocomplete!
338338

339+
#### Using Generated Types
340+
341+
You can import and use the generated types in your code:
342+
343+
**Client-side types** (`#graphql/client`):
344+
```ts
345+
// Import generated types for queries, mutations, and operations
346+
import type { GetUsersQuery, CreateUserInput } from '#graphql/client'
347+
348+
// Use in Vue components
349+
const users = ref<GetUsersQuery['users']>([])
350+
351+
// Use in composables
352+
export function useUsers() {
353+
const createUser = async (input: CreateUserInput) => {
354+
// Type-safe input
355+
}
356+
}
357+
```
358+
359+
**Server-side types** (`#graphql/server`):
360+
```ts
361+
// Import generated types and interfaces
362+
import type { User, Post, CreateUserInput } from '#graphql/server'
363+
364+
// Use types in your server code
365+
function validateUser(user: User): boolean {
366+
return user.email.includes('@')
367+
}
368+
369+
// Use in data layer
370+
async function getUserPosts(user: User): Promise<Post[]> {
371+
// user is fully typed with all fields
372+
return await db.posts.findMany({ where: { authorId: user.id } })
373+
}
374+
375+
// Use input types for validation
376+
function validateCreateUserInput(input: CreateUserInput): void {
377+
if (!input.email || !input.name) {
378+
throw new Error('Email and name are required')
379+
}
380+
}
381+
```
382+
383+
These imports provide full TypeScript support with autocompletion, type checking, and IntelliSense in your IDE.
384+
339385
> [!TIP]
340386
> **Nitro Auto-Imports**: Thanks to Nitro's auto-import feature, you don't need to manually import `defineResolver`, `defineQuery`, `defineMutation`, and other utilities in your resolver files. They're available globally! However, if you prefer explicit imports, you can use:
341387
> ```ts

0 commit comments

Comments
 (0)