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

[Server]: Add GraphQL Support for Enhanced API Flexibility #6

Open
15 tasks
NightClover-code opened this issue Dec 8, 2024 · 0 comments
Open
15 tasks
Labels
enhancement New feature or request

Comments

@NightClover-code
Copy link
Owner

Description:

GraphQL can be particularly beneficial for our e-commerce application by:

  • Reducing over-fetching (clients can request exactly what they need)
  • Enabling efficient product browsing with custom field selection
  • Allowing complex queries (e.g., products with reviews, related items) in a single request
  • Improving mobile app performance through selective data fetching

Current State:

We have a REST API that requires multiple endpoints for related data. For example, to get a product with its reviews and related products requires multiple API calls.

Required Packages:

pnpm add @nestjs/graphql @nestjs/apollo graphql apollo-server-express

Example Product Type Definition:

import { ObjectType, Field, ID, Float } from '@nestjs/graphql';

@ObjectType()
export class ProductType {
  @Field(() => ID)
  id: string;

  @Field()
  name: string;

  @Field()
  description: string;

  @Field(() => Float)
  price: number;

  @Field(() => [ReviewType])
  reviews: ReviewType[];

  @Field(() => [ProductType], { nullable: true })
  relatedProducts?: ProductType[];
}

Example Query Resolver:

@Resolver(() => ProductType)
export class ProductResolver {
  constructor(private productsService: ProductsService) {}

  @Query(() => [ProductType])
  async products(
    @Args('keyword', { nullable: true }) keyword?: string,
    @Args('page', { nullable: true }) page?: number,
  ) {
    return this.productsService.findMany(keyword, page?.toString());
  }

  @ResolveField(() => [ProductType])
  async relatedProducts(@Parent() product: ProductType) {
    return this.productsService.findRelated(product.id);
  }
}

Tasks

1. Initial Setup

  • Install required packages
  • Configure GraphQL module
  • Set up Apollo playground
  • Add basic health check query
  • Document setup in README.md

2: Product GraphQL Types

  • Create Product type
  • Create Review type
  • Create input types for mutations
  • Add pagination support
  • Add field descriptions

3: Product Queries and Resolvers

  • Implement products query
  • Add single product query
  • Add top rated products query
  • Add review field resolver
  • Add related products resolver

More GraphQL tasks to be added after first implementation.

Relevant Docs

https://docs.nestjs.com/graphql/quick-start

@NightClover-code NightClover-code added the enhancement New feature or request label Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant