Skip to content

Commit 886b351

Browse files
committed
Slight refactor to GraphQL helper, review feedback on current_user
1 parent 045802f commit 886b351

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

internal/provider/data_source_gitlab_current_user.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111

1212
var _ = registerDataSource("gitlab_current_user", func() *schema.Resource {
1313
return &schema.Resource{
14-
Description: `The ` + "`gitlab_current_user`" + ` data source allows details of the current user (determined by the input provider token) to be retrieved.
14+
Description: `The ` + "`gitlab_current_user`" + ` data source allows details of the current user (determined by ` + "`token`" + ` provider attribute) to be retrieved.
1515
16-
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/graphql/reference/index.html#querycurrentuser)`,
16+
**Upstream API**: [GitLab GraphQL API docs](https://docs.gitlab.com/ee/api/graphql/reference/index.html#querycurrentuser)`,
1717

1818
ReadContext: dataSourceGitlabCurrentUserRead,
1919
Schema: map[string]*schema.Schema{
@@ -28,7 +28,7 @@ var _ = registerDataSource("gitlab_current_user", func() *schema.Resource {
2828
Computed: true,
2929
},
3030
"name": {
31-
Description: "Human-readable name of the user. Returns **** if the user is a project bot and the requester does not have permission to view the project. ",
31+
Description: "Human-readable name of the user. Returns **** if the user is a project bot and the requester does not have permission to view the project.",
3232
Type: schema.TypeString,
3333
Computed: true,
3434
},
@@ -60,13 +60,12 @@ func dataSourceGitlabCurrentUserRead(ctx context.Context, d *schema.ResourceData
6060
client := meta.(*gitlab.Client)
6161

6262
query := GraphQLQuery{
63-
`query {currentUser {name, bot, groupCount, id, namespace{id} publicEmail, username}}`,
63+
`query {currentUser {name, bot, groupCount, id, namespace{id}, publicEmail, username}}`,
6464
}
6565
log.Printf("[DEBUG] executing GraphQL Query %s to retrieve current user", query.Query)
6666

6767
var response CurrentUserResponse
68-
_, err := SendGraphQLRequest(context.Background(), client, query, &response)
69-
if err != nil {
68+
if _, err := SendGraphQLRequest(ctx, client, query, &response); err != nil {
7069
return diag.FromErr(err)
7170
}
7271

internal/provider/graphql_helper.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,20 @@ import (
77
)
88

99
// Helper method for modifying client requests appropriately for sending a GraphQL call instead of a REST call.
10-
func SendGraphQLRequest(ctx context.Context, client *gitlab.Client, graphQLCall GraphQLQuery, objectToParseForResponse interface{}) (interface{}, error) {
11-
12-
request, err := client.NewRequest("POST", "", graphQLCall, nil)
13-
//Overwrite the path of the existing request, as otherwise the client appends /api/v4 instead.
14-
request.URL.Path = "/api/graphql"
10+
func SendGraphQLRequest(ctx context.Context, client *gitlab.Client, query GraphQLQuery, response interface{}) (interface{}, error) {
11+
request, err := client.NewRequest("POST", "", query, nil)
1512
if err != nil {
1613
return nil, err
1714
}
18-
19-
_, err = client.Do(request, objectToParseForResponse)
20-
if err != nil {
15+
// Overwrite the path of the existing request, as otherwise the go-gitlab client appends /api/v4 instead.
16+
request.URL.Path = "/api/graphql"
17+
if _, err = client.Do(request, response); err != nil {
2118
return nil, err
2219
}
23-
24-
return objectToParseForResponse, nil
20+
return response, nil
2521
}
2622

27-
// Represents a GraphQL call to the API. All graphQL calls are a string passed to the "query" parameter, so they should be included here.
23+
// Represents a GraphQL call to the API. All GraphQL calls are a string passed to the "query" parameter, so they should be included here.
2824
type GraphQLQuery struct {
2925
Query string `json:"query"`
3026
}

0 commit comments

Comments
 (0)