github.com/weaviate/weaviate@v1.24.6/test/helper/graphql/graphql_helper.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package graphqlhelper 13 14 import ( 15 "encoding/json" 16 "fmt" 17 "strings" 18 "testing" 19 20 "github.com/go-openapi/runtime" 21 "github.com/stretchr/testify/require" 22 "github.com/weaviate/weaviate/client/graphql" 23 graphql_client "github.com/weaviate/weaviate/client/graphql" 24 "github.com/weaviate/weaviate/entities/models" 25 "github.com/weaviate/weaviate/test/helper" 26 ) 27 28 type GraphQLResult struct { 29 Result interface{} 30 } 31 32 // Perform a GraphQL request 33 func QueryGraphQL(t *testing.T, auth runtime.ClientAuthInfoWriterFunc, operation string, query string, variables map[string]interface{}) (*models.GraphQLResponse, error) { 34 var vars interface{} = variables 35 params := graphql_client.NewGraphqlPostParams().WithBody(&models.GraphQLQuery{OperationName: operation, Query: query, Variables: vars}) 36 response, err := helper.Client(t).Graphql.GraphqlPost(params, nil) 37 if err != nil { 38 return nil, err 39 } 40 41 return response.Payload, nil 42 } 43 44 // Perform a GraphQL request and call fatal on failure 45 func QueryGraphQLOrFatal(t *testing.T, auth runtime.ClientAuthInfoWriterFunc, operation string, query string, variables map[string]interface{}) *models.GraphQLResponse { 46 response, err := QueryGraphQL(t, auth, operation, query, variables) 47 if err != nil { 48 parsedErr, ok := err.(*graphql.GraphqlPostUnprocessableEntity) 49 if !ok { 50 t.Fatalf("Expected the query to succeed, but failed due to: %#v", err) 51 } 52 t.Fatalf("Expected the query to succeed, but failed with unprocessable entity: %v", parsedErr.Payload.Error[0]) 53 } 54 return response 55 } 56 57 // Perform a query and assert that it is successful 58 func AssertGraphQL(t *testing.T, auth runtime.ClientAuthInfoWriterFunc, query string) *GraphQLResult { 59 response := QueryGraphQLOrFatal(t, auth, "", query, nil) 60 61 if len(response.Errors) != 0 { 62 j, _ := json.Marshal(response.Errors) 63 t.Fatal("GraphQL resolved to an error:", string(j)) 64 } 65 66 data := make(map[string]interface{}) 67 68 // get rid of models.JSONData 69 for key, value := range response.Data { 70 data[key] = value 71 } 72 73 return &GraphQLResult{Result: data} 74 } 75 76 // Perform a query and assert that it has errors 77 func ErrorGraphQL(t *testing.T, auth runtime.ClientAuthInfoWriterFunc, query string) []*models.GraphQLError { 78 response := QueryGraphQLOrFatal(t, auth, "", query, nil) 79 80 if len(response.Errors) == 0 { 81 j, _ := json.Marshal(response.Errors) 82 t.Fatal("GraphQL resolved to data:", string(j)) 83 } 84 85 return response.Errors 86 } 87 88 // Drill down in the result 89 func (g GraphQLResult) Get(paths ...string) *GraphQLResult { 90 current := g.Result 91 for _, path := range paths { 92 var ok bool 93 currentAsMap := (current.(map[string]interface{})) 94 current, ok = currentAsMap[path] 95 if !ok { 96 panic(fmt.Sprintf("Cannot get element %s in %#v; result: %#v", path, paths, g.Result)) 97 } 98 } 99 100 return &GraphQLResult{ 101 Result: current, 102 } 103 } 104 105 // Cast the result to a slice 106 func (g *GraphQLResult) AsSlice() []interface{} { 107 return g.Result.([]interface{}) 108 } 109 110 func Vec2String(v []float32) (s string) { 111 for _, n := range v { 112 s = fmt.Sprintf("%s, %f", s, n) 113 } 114 s = strings.TrimLeft(s, ", ") 115 return fmt.Sprintf("[%s]", s) 116 } 117 118 func ParseVec(t *testing.T, iVec []interface{}) []float32 { 119 vec := make([]float32, len(iVec)) 120 for i, val := range iVec { 121 parsed, err := val.(json.Number).Float64() 122 require.Nil(t, err) 123 vec[i] = float32(parsed) 124 } 125 return vec 126 }