-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathcommon.go
800 lines (683 loc) · 22.8 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*
* Copyright 2019 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package common
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"strings"
"testing"
"time"
"github.com/dgraph-io/dgo/v200"
"github.com/dgraph-io/dgo/v200/protos/api"
"github.com/dgraph-io/dgraph/x"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)
const (
GraphqlURL = "http://localhost:8180/graphql"
GraphqlAdminURL = "http://localhost:8180/admin"
AlphagRPC = "localhost:9180"
adminDgraphHealthURL = "http://localhost:8280/health?all"
adminDgraphStateURL = "http://localhost:8280/state"
graphqlAdminTestURL = "http://localhost:8280/graphql"
graphqlAdminTestAdminURL = "http://localhost:8280/admin"
graphqlAdminTestAdminSchemaURL = "http://localhost:8280/admin/schema"
alphaAdminTestgRPC = "localhost:9280"
)
// GraphQLParams is parameters for the constructing a GraphQL query - that's
// http POST with this body, or http GET with this in the query string.
//
// https://graphql.org/learn/serving-over-http/ says:
//
// POST
// ----
// 'A standard GraphQL POST request should use the application/json content type,
// and include a JSON-encoded body of the following form:
// {
// "query": "...",
// "operationName": "...",
// "variables": { "myVariable": "someValue", ... }
// }
// operationName and variables are optional fields. operationName is only
// required if multiple operations are present in the query.'
//
//
// GET
// ---
//
// http://myapi/graphql?query={me{name}}
// "Query variables can be sent as a JSON-encoded string in an additional query parameter
// called variables. If the query contains several named operations, an operationName query
// parameter can be used to control which one should be executed."
//
// acceptGzip sends "Accept-Encoding: gzip" header to the server, which would return the
// response after gzip.
// gzipEncoding would compress the request to the server and add "Content-Encoding: gzip"
// header to the same.
type GraphQLParams struct {
Query string `json:"query"`
OperationName string `json:"operationName"`
Variables map[string]interface{} `json:"variables"`
acceptGzip bool
gzipEncoding bool
Headers http.Header
}
type requestExecutor func(t *testing.T, url string, params *GraphQLParams) *GraphQLResponse
// GraphQLResponse GraphQL response structure.
// see https://graphql.github.io/graphql-spec/June2018/#sec-Response
type GraphQLResponse struct {
Data json.RawMessage `json:"data,omitempty"`
Errors x.GqlErrorList `json:"errors,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}
type country struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
States []*state `json:"states,omitempty"`
}
type author struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Dob *time.Time `json:"dob,omitempty"`
Reputation float32 `json:"reputation,omitempty"`
Country *country `json:"country,omitempty"`
Posts []*post `json:"posts,omitempty"`
}
type user struct {
Name string `json:"name,omitempty"`
Password string `json:"password,omitempty"`
}
type post struct {
PostID string `json:"postID,omitempty"`
Title string `json:"title,omitempty"`
Text string `json:"text,omitempty"`
Tags []string `json:"tags,omitempty"`
NumLikes int `json:"numLikes,omitempty"`
NumViews int64 `json:"numViews,omitempty"`
IsPublished bool `json:"isPublished,omitempty"`
PostType string `json:"postType,omitempty"`
Author *author `json:"author,omitempty"`
Category *category `json:"category,omitempty"`
}
type category struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Posts []post `json:"posts,omitempty"`
}
type state struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Code string `json:"xcode,omitempty"`
Capital string `json:"capital,omitempty"`
Country *country `json:"country,omitempty"`
}
type movie struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Director []*director `json:"moviedirector,omitempty"`
}
type director struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
type teacher struct {
ID string `json:"id,omitempty"`
Xid string `json:"xid,omitempty"`
Name string `json:"name,omitempty"`
Subject string `json:"subject,omitempty"`
Teaches []*student `json:"teaches,omitempty"`
}
type student struct {
ID string `json:"id,omitempty"`
Xid string `json:"xid,omitempty"`
Name string `json:"name,omitempty"`
TaughtBy []*teacher `json:"taughtBy,omitempty"`
}
func BootstrapServer(schema, data []byte) {
err := checkGraphQLStarted(GraphqlAdminURL)
if err != nil {
x.Panic(errors.Errorf(
"Waited for GraphQL test server to become available, but it never did.\n"+
"Got last error %+v", err.Error()))
}
err = checkGraphQLStarted(graphqlAdminTestAdminURL)
if err != nil {
x.Panic(errors.Errorf(
"Waited for GraphQL AdminTest server to become available, "+
"but it never did.\n Got last error: %+v", err.Error()))
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
d, err := grpc.DialContext(ctx, AlphagRPC, grpc.WithInsecure())
if err != nil {
x.Panic(err)
}
client := dgo.NewDgraphClient(api.NewDgraphClient(d))
err = addSchema(GraphqlAdminURL, string(schema))
if err != nil {
x.Panic(err)
}
err = maybePopulateData(client, data)
if err != nil {
x.Panic(err)
}
if err = d.Close(); err != nil {
x.Panic(err)
}
}
// RunAll runs all the test functions in this package as sub tests.
func RunAll(t *testing.T) {
// admin tests
t.Run("admin", admin)
t.Run("health", health)
t.Run("partial health", partialHealth)
t.Run("alias should work in admin", adminAlias)
t.Run("state", adminState)
t.Run("propagate client remote ip", clientInfoLogin)
// schema tests
t.Run("graphql descriptions", graphQLDescriptions)
// header tests
t.Run("touched uids header", touchedUidsHeader)
// encoding
t.Run("gzip compression", gzipCompression)
t.Run("gzip compression header", gzipCompressionHeader)
t.Run("gzip compression no header", gzipCompressionNoHeader)
// query tests
t.Run("get request", getRequest)
t.Run("get query empty variable", getQueryEmptyVariable)
t.Run("post request with application/graphql", queryApplicationGraphQl)
t.Run("query by type", queryByType)
t.Run("uid alias", uidAlias)
t.Run("order at root", orderAtRoot)
t.Run("page at root", pageAtRoot)
t.Run("regexp", regExp)
t.Run("multiple search indexes", multipleSearchIndexes)
t.Run("multiple search indexes wrong field", multipleSearchIndexesWrongField)
t.Run("hash search", hashSearch)
t.Run("deep filter", deepFilter)
t.Run("many queries", manyQueries)
t.Run("query order at root", queryOrderAtRoot)
t.Run("queries with error", queriesWithError)
t.Run("date filters", dateFilters)
t.Run("float filters", floatFilters)
t.Run("Int filters", int32Filters)
t.Run("Int64 filters", int64Filters)
t.Run("boolean filters", booleanFilters)
t.Run("term filters", termFilters)
t.Run("full text filters", fullTextFilters)
t.Run("string exact filters", stringExactFilters)
t.Run("scalar list filters", scalarListFilters)
t.Run("skip directive", skipDirective)
t.Run("include directive", includeDirective)
t.Run("include and skip directive", includeAndSkipDirective)
t.Run("query by mutliple ids", queryByMultipleIds)
t.Run("enum filter", enumFilter)
t.Run("default enum filter", defaultEnumFilter)
t.Run("query by multiple invalid ids", queryByMultipleInvalidIds)
t.Run("query typename", queryTypename)
t.Run("query nested typename", queryNestedTypename)
t.Run("typename for interface", typenameForInterface)
t.Run("query only typename", queryOnlyTypename)
t.Run("query nested only typename", querynestedOnlyTypename)
t.Run("test onlytypename for interface types", onlytypenameForInterface)
t.Run("get state by xid", getStateByXid)
t.Run("get state without args", getStateWithoutArgs)
t.Run("get state by both xid and uid", getStateByBothXidAndUid)
t.Run("query state by xid", queryStateByXid)
t.Run("query state by xid regex", queryStateByXidRegex)
t.Run("multiple operations", multipleOperations)
t.Run("query post with author", queryPostWithAuthor)
t.Run("queries have extensions", queriesHaveExtensions)
t.Run("alias works for queries", queryWithAlias)
t.Run("cascade directive", queryWithCascade)
// mutation tests
t.Run("add mutation", addMutation)
t.Run("update mutation by ids", updateMutationByIds)
t.Run("update mutation by name", updateMutationByName)
t.Run("update mutation by name no match", updateMutationByNameNoMatch)
t.Run("update delete", updateRemove)
t.Run("filter in update", filterInUpdate)
t.Run("selection in add object", testSelectionInAddObject)
t.Run("delete mutation with multiple ids", deleteMutationWithMultipleIds)
t.Run("delete mutation with single id", deleteMutationWithSingleID)
t.Run("delete mutation by name", deleteMutationByName)
t.Run("delete mutation removes references", deleteMutationReferences)
t.Run("add mutation updates references", addMutationReferences)
t.Run("update set mutation updates references", updateMutationReferences)
t.Run("delete wrong id", deleteWrongID)
t.Run("many mutations", manyMutations)
t.Run("mutations with deep filter", mutationWithDeepFilter)
t.Run("many mutations with query error", manyMutationsWithQueryError)
t.Run("query interface after add mutation", queryInterfaceAfterAddMutation)
t.Run("add mutation with xid", addMutationWithXID)
t.Run("deep mutations", deepMutations)
t.Run("add multiple mutations", testMultipleMutations)
t.Run("deep XID mutations", deepXIDMutations)
t.Run("error in multiple mutations", addMultipleMutationWithOneError)
t.Run("dgraph directive with reverse edge adds data correctly",
addMutationWithReverseDgraphEdge)
t.Run("numUids test", testNumUids)
t.Run("empty delete", mutationEmptyDelete)
t.Run("password in mutation", passwordTest)
t.Run("duplicate xid in single mutation", deepMutationDuplicateXIDsSameObjectTest)
t.Run("query typename in mutation payload", queryTypenameInMutationPayload)
t.Run("ensure alias in mutation payload", ensureAliasInMutationPayload)
t.Run("mutations have extensions", mutationsHaveExtensions)
t.Run("alias works for mutations", mutationsWithAlias)
t.Run("three level deep", threeLevelDeepMutation)
t.Run("update mutation without set & remove", updateMutationWithoutSetRemove)
// error tests
t.Run("graphql completion on", graphQLCompletionOn)
t.Run("request validation errors", requestValidationErrors)
t.Run("panic catcher", panicCatcher)
t.Run("deep mutation errors", deepMutationErrors)
// fragment tests
t.Run("fragment in mutation", fragmentInMutation)
t.Run("fragment in query", fragmentInQuery)
t.Run("fragment in query on Interface", fragmentInQueryOnInterface)
t.Run("fragment in query on Object", fragmentInQueryOnObject)
}
func gunzipData(data []byte) ([]byte, error) {
b := bytes.NewBuffer(data)
r, err := gzip.NewReader(b)
if err != nil {
return nil, err
}
var resB bytes.Buffer
if _, err := resB.ReadFrom(r); err != nil {
return nil, err
}
return resB.Bytes(), nil
}
func gzipData(data []byte) ([]byte, error) {
var b bytes.Buffer
gz := gzip.NewWriter(&b)
if _, err := gz.Write(data); err != nil {
return nil, err
}
if err := gz.Close(); err != nil {
return nil, err
}
return b.Bytes(), nil
}
// This tests that if a request has gzip header but the body is
// not compressed, then it should return an error
func gzipCompressionHeader(t *testing.T) {
queryCountry := &GraphQLParams{
Query: `query {
queryCountry {
name
}
}`,
}
req, err := queryCountry.createGQLPost(GraphqlURL)
require.NoError(t, err)
req.Header.Set("Content-Encoding", "gzip")
resData, err := runGQLRequest(req)
require.NoError(t, err)
var result *GraphQLResponse
err = json.Unmarshal(resData, &result)
require.NoError(t, err)
require.NotNil(t, result.Errors)
require.Contains(t, result.Errors[0].Message, "Unable to parse gzip")
}
// This tests that if a req's body is compressed but the
// header is not present, then it should return an error
func gzipCompressionNoHeader(t *testing.T) {
queryCountry := &GraphQLParams{
Query: `query {
queryCountry {
name
}
}`,
gzipEncoding: true,
}
req, err := queryCountry.createGQLPost(GraphqlURL)
require.NoError(t, err)
req.Header.Del("Content-Encoding")
resData, err := runGQLRequest(req)
require.NoError(t, err)
var result *GraphQLResponse
err = json.Unmarshal(resData, &result)
require.NoError(t, err)
require.NotNil(t, result.Errors)
require.Contains(t, result.Errors[0].Message, "Not a valid GraphQL request body")
}
func getRequest(t *testing.T) {
add(t, getExecutor)
}
func getQueryEmptyVariable(t *testing.T) {
queryCountry := &GraphQLParams{
Query: `query {
queryCountry {
name
}
}`,
}
req, err := queryCountry.createGQLGet(GraphqlURL)
require.NoError(t, err)
q := req.URL.Query()
q.Del("variables")
req.URL.RawQuery = q.Encode()
res := queryCountry.Execute(t, req)
require.Nil(t, res.Errors)
}
// Execute takes a HTTP request from either ExecuteAsPost or ExecuteAsGet
// and executes the request
func (params *GraphQLParams) Execute(t require.TestingT, req *http.Request) *GraphQLResponse {
for h := range params.Headers {
req.Header.Set(h, params.Headers.Get(h))
}
res, err := runGQLRequest(req)
require.NoError(t, err)
var result *GraphQLResponse
if params.acceptGzip {
res, err = gunzipData(res)
require.NoError(t, err)
require.Contains(t, req.Header.Get("Accept-Encoding"), "gzip")
}
err = json.Unmarshal(res, &result)
require.NoError(t, err)
return result
}
// ExecuteAsPost builds a HTTP POST request from the GraphQL input structure
// and executes the request to url.
func (params *GraphQLParams) ExecuteAsPost(t require.TestingT, url string) *GraphQLResponse {
req, err := params.createGQLPost(url)
require.NoError(t, err)
return params.Execute(t, req)
}
// ExecuteAsPostApplicationGraphql builds an HTTP Post with type application/graphql
// Note, variables are not allowed
func (params *GraphQLParams) ExecuteAsPostApplicationGraphql(t *testing.T, url string) *GraphQLResponse {
require.Empty(t, params.Variables)
req, err := params.createApplicationGQLPost(url)
require.NoError(t, err)
return params.Execute(t, req)
}
// ExecuteAsGet builds a HTTP GET request from the GraphQL input structure
// and executes the request to url.
func (params *GraphQLParams) ExecuteAsGet(t *testing.T, url string) *GraphQLResponse {
req, err := params.createGQLGet(url)
require.NoError(t, err)
return params.Execute(t, req)
}
func getExecutor(t *testing.T, url string, params *GraphQLParams) *GraphQLResponse {
return params.ExecuteAsGet(t, url)
}
func postExecutor(t *testing.T, url string, params *GraphQLParams) *GraphQLResponse {
return params.ExecuteAsPost(t, url)
}
func (params *GraphQLParams) createGQLGet(url string) (*http.Request, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("query", params.Query)
q.Add("operationName", params.OperationName)
variableString, err := json.Marshal(params.Variables)
if err != nil {
return nil, err
}
q.Add("variables", string(variableString))
req.URL.RawQuery = q.Encode()
if params.acceptGzip {
req.Header.Set("Accept-Encoding", "gzip")
}
return req, nil
}
func (params *GraphQLParams) buildPostRequest(url string, body []byte, contentType string) (*http.Request, error) {
var err error
if params.gzipEncoding {
if body, err = gzipData(body); err != nil {
return nil, err
}
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
if params.gzipEncoding {
req.Header.Set("Content-Encoding", "gzip")
}
if params.acceptGzip {
req.Header.Set("Accept-Encoding", "gzip")
}
return req, nil
}
func (params *GraphQLParams) createGQLPost(url string) (*http.Request, error) {
body, err := json.Marshal(params)
if err != nil {
return nil, err
}
return params.buildPostRequest(url, body, "application/json")
}
func (params *GraphQLParams) createApplicationGQLPost(url string) (*http.Request, error) {
return params.buildPostRequest(url, []byte(params.Query), "application/graphql")
}
// runGQLRequest runs a HTTP GraphQL request and returns the data or any errors.
func runGQLRequest(req *http.Request) ([]byte, error) {
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
// GraphQL server should always return OK, even when there are errors
if status := resp.StatusCode; status != http.StatusOK {
return nil, errors.Errorf("unexpected status code: %v", status)
}
if strings.ToLower(resp.Header.Get("Content-Type")) != "application/json" {
return nil, errors.Errorf("unexpected content type: %v", resp.Header.Get("Content-Type"))
}
if resp.Header.Get("Access-Control-Allow-Origin") != "*" {
return nil, errors.Errorf("cors headers weren't set in response")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Errorf("unable to read response body: %v", err)
}
return body, nil
}
func requireUID(t *testing.T, uid string) {
_, err := strconv.ParseUint(uid, 0, 64)
require.NoError(t, err)
}
func RequireNoGQLErrors(t *testing.T, resp *GraphQLResponse) {
require.Nil(t, resp.Errors,
"required no GraphQL errors, but received :\n%s", serializeOrError(resp.Errors))
}
func serializeOrError(toSerialize interface{}) string {
byts, err := json.Marshal(toSerialize)
if err != nil {
return "unable to serialize because " + err.Error()
}
return string(byts)
}
func PopulateGraphQLData(client *dgo.Dgraph, data []byte) error {
mu := &api.Mutation{
CommitNow: true,
SetJson: data,
}
_, err := client.NewTxn().Mutate(context.Background(), mu)
if err != nil {
return errors.Wrap(err, "Unable to add GraphQL test data")
}
return nil
}
func maybePopulateData(client *dgo.Dgraph, data []byte) error {
if data == nil {
return nil
}
// Helps in local dev to not re-add data multiple times.
countries, err := allCountriesAdded()
if err != nil {
return errors.Wrap(err, "couldn't determine if GraphQL data had already been added")
}
if len(countries) > 0 {
return nil
}
return PopulateGraphQLData(client, data)
}
func allCountriesAdded() ([]*country, error) {
body, err := json.Marshal(&GraphQLParams{Query: `query { queryCountry { name } }`})
if err != nil {
return nil, errors.Wrap(err, "unable to build GraphQL query")
}
req, err := http.NewRequest("POST", GraphqlURL, bytes.NewBuffer(body))
if err != nil {
return nil, errors.Wrap(err, "unable to build GraphQL request")
}
req.Header.Set("Content-Type", "application/json")
resp, err := runGQLRequest(req)
if err != nil {
return nil, errors.Wrap(err, "error running GraphQL query")
}
var result struct {
Data struct {
QueryCountry []*country
}
}
err = json.Unmarshal(resp, &result)
if err != nil {
return nil, errors.Wrap(err, "error trying to unmarshal GraphQL query result")
}
return result.Data.QueryCountry, nil
}
func checkGraphQLStarted(url string) error {
var err error
retries := 6
sleep := 10 * time.Second
// Because of how GraphQL starts (it needs to read the schema from Dgraph),
// there's no guarantee that GraphQL is available by now. So we
// need to try and connect and potentially retry a few times.
for retries > 0 {
retries--
_, err = hasCurrentGraphQLSchema(url)
if err == nil {
return nil
}
time.Sleep(sleep)
}
return err
}
func hasCurrentGraphQLSchema(url string) (bool, error) {
schemaQry := &GraphQLParams{
Query: `query { getGQLSchema { schema } }`,
}
req, err := schemaQry.createGQLPost(url)
if err != nil {
return false, errors.Wrap(err, "while creating gql post")
}
res, err := runGQLRequest(req)
if err != nil {
return false, errors.Wrap(err, "error running GraphQL query")
}
var result *GraphQLResponse
err = json.Unmarshal(res, &result)
if err != nil {
return false, errors.Wrap(err, "error unmarshalling result")
}
if len(result.Errors) > 0 {
return false, result.Errors
}
var sch struct {
GetGQLSchema struct {
Schema string
}
}
err = json.Unmarshal(result.Data, &sch)
if err != nil {
return false, errors.Wrap(err, "error trying to unmarshal GraphQL query result")
}
if sch.GetGQLSchema.Schema == "" {
return false, nil
}
return true, nil
}
func addSchema(url, schema string) error {
add := &GraphQLParams{
Query: `mutation updateGQLSchema($sch: String!) {
updateGQLSchema(input: { set: { schema: $sch }}) {
gqlSchema {
schema
}
}
}`,
Variables: map[string]interface{}{"sch": schema},
}
req, err := add.createGQLPost(url)
if err != nil {
return errors.Wrap(err, "error creating GraphQL query")
}
resp, err := runGQLRequest(req)
if err != nil {
return errors.Wrap(err, "error running GraphQL query")
}
var addResult struct {
Data struct {
UpdateGQLSchema struct {
GQLSchema struct {
Schema string
}
}
}
Errors []interface{}
}
err = json.Unmarshal(resp, &addResult)
if err != nil {
return errors.Wrap(err, "error trying to unmarshal GraphQL mutation result")
}
if len(addResult.Errors) > 0 {
return errors.Errorf("%v", addResult.Errors)
}
if addResult.Data.UpdateGQLSchema.GQLSchema.Schema == "" {
return errors.New("GraphQL schema mutation failed")
}
return nil
}
func addSchemaThroughAdminSchemaEndpt(url, schema string) error {
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(schema))
if err != nil {
return errors.Wrap(err, "error running GraphQL query")
}
resp, err := runGQLRequest(req)
if err != nil {
return errors.Wrap(err, "error running GraphQL query")
}
var addResult struct {
Data struct {
Code string
Message string
}
}
err = json.Unmarshal(resp, &addResult)
if err != nil {
return errors.Wrap(err, "error trying to unmarshal GraphQL mutation result")
}
if addResult.Data.Code != "Success" && addResult.Data.Message != "Done" {
return errors.New("GraphQL schema mutation failed")
}
return nil
}