github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/log/gql_ops_logger_test.go (about)

     1  package log
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/99designs/gqlgen/graphql"
     8  	"github.com/vektah/gqlparser/v2/ast"
     9  )
    10  
    11  const testOperationName = "myOperation"
    12  
    13  func TestGraphQlRequestDetailsLogging(t *testing.T) {
    14  	t.Run("log GQL query details", func(t *testing.T) {
    15  		ctx := ContextWithMdc(context.Background())
    16  		ctx = graphql.WithOperationContext(ctx, &graphql.OperationContext{
    17  			Operation: &ast.OperationDefinition{
    18  				Operation: ast.Query,
    19  				SelectionSet: []ast.Selection{&ast.Field{
    20  					Name: testOperationName,
    21  				}},
    22  			},
    23  		})
    24  
    25  		middleware := NewGqlLoggingInterceptor()
    26  		middleware.InterceptOperation(ctx, func(ctx context.Context) graphql.ResponseHandler {
    27  			return nil
    28  		})
    29  
    30  		mdc := MdcFromContext(ctx)
    31  		opType, ok := mdc.mdc[logKeyOperationType]
    32  		if !ok || opType.(string) != string(ast.Query) {
    33  			t.Errorf("The GraphQL operation type is missing or incorrect. Expected=%v; Actual=%v", ast.Query, opType)
    34  		}
    35  
    36  		opName, ok := mdc.mdc[logKeySelectionSet]
    37  		if !ok || opName.(string) != testOperationName {
    38  			t.Errorf("The GraphQL operation name is missing or incorrect: %v", opName)
    39  		}
    40  	})
    41  }