Skip to content

Commit 9c2a4f0

Browse files
committed
dbeaver/pro#5701 read operations from a common folder
1 parent f59ba16 commit 9c2a4f0

File tree

5 files changed

+73
-73
lines changed

5 files changed

+73
-73
lines changed

go/api/client.go

Lines changed: 49 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,17 @@ import (
55
"errors"
66
"fmt"
77
"log/slog"
8+
"os"
89
"time"
910

1011
"github.com/dbeaver/cloudbeaver-graphql-examples/go/graphql"
1112
"github.com/dbeaver/cloudbeaver-graphql-examples/go/lib"
1213
)
1314

14-
// Queries and mutations
15-
const (
16-
authQuery = `
17-
query authLogin($token: String!) {
18-
authLogin(provider: "token", credentials: { token: $token }) {
19-
userTokens {
20-
userId
21-
}
22-
authStatus
23-
}
24-
}
25-
`
26-
27-
createTeamQuery = `
28-
query createTeam($teamId: ID!) {
29-
createTeam(teamId: $teamId) {
30-
teamId
31-
}
32-
}
33-
`
34-
35-
deleteTeamQuery = `
36-
query deleteTeam($teamId: ID!, $force: Boolean) {
37-
deleteTeam(teamId: $teamId, force: $force)
38-
}
39-
`
40-
41-
createProjectMutation = `
42-
mutation RmCreateProject($projectName: String!) {
43-
rmCreateProject(projectName: $projectName) {
44-
id
45-
}
46-
}
47-
`
48-
49-
deleteProjectMutation = `
50-
mutation RmDeleteProject($projectId: ID!) {
51-
rmDeleteProject(projectId: $projectId)
52-
}
53-
`
54-
addProjectPermissionsMutation = `
55-
mutation addProjectsPermissions($projectIds: [ID!]!, $subjectIds: [ID!]!, $permissions: [String!]!) {
56-
rmAddProjectsPermissions(
57-
projectIds: $projectIds
58-
subjectIds: $subjectIds
59-
permissions: $permissions
60-
)
61-
}
62-
`
63-
)
64-
6515
type Client struct {
66-
GraphQLClient graphql.Client
67-
Endpoint string
16+
GraphQLClient graphql.Client
17+
Endpoint string
18+
OperationsPath string
6819
}
6920

7021
func (client Client) sendRequest(operationName, query string, variables graphql.Object) (json.RawMessage, error) {
@@ -90,42 +41,70 @@ func (client Client) sendRequestDiscardingData(operationName, query string, vari
9041
return err
9142
}
9243

44+
func (client Client) readOperationText(operationName string) (string, error) {
45+
path := client.OperationsPath + "/" + operationName + ".gql"
46+
bytes, err := os.ReadFile(path)
47+
if err != nil {
48+
return "", lib.WrapError(fmt.Sprintf("unable to read operation file %s", path), err)
49+
}
50+
return string(bytes), nil
51+
}
52+
9353
func (client Client) Auth(token string) error {
54+
query, err := client.readOperationText("auth")
55+
if err != nil {
56+
return err
57+
}
9458
variables := map[string]any{
9559
"token": token,
9660
}
97-
return client.sendRequestDiscardingData("auth", authQuery, variables)
61+
return client.sendRequestDiscardingData("auth", query, variables)
9862
}
9963

10064
func (client Client) CreateTeam(teamId string) error {
65+
query, err := client.readOperationText("create_team")
66+
if err != nil {
67+
return err
68+
}
10169
variables := map[string]any{
10270
"teamId": teamId,
10371
}
10472
return client.sendRequestDiscardingData(
10573
fmt.Sprintf("create team '%s'", teamId),
106-
createTeamQuery,
107-
variables)
74+
query,
75+
variables,
76+
)
10877
}
10978

11079
func (client Client) DeleteTeam(teamId string, force bool) error {
80+
query, err := client.readOperationText("delete_team")
81+
if err != nil {
82+
return err
83+
}
11184
variables := map[string]any{
11285
"teamId": teamId,
11386
"force": force,
11487
}
11588
return client.sendRequestDiscardingData(
11689
fmt.Sprintf("delete team '%s'", teamId),
117-
deleteTeamQuery,
118-
variables)
90+
query,
91+
variables,
92+
)
11993
}
12094

