From 7a00f10f33fcb27cc3add4edab34654c2a576f5e Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Mon, 17 Apr 2023 14:05:43 +0200 Subject: [PATCH] API: fetch user context in loader implementation Fetching it outside of this function will cause it to attempt to fetch the auth context when the middleware is stood up, which occurs unconditionally on all requests -- including unauthenticated ones, such as /query/metrics. Moving it into the function body causes it to only occur once we actually attempt to fetch jobs. --- api/graph/schema.resolvers.go | 1 + api/loaders/middleware.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/api/graph/schema.resolvers.go b/api/graph/schema.resolvers.go index d87562f..30cc17d 100644 --- a/api/graph/schema.resolvers.go +++ b/api/graph/schema.resolvers.go @@ -2,6 +2,7 @@ package graph // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. +// Code generated by github.com/99designs/gqlgen version v0.17.29 import ( "bytes" diff --git a/api/loaders/middleware.go b/api/loaders/middleware.go index 7575d12..cdb8b98 100644 --- a/api/loaders/middleware.go +++ b/api/loaders/middleware.go @@ -119,8 +119,8 @@ func fetchUsersByName(ctx context.Context) func(names []string) ([]*model.User, } func fetchJobsByID(ctx context.Context) func(ids []int) ([]*model.Job, []error) { - user := auth.ForContext(ctx) return func(ids []int) ([]*model.Job, []error) { + user := auth.ForContext(ctx) jobs := make([]*model.Job, len(ids)) if err := database.WithTx(ctx, &sql.TxOptions{ Isolation: 0,