github.com/weaviate/weaviate@v1.24.6/test/acceptance/classifications/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 test
    13  
    14  import (
    15  	"encoding/json"
    16  	"fmt"
    17  	"testing"
    18  
    19  	"github.com/go-openapi/runtime"
    20  	"github.com/weaviate/weaviate/client/graphql"
    21  	graphql_client "github.com/weaviate/weaviate/client/graphql"
    22  	"github.com/weaviate/weaviate/entities/models"
    23  	"github.com/weaviate/weaviate/test/helper"
    24  )
    25  
    26  type GraphQLResult struct {
    27  	Result interface{}
    28  }
    29  
    30  // Perform a GraphQL request
    31  func QueryGraphQL(t *testing.T, auth runtime.ClientAuthInfoWriterFunc, operation string, query string, variables map[string]interface{}) (*models.GraphQLResponse, error) {
    32  	var vars interface{} = variables
    33  	params := graphql_client.NewGraphqlPostParams().WithBody(&models.GraphQLQuery{OperationName: operation, Query: query, Variables: vars})
    34  	response, err := helper.Client(t).Graphql.GraphqlPost(params, nil)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	return response.Payload, nil
    40  }
    41  
    42  // Perform a query and assert that it is successful
    43  func AssertGraphQL(t *testing.T, auth runtime.ClientAuthInfoWriterFunc, query string) *GraphQLResult {
    44  	response, err := QueryGraphQL(t, auth, "", query, nil)
    45  	if err != nil {
    46  		parsedErr, ok := err.(*graphql.GraphqlPostUnprocessableEntity)
    47  		if !ok {
    48  			t.Fatalf("Expected the query to succeed, but failed due to: %#v", err)
    49  		}
    50  		t.Fatalf("Expected the query to succeed, but failed with unprocessable entity: %v", parsedErr.Payload.Error[0])
    51  	}
    52  
    53  	if len(response.Errors) != 0 {
    54  		j, _ := json.Marshal(response.Errors)
    55  		t.Fatal("GraphQL resolved to an error:", string(j))
    56  	}
    57  
    58  	data := make(map[string]interface{})
    59  
    60  	// get rid of models.JSONData
    61  	for key, value := range response.Data {
    62  		data[key] = value
    63  	}
    64  
    65  	return &GraphQLResult{Result: data}
    66  }
    67  
    68  // Drill down in the result
    69  func (g GraphQLResult) Get(paths ...string) *GraphQLResult {
    70  	current := g.Result
    71  	for _, path := range paths {
    72  		var ok bool
    73  		currentAsMap := (current.(map[string]interface{}))
    74  		current, ok = currentAsMap[path]
    75  		if !ok {
    76  			panic(fmt.Sprintf("Cannot get element %s in %#v; result: %#v", path, paths, g.Result))
    77  		}
    78  	}
    79  
    80  	return &GraphQLResult{
    81  		Result: current,
    82  	}
    83  }
    84  
    85  // Cast the result to a slice
    86  func (g *GraphQLResult) AsSlice() []interface{} {
    87  	return g.Result.([]interface{})
    88  }