12195
func (client Client) CreateProject(projectName string) (id string, err error) {
96+
query, err := client.readOperationText("create_project")
97+
if err != nil {
98+
return "", err
99+
}
122100
variables := map[string]any{
123101
"projectName": projectName,
124102
}
125103
rawData, err := client.sendRequest(
126104
fmt.Sprintf("create project with name '%s'", projectName),
127-
createProjectMutation,
128-
variables)
105+
query,
106+
variables,
107+
)
129108
if err != nil {
130109
return id, err
131110
}
@@ -138,18 +117,26 @@ func (client Client) CreateProject(projectName string) (id string, err error) {
138117
}
139118

140119
func (client Client) DeleteProject(projectId string) error {
120+
query, err := client.readOperationText("delete_project")
121+
if err != nil {
122+
return err
123+
}
141124
variables := map[string]any{
142125
"projectId": projectId,
143126
}
144127
return client.sendRequestDiscardingData(
145128
fmt.Sprintf("delete project with id '%s'", projectId),
146-
deleteProjectMutation,
129+
query,
147130
variables,
148131
)
149132
}
150133

151134
// subjectIds: Ids of teams or individual users
152135
func (client Client) AddProjectAccess(projectId string, subjectIds ...string) error {
136+
query, err := client.readOperationText("add_project_permissions")
137+
if err != nil {
138+
return err
139+
}
153140
variables := map[string]any{
154141
"projectIds": [1]string{projectId},
155142
"subjectIds": subjectIds,
@@ -160,7 +147,7 @@ func (client Client) AddProjectAccess(projectId string, subjectIds ...string) er
160147
}
161148
return client.sendRequestDiscardingData(
162149
fmt.Sprintf("grant subjects %s access to project with id '%s'", subjectIds, projectId),
163-
addProjectPermissionsMutation,
150+
query,
164151
variables,
165152
)
166153
}

go/main.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"bufio"
5+
"flag"
56
"fmt"
67
"log/slog"
78
"net/http"
@@ -29,11 +30,14 @@ func main() {
2930

3031
func main0() error {
3132
// Instantiate a client
32-
env, err := readEnv()
33+
envFlag := flag.String("env", "../.env", "Path to the .env file")
34+
operationsFlag := flag.String("operations", "../operations", "Path to the folder with GraphQL operations")
35+
flag.Parse()
36+
env, err := readEnv(*envFlag)
3337
if err != nil {
3438
return lib.WrapError("error while reading variables", err)
3539
}
36-
apiClient := initClient(env.serverURL + "/" + env.serviceURI + "/gql")
40+
apiClient := initClient(env.serverURL+"/"+env.serviceURI+"/gql", *operationsFlag)
3741

3842
// Auth
3943
err = apiClient.Auth(env.apiToken)
@@ -74,13 +78,7 @@ type env struct {
7478
serviceURI string
7579
}
7680

77-
func readEnv() (env, error) {
78-
var envFilePath string
79-
if len(os.Args) > 1 {
80-
envFilePath = os.Args[1]
81-
} else {
82-
envFilePath = "../.env"
83-
}
81+
func readEnv(envFilePath string) (env, error) {
8482
env := env{}
8583
file, err := os.Open(envFilePath)
8684
if err != nil {
@@ -109,14 +107,14 @@ func readEnv() (env, error) {
109107
return env, err
110108
}
111109

112-
func initClient(endpoint string) api.Client {
110+
func initClient(endpoint string, operationsPath string) api.Client {
113111
cookieJar, err := cookiejar.New(nil)
114112
if err != nil {
115113
// Invariant: the method that creates cookie jar with no options never returns non-nil err
116114
panic("encountered error while creating a cookie jar! " + err.Error())
117115
}
118116
graphQLClient := graphql.Client{HttpClient: &http.Client{Jar: cookieJar}}
119-
return api.Client{GraphQLClient: graphQLClient, Endpoint: endpoint}
117+
return api.Client{GraphQLClient: graphQLClient, Endpoint: endpoint, OperationsPath: operationsPath}
120118
}
121119

122120
func cleanup(callDescription string, apiCall func() error) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mutation addProjectsPermissions($projectIds: [ID!]!, $subjectIds: [ID!]!, $permissions: [String!]!) {
2+
rmAddProjectsPermissions(
3+
projectIds: $projectIds
4+
subjectIds: $subjectIds
5+
permissions: $permissions
6+
)
7+
}

operations/create_project.gql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mutation RmCreateProject($projectName: String!) {
2+
rmCreateProject(projectName: $projectName) {
3+
id
4+
}
5+
}

operations/delete_project.gql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mutation RmDeleteProject($projectId: ID!) {
2+
rmDeleteProject(projectId: $projectId)
3+
}

0 commit comments

Comments
 (0)