From b9b9f20166ccfe381148a219752fad283d090e03 Mon Sep 17 00:00:00 2001 From: Adnan Maolood Date: Thu, 20 Jan 2022 19:06:44 -0500 Subject: [PATCH] api/graph: Add User.repository query Replace the Query.repositoryByName and repositoryByOwner queries with a new User.repository query which is more graph-like. References: https://todo.sr.ht/~sircmpwn/sr.ht/309 --- api/graph/schema.graphqls | 22 +++++++++++++--------- api/graph/schema.resolvers.go | 19 +++++-------------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/api/graph/schema.graphqls b/api/graph/schema.graphqls index f4bdbf7..1ed6a2d 100644 --- a/api/graph/schema.graphqls +++ b/api/graph/schema.graphqls @@ -87,6 +87,18 @@ interface Entity { """ canonicalName: String! + "Returns a specific repository owned by the user." + repository(name: String!): Repository @access(scope: REPOSITORIES, kind: RO) + + """ + Returns repositories that the user has access to. + + NOTE: in this version of the API, only repositories owned by the + authenticated user are returned, but in the future the default behavior + will be to return all repositories that the user either (1) has been given + explicit access to via ACLs or (2) has implicit access to either by + ownership or group membership. + """ repositories(cursor: Cursor, filter: Filter): RepositoryCursor! @access(scope: REPOSITORIES, kind: RO) } @@ -101,6 +113,7 @@ type User implements Entity { location: String bio: String + repository(name: String!): Repository @access(scope: REPOSITORIES, kind: RO) repositories(cursor: Cursor, filter: Filter): RepositoryCursor! @access(scope: REPOSITORIES, kind: RO) } @@ -471,15 +484,6 @@ type Query { """ repositories(cursor: Cursor, filter: Filter): RepositoryCursor @access(scope: REPOSITORIES, kind: RO) - "Returns a specific repository, owned by the authenticated user." - repositoryByName(name: String!): Repository @access(scope: REPOSITORIES, kind: RO) - - """ - Returns a specific repository, owned by the given canonical name (e.g. - "~sircmpwn"). - """ - repositoryByOwner(owner: String!, repo: String!): Repository @access(scope: REPOSITORIES, kind: RO) - """ Returns a list of user webhook subscriptions. For clients authenticated with a personal access token, this returns all webhooks diff --git a/api/graph/schema.resolvers.go b/api/graph/schema.resolvers.go index 1b43fc8..b335f89 100644 --- a/api/graph/schema.resolvers.go +++ b/api/graph/schema.resolvers.go @@ -857,20 +857,6 @@ func (r *queryResolver) Repositories(ctx context.Context, cursor *coremodel.Curs return &model.RepositoryCursor{repos, cursor}, nil } -func (r *queryResolver) RepositoryByName(ctx context.Context, name string) (*model.Repository, error) { - return loaders.ForContext(ctx).RepositoriesByName.Load(name) -} - -func (r *queryResolver) RepositoryByOwner(ctx context.Context, owner string, repo string) (*model.Repository, error) { - if strings.HasPrefix(owner, "~") { - owner = owner[1:] - } else { - return nil, fmt.Errorf("Expected owner to be a canonical name") - } - return loaders.ForContext(ctx). - RepositoriesByOwnerRepoName.Load([2]string{owner, repo}) -} - func (r *queryResolver) UserWebhooks(ctx context.Context, cursor *coremodel.Cursor) (*model.WebhookSubscriptionCursor, error) { if cursor == nil { cursor = coremodel.NewCursor(nil) @@ -1222,6 +1208,11 @@ func (r *treeResolver) Entries(ctx context.Context, obj *model.Tree, cursor *cor return &model.TreeEntryCursor{entries, cursor}, nil } +func (r *userResolver) Repository(ctx context.Context, obj *model.User, name string) (*model.Repository, error) { + // TODO: Load repository with user ID instead of username. Needs a new loader. + return loaders.ForContext(ctx).RepositoriesByOwnerRepoName.Load([2]string{obj.Username, name}) +} + func (r *userResolver) Repositories(ctx context.Context, obj *model.User, cursor *coremodel.Cursor, filter *coremodel.Filter) (*model.RepositoryCursor, error) { if cursor == nil { cursor = coremodel.NewCursor(filter) -- 2.38.4