Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate TypeScript types from GraphQL schema for modular use throughout the codebase #24

Open
mattfsourcecode opened this issue Jan 12, 2025 · 0 comments

Comments

@mattfsourcecode
Copy link
Owner

Current Situation

  • Schema types are defined in src/graphql/schema/typeDefs.ts using gql from graphql-tag.
  • Tests in src/__tests__/serverIntegration.test.ts use manually defined interfaces for response types.
  • No shared type definitions across the application, leading to inconsistencies and duplication.

Proposed Solution

Implement @graphql-codegen/cli to generate shared TypeScript types from the GraphQL schema, ensuring type safety and consistency throughout the application.


Use Cases for Generated Types:

  1. GraphQL Resolvers
    • Type-safe resolver functions with automatic parameter and return type validation.
  2. Service Layer
    • Typed data manipulation and transformation operations.
  3. Database Layer
    • Type alignment between the GraphQL schema and database models for safer queries and updates.
  4. Middleware
    • Typed request/response handling and structured error scenarios.
  5. Integration Testing
    • Type-safe test assertions and mock data generation.
  6. Client-Side Code (if applicable)
    • Shared types for frontend API calls, aligning client and server logic.

Benefits

  • Establishes a single source of truth for types across the application.
  • Automatically generates and updates types from the GraphQL schema.
  • Enhances IDE support with autocompletion and type safety.
  • Reduces maintenance by eliminating manual type definitions.
  • Facilitates easier refactoring with compile-time error detection.
  • Improves developer productivity and reduces type-related bugs.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant