From 864816cfbc0ca3cc86b9e7248196486b245545c7 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 24 Jan 2024 10:47:14 +0000 Subject: [PATCH] client: rename Execute to Do 583d0b1bcb08 ("client/graphql: handle GraphQL errors") changed Execute behavior by wrapping the result into a struct with "data" and "errors" fields. This is a breaking change, but it's hard to spot when upgrading core-go because it won't cause a compilation error. Rename Execute to Do to break the build and force callers to update accordingly. --- auth/middleware.go | 47 +++++++++++++++++++--------------------------- client/graphql.go | 2 +- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/auth/middleware.go b/auth/middleware.go index 03321ee2cb1f484d8a59c9efa6c1e0d2fc279ed1..508c4beea2542b6b606b98e95cbaffb1df5b4de7 100644 --- a/auth/middleware.go +++ b/auth/middleware.go @@ -320,23 +320,6 @@ func FetchMetaProfile(ctx context.Context, username string, user *AuthContext) e panic(errors.New("Cannot fetch profile from ourselves")) } - type GraphQLProfile struct { - ID int `json:"id"` - Username string `json:"username"` - Email string `json:"email"` - URL *string `json:"url"` - Location *string `json:"location"` - Bio *string `json:"bio"` - UserType string `json:"userType"` - SuspensionNotice string `json:"suspensionNotice"` - } - - type GraphQLResponse struct { - Data struct { - Me GraphQLProfile `json:"me"` - } `json:"data"` - } - query := client.GraphQLQuery{ Query: ` query { @@ -352,13 +335,24 @@ func FetchMetaProfile(ctx context.Context, username string, user *AuthContext) e }`, } - var result GraphQLResponse - if err := client.Execute(ctx, username, + var result struct { + Me struct { + ID int `json:"id"` + Username string `json:"username"` + Email string `json:"email"` + URL *string `json:"url"` + Location *string `json:"location"` + Bio *string `json:"bio"` + UserType string `json:"userType"` + SuspensionNotice string `json:"suspensionNotice"` + } `json:"me"` + } + if err := client.Do(ctx, username, "meta.sr.ht", query, &result); err != nil { return err } - profile := result.Data.Me + profile := result.Me return database.WithTx(ctx, nil, func(tx *sql.Tx) error { // TODO: Make the database representation consistent with this ut := strings.ToLower(profile.UserType) @@ -474,11 +468,6 @@ func LookupUser(ctx context.Context, username string, user *AuthContext) error { // should not be trusted) func LookupTokenRevocation(ctx context.Context, username string, hash [64]byte, clientID string) (bool, error) { - type GraphQLResponse struct { - Data struct { - RevocationStatus bool `json:"tokenRevocationStatus"` - } `json:"data"` - } query := client.GraphQLQuery{ Query: ` @@ -491,12 +480,14 @@ func LookupTokenRevocation(ctx context.Context, }, } - var result GraphQLResponse - if err := client.Execute(ctx, username, + var result struct { + RevocationStatus bool `json:"tokenRevocationStatus"` + } + if err := client.Do(ctx, username, "meta.sr.ht", query, &result); err != nil { return true, err } - return result.Data.RevocationStatus, nil + return result.RevocationStatus, nil } func OAuth2(token string, hash [64]byte, w http.ResponseWriter, diff --git a/client/graphql.go b/client/graphql.go index 0b986057ce1dd434808677fd52b78f633b86eeed..59ce62e8dfeba275c3786b8f1abe2e8ef4aeb250 100644 --- a/client/graphql.go +++ b/client/graphql.go @@ -25,7 +25,7 @@ type InternalAuth struct { NodeID string `json:"node_id"` } -func Execute(ctx context.Context, username string, svc string, +func Do(ctx context.Context, username string, svc string, query GraphQLQuery, result interface{}) error { body, err := json.Marshal(query)