Skip to content

Experimenting with GraphQL, FastEndpoints and RulesEngine

Notifications You must be signed in to change notification settings

fredimachado/ProductDetails

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Product Details

Experiments with GraphQL, FastEndpoints and RulesEngine.

ProductDetails.Api

This project is a GraphQL API that exposes endpoints for querying product details using using the HotChocolate library.

Fetching Product Details:

Product details can be fetched by stockcode using the following GraphQL query:

query {
  product(stockcode: "1-1") {
    description
    name
    price
    stockcode
  }
}

This will result in the following response:

{
  "data": {
    "product": {
      "description": "Laptop with Intel i5, 16Gb RAM and 1TB SSD",
      "name": "Intel Laptop",
      "price": 1900,
      "stockcode": "1-1"
    }
  }
}

Product Tags

The Product model was extended to include tags. Tags can be added to a product using the following query:

query {
  product(stockcode: "1-1") {
    description
    name
    price
    stockcode
    tags {
      category
      kind
      text
      value
    }
  }
}

Which will result in the following response:

{
  "data": {
    "product": {
      "description": "Laptop with Intel i5, 16Gb RAM and 1TB SSD",
      "name": "Intel Laptop",
      "price": 1900,
      "stockcode": "1-1",
      "tags": [
        {
          "category": "New",
          "kind": "Information",
          "text": "New",
          "value": ""
        }
      ]
    }
  }
}

DataLoader to avoid the N+1 problem

Fetching multiple products can result in the N+1 problem, which will cause multiple database queries to be executed, due to GraphQL field resolvers being atomic and not knowing about the query as a whole. This can be avoided by using a DataLoader, which will batch all requests together into one query to the database.

Data Loaders were implemented for both Product and ProductTag models.

Managing Products

The Api project also exposes REST API endpoints for managing products. Swagger UI can be used to interact with the API.

Security

The REST API is secured using JWT tokens.

In local development environment, a token can be generated using the user-jwt tool. Execute the following command from the ProductDetails.Api project directory:

dotnet user-jwts create --audience productdetails-api --audience productdetails-promotion-api --role Admin --claim "AdminId=1""

This will generate a JWT token that can be used to authenticate to both ProductDetails REST API and Promotion API.