Skip to content

Commit

Permalink
FEAT: Adding LRU Caching to GraphQL Server (#30)
Browse files Browse the repository at this point in the history
* feat: adding graphql service for accounts

* feat: adding support for accounts-graphql schema

* fix: scale machines to 0

* fix: fly.toml

* fix: internal port 8080 in production

* fix: change back to port 3000

* fix: move graph into internal

* feat: code generation for gql

* fix: models

* fix: models

* feat: resolvers are fixed

* feat: updating resolver path

* feat: move schemas into their own directory

* fix: moving models to a package

* feat: adding proper generated files package

* feat: adding apollo router locally for dev

* feat: logged todos resolver is called

* fix: empty resolvers

* fix: moving schema configs to dev

* fix: moving schema configs to dev

* feat: adding apolo router

* feat: super graph dev and prod setup

* feat: remove the custom metrics

* fix: deployment for apollo router

* fix: delete older files

* feat: updating dockerfile for router

* fix: remove homepage

* fix: disable homepage

* fix: disable homepage

* fix: router config

* fix: config

* fix: apollo router config

* fix: dockerfile

* fix: dockerfile slashes

* fix: apollo router prod

* fix: prod

* fix: http service for accounts graphql

* fix: revert back to public prod url

* fix: graphql schema

* feat: account model generation

* feat: update prod graph

* fix: use internal routing

* feat: adding accounts internal

* feat: hide accounts-graphql behind VPC

* feat: graphql service generator

* feat: cleanup graphql files

* fix: file generation

* fix: further cleanup

* fix: remove unwanted package in go workspace

* feat: calling accounts api from accounts graph

* feat: updating supergraph schema with prod

* refactor: change name to account_service handler

* feat: adding uuid validation for account

* fix: models to query by commonID

* feat: abstracting lru cache and other caches to their own package
  • Loading branch information
ericzorn93 authored Jan 3, 2025
1 parent 52b27e0 commit 87e0b62
Show file tree
Hide file tree
Showing 17 changed files with 186 additions and 64 deletions.
23 changes: 19 additions & 4 deletions apps/services/accounts-graphql/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"apps/services/accounts-graphql/internal/graph/resolvers"
"context"
"errors"
"libs/backend/cache"
"log"
"log/slog"
"os"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/vektah/gqlparser/v2/ast"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"

Expand Down Expand Up @@ -84,13 +86,26 @@ func run() error {
srv.AddTransport(transport.Options{})
srv.AddTransport(transport.GET{})
srv.AddTransport(transport.POST{})
// srv.SetQueryCache(lru.New[*ast.QueryDocument](1000))

// Create caches
queryCache, err := cache.NewLRUCache[*ast.QueryDocument](1000, params.Logger)
if err != nil {
params.Logger.Error("Cannot create query cache", slog.Any("error", err))
return err
}

automaticPersistedQueryCache, err := cache.NewLRUCache[string](100, params.Logger)
if err != nil {
params.Logger.Error("Cannot create persisted query cache", slog.Any("error", err))
return err
}

// Middleware
srv.Use(extension.Introspection{})
// srv.Use(extension.AutomaticPersistedQuery{
// Cache: lru.New[string](100),
// })
srv.SetQueryCache(queryCache)
srv.Use(extension.AutomaticPersistedQuery{
Cache: automaticPersistedQueryCache,
})

params.Mux.Handle("/", playground.Handler("GraphQL playground", "/graphql"))
params.Mux.Handle("/graphql", srv)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""
The Account type represents a user account in the system.
"""
type Account {
"""
The unique identifier for the account
Expand All @@ -18,5 +21,13 @@ type Account {
}

extend type Viewer {
account(id: ID!): Account @goField(forceResolver: true)
"""
Get an account by its unique identifier
"""
account(
"""
The unique identifier of the account
"""
commonID: UUID!
): Account @goField(forceResolver: true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ type Query {
"""
Viewer is the root query object for the user
"""
viewer: Viewer!
viewer: Viewer
}

type Mutation {
empty: Boolean!
}

"""
Viewer is the root query object for the user
"""
type Viewer {
empty: Boolean!
}
10 changes: 8 additions & 2 deletions apps/services/apollo-router/supergraph-prod.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ directive @join__owner(graph: join__Graph!) on INTERFACE | OBJECT

directive @join__type(graph: join__Graph!, key: join__FieldSet) repeatable on INTERFACE | OBJECT

"""The Account type represents a user account in the system."""
type Account {
"""The createdAt time of the account"""
createdAt: Time!
Expand All @@ -38,15 +39,20 @@ type Query {
empty: Boolean! @join__field(graph: ACCOUNTS)

"""Viewer is the root query object for the user"""
viewer: Viewer! @join__field(graph: ACCOUNTS)
viewer: Viewer @join__field(graph: ACCOUNTS)
}

scalar Time

scalar UUID

"""Viewer is the root query object for the user"""
type Viewer {
account(id: ID!): Account @join__field(graph: ACCOUNTS)
"""Get an account by its unique identifier"""
account(
"""The unique identifier of the account"""
commonID: UUID!
): Account @join__field(graph: ACCOUNTS)
empty: Boolean!
}

Expand Down
3 changes: 2 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ go 1.23

use (
./apps/services/accounts-api
./apps/services/accounts-worker
./apps/services/accounts-graphql
./apps/services/accounts-worker
./apps/services/inbound-webhooks-api
./libs/backend/boot
./libs/backend/cache
./libs/backend/domain/user
./libs/backend/eventing
./libs/backend/proto-gen/go
Expand Down
10 changes: 8 additions & 2 deletions graphql/supergraph-dev.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ directive @join__owner(graph: join__Graph!) on INTERFACE | OBJECT

directive @join__type(graph: join__Graph!, key: join__FieldSet) repeatable on INTERFACE | OBJECT

"""The Account type represents a user account in the system."""
type Account {
"""The createdAt time of the account"""
createdAt: Time!
Expand All @@ -38,15 +39,20 @@ type Query {
empty: Boolean! @join__field(graph: ACCOUNTS)

"""Viewer is the root query object for the user"""
viewer: Viewer! @join__field(graph: ACCOUNTS)
viewer: Viewer @join__field(graph: ACCOUNTS)
}

scalar Time

scalar UUID

"""Viewer is the root query object for the user"""
type Viewer {
account(id: ID!): Account @join__field(graph: ACCOUNTS)
"""Get an account by its unique identifier"""
account(
"""The unique identifier of the account"""
commonID: UUID!
): Account @join__field(graph: ACCOUNTS)
empty: Boolean!
}

Expand Down
5 changes: 5 additions & 0 deletions libs/backend/cache/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module libs/backend/cache

go 1.23

require github.com/hashicorp/golang-lru v1.0.2
2 changes: 2 additions & 0 deletions libs/backend/cache/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c=
github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
Loading

0 comments on commit 87e0b62

Please sign in to comment.