@@ -5,66 +5,17 @@ import (
5
5
"errors"
6
6
"fmt"
7
7
"log/slog"
8
+ "os"
8
9
"time"
9
10
10
11
"github.com/dbeaver/cloudbeaver-graphql-examples/go/graphql"
11
12
"github.com/dbeaver/cloudbeaver-graphql-examples/go/lib"
12
13
)
13
14
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
-
65
15
type Client struct {
66
- GraphQLClient graphql.Client
67
- Endpoint string
16
+ GraphQLClient graphql.Client
17
+ Endpoint string
18
+ OperationsPath string
68
19
}
69
20
70
21
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
90
41
return err
91
42
}
92
43
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
+
93
53
func (client Client ) Auth (token string ) error {
54
+ query , err := client .readOperationText ("auth" )
55
+ if err != nil {
56
+ return err
57
+ }
94
58
variables := map [string ]any {
95
59
"token" : token ,
96
60
}
97
- return client .sendRequestDiscardingData ("auth" , authQuery , variables )
61
+ return client .sendRequestDiscardingData ("auth" , query , variables )
98
62
}
99
63
100
64
func (client Client ) CreateTeam (teamId string ) error {
65
+ query , err := client .readOperationText ("create_team" )
66
+ if err != nil {
67
+ return err
68
+ }
101
69
variables := map [string ]any {
102
70
"teamId" : teamId ,
103
71
}
104
72
return client .sendRequestDiscardingData (
105
73
fmt .Sprintf ("create team '%s'" , teamId ),
106
- createTeamQuery ,
107
- variables )
74
+ query ,
75
+ variables ,
76
+ )
108
77
}
109
78
110
79
func (client Client ) DeleteTeam (teamId string , force bool ) error {
80
+ query , err := client .readOperationText ("delete_team" )
81
+ if err != nil {
82
+ return err
83
+ }
111
84
variables := map [string ]any {
112
85
"teamId" : teamId ,
113
86
"force" : force ,
114
87
}
115
88
return client .sendRequestDiscardingData (
116
89
fmt .Sprintf ("delete team '%s'" , teamId ),
117
- deleteTeamQuery ,
118
- variables )
90
+ query ,
91
+ variables ,
92
+ )
119
93
}
120
94
121
95
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
+ }
122
100
variables := map [string ]any {
123
101
"projectName" : projectName ,
124
102
}
125
103
rawData , err := client .sendRequest (
126
104
fmt .Sprintf ("create project with name '%s'" , projectName ),
127
- createProjectMutation ,
128
- variables )
105
+ query ,
106
+ variables ,
107
+ )
129
108
if err != nil {
130
109
return id , err
131
110
}
@@ -138,18 +117,26 @@ func (client Client) CreateProject(projectName string) (id string, err error) {
138
117
}
139
118
140
119
func (client Client ) DeleteProject (projectId string ) error {
120
+ query , err := client .readOperationText ("delete_project" )
121
+ if err != nil {
122
+ return err
123
+ }
141
124
variables := map [string ]any {
142
125
"projectId" : projectId ,
143
126
}
144
127
return client .sendRequestDiscardingData (
145
128
fmt .Sprintf ("delete project with id '%s'" , projectId ),
146
- deleteProjectMutation ,
129
+ query ,
147
130
variables ,
148
131
)
149
132
}
150
133
151
134
// subjectIds: Ids of teams or individual users
152
135
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
+ }
153
140
variables := map [string ]any {
154
141
"projectIds" : [1 ]string {projectId },
155
142
"subjectIds" : subjectIds ,
@@ -160,7 +147,7 @@ func (client Client) AddProjectAccess(projectId string, subjectIds ...string) er
160
147
}
161
148
return client .sendRequestDiscardingData (
162
149
fmt .Sprintf ("grant subjects %s access to project with id '%s'" , subjectIds , projectId ),
163
- addProjectPermissionsMutation ,
150
+ query ,
164
151
variables ,
165
152
)
166
153
}
0 commit comments