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

refactor: use gql for integrations #695

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
8 changes: 6 additions & 2 deletions apps/api-gql/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"github.com/twirapp/twir/apps/api-gql/internal/gql"
"github.com/twirapp/twir/apps/api-gql/internal/gql/directives"
"github.com/twirapp/twir/apps/api-gql/internal/gql/resolvers"
subscriptions_store "github.com/twirapp/twir/apps/api-gql/internal/gql/subscriptions-store"
gqlintegrationshelpers "github.com/twirapp/twir/apps/api-gql/internal/gql/resolvers/integrations"
subscriptionsstore "github.com/twirapp/twir/apps/api-gql/internal/gql/subscriptions-store"
"github.com/twirapp/twir/apps/api-gql/internal/httpserver"
"github.com/twirapp/twir/apps/api-gql/internal/minio"
pubclicroutes "github.com/twirapp/twir/apps/api-gql/internal/routes/public"
Expand All @@ -27,7 +28,10 @@ func main() {
},
minio.New,
twitchcache.New,
subscriptions_store.New,
subscriptionsstore.New,
gqlintegrationshelpers.NewPostCodeHandler,
gqlintegrationshelpers.NewLinksResolver,
gqlintegrationshelpers.NewDataFetcher,
resolvers.New,
directives.New,
httpserver.New,
Expand Down
4 changes: 2 additions & 2 deletions apps/api-gql/internal/gql/data-loader/data-loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"time"

"github.com/nicklaw5/helix/v2"
"github.com/twirapp/twir/apps/api-gql/internal/gql/gqlmodel"
"github.com/twirapp/twir/libs/cache/twitch"
"github.com/vikstrous/dataloadgen"
)
Expand All @@ -17,7 +17,7 @@ const (

type DataLoader struct {
cachedTwitchClient *twitch.CachedTwitchClient
helixUserLoader *dataloadgen.Loader[string, *helix.User]
helixUserLoader *dataloadgen.Loader[string, *gqlmodel.TwirUserTwitchInfo]
}

type Opts struct {
Expand Down
22 changes: 15 additions & 7 deletions apps/api-gql/internal/gql/data-loader/twitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package data_loader
import (
"context"

"github.com/nicklaw5/helix/v2"
"github.com/samber/lo"
"github.com/twirapp/twir/apps/api-gql/internal/gql/gqlmodel"
"github.com/twirapp/twir/libs/cache/twitch"
)

func (c *DataLoader) getHelixUsersByIds(ctx context.Context, ids []string) (
[]*helix.User,
[]*gqlmodel.TwirUserTwitchInfo,
[]error,
) {
users, err := c.cachedTwitchClient.GetUsersByIds(ctx, ids)
if err != nil {
return nil, []error{err}
}

mappedUsers := make([]*helix.User, 0, len(users))
mappedUsers := make([]*gqlmodel.TwirUserTwitchInfo, 0, len(users))

for _, id := range ids {
user, ok := lo.Find(
Expand All @@ -27,7 +27,7 @@ func (c *DataLoader) getHelixUsersByIds(ctx context.Context, ids []string) (
)
if !ok {
mappedUsers = append(
mappedUsers, &helix.User{
mappedUsers, &gqlmodel.TwirUserTwitchInfo{
ID: id,
Login: "[twir] twitch banned",
DisplayName: "[Twir] Twitch Banned",
Expand All @@ -36,18 +36,26 @@ func (c *DataLoader) getHelixUsersByIds(ctx context.Context, ids []string) (
continue
}

mappedUsers = append(mappedUsers, &user.User)
mappedUsers = append(
mappedUsers, &gqlmodel.TwirUserTwitchInfo{
ID: user.ID,
Login: user.Login,
DisplayName: user.DisplayName,
ProfileImageURL: user.ProfileImageURL,
Description: user.Description,
},
)
}

return mappedUsers, nil
}

func GetHelixUser(ctx context.Context, userID string) (*helix.User, error) {
func GetHelixUser(ctx context.Context, userID string) (*gqlmodel.TwirUserTwitchInfo, error) {
loaders := GetLoaderForRequest(ctx)
return loaders.helixUserLoader.Load(ctx, userID)
}

func GetHelixUsers(ctx context.Context, userIDs []string) ([]*helix.User, error) {
func GetHelixUsers(ctx context.Context, userIDs []string) ([]*gqlmodel.TwirUserTwitchInfo, error) {
loaders := GetLoaderForRequest(ctx)
return loaders.helixUserLoader.LoadAll(ctx, userIDs)
}
24 changes: 5 additions & 19 deletions apps/api-gql/internal/gql/resolvers/admin-users.resolver.go

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

15 changes: 1 addition & 14 deletions apps/api-gql/internal/gql/resolvers/greetings.resolver.go

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

35 changes: 35 additions & 0 deletions apps/api-gql/internal/gql/resolvers/integrations.resolver.go

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

Loading
Loading