Skip to content

Commit 9a26e5c

Browse files
authored
fix: disable minifier for gRPC datasource (#1249)
Minification can lead to fragments to be created to reduce the size of operation. We want to reduce the amount of fragments in gRCP Datasource.
1 parent d9cfb21 commit 9a26e5c

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

v2/pkg/engine/datasource/graphql_datasource/graphql_datasource.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,8 @@ func (p *Planner[T]) generateQueryPlansForFetchConfiguration(operation *ast.Docu
14351435
}
14361436
}
14371437

1438-
// printOperation - prints normalized upstream operation
1438+
// printOperation returns normalized and validated upstream operation, optionally minified.
1439+
// Errors encountered during processing terminate the operation, and nils are returned.
14391440
func (p *Planner[T]) printOperation() (operationBytes []byte, variablesBytes []byte) {
14401441

14411442
kit := p.getKit()
@@ -1448,8 +1449,8 @@ func (p *Planner[T]) printOperation() (operationBytes []byte, variablesBytes []b
14481449
return nil, nil
14491450
}
14501451

1451-
// When datasource is nested and definition query type do not contain operation field
1452-
// we have to replace a query type with a current root type
1452+
// When datasource is nested and definition's query type does not contain operation field
1453+
// we have to replace a query type with a current root type.
14531454
p.replaceQueryType(definition)
14541455

14551456
// normalize upstream operation
@@ -1483,7 +1484,8 @@ func (p *Planner[T]) printOperation() (operationBytes []byte, variablesBytes []b
14831484
rawOperationBytes := make([]byte, kit.buf.Len())
14841485
copy(rawOperationBytes, kit.buf.Bytes())
14851486

1486-
if p.minifier != nil && len(rawOperationBytes) > 140 {
1487+
// gRPC DataSource requires minification to be disabled.
1488+
if p.minifier != nil && !p.config.IsGRPC() && len(rawOperationBytes) > 140 {
14871489
kit.buf.Reset()
14881490
madeReplacements, err := p.minifier.Minify(rawOperationBytes, definition, astminify.MinifyOptions{
14891491
SortAST: true,
@@ -1596,7 +1598,7 @@ func (p *Planner[T]) replaceQueryType(definition *ast.Document) {
15961598
definition.ReplaceRootOperationTypeDefinition(p.rootTypeName, ast.OperationTypeQuery)
15971599
}
15981600

1599-
// normalizeOperation - normalizes operation against definition.
1601+
// normalizeOperation normalizes operation against definition.
16001602
func (p *Planner[T]) normalizeOperation(operation, definition *ast.Document, report *operationreport.Report) (ok bool) {
16011603
report.Reset()
16021604
normalizer := astnormalization.NewWithOpts(
@@ -1611,6 +1613,7 @@ func (p *Planner[T]) normalizeOperation(operation, definition *ast.Document, rep
16111613
return !report.HasErrors()
16121614
}
16131615

1616+
// handleFieldAlias determines the appropriate field name and alias for a given field reference.
16141617
func (p *Planner[T]) handleFieldAlias(ref int) (newFieldName string, alias ast.Alias) {
16151618
fieldName := p.visitor.Operation.FieldNameString(ref)
16161619
alias = ast.Alias{
@@ -1650,7 +1653,7 @@ func (p *Planner[T]) handleFieldAlias(ref int) (newFieldName string, alias ast.A
16501653
return fieldName, alias
16511654
}
16521655

1653-
// addField - add a field to an upstream operation
1656+
// addField adds a field referenced by ref to the upstream operation.
16541657
func (p *Planner[T]) addField(ref int) (upstreamFieldRef int) {
16551658
fieldName, alias := p.handleFieldAlias(ref)
16561659

@@ -1673,7 +1676,7 @@ func (p *Planner[T]) addField(ref int) (upstreamFieldRef int) {
16731676
type OnWsConnectionInitCallback func(ctx context.Context, url string, header http.Header) (json.RawMessage, error)
16741677

16751678
type printKit struct {
1676-
buf *bytes.Buffer
1679+
buf *bytes.Buffer // output goes here
16771680
parser *astparser.Parser
16781681
printer *astprinter.Printer
16791682
validator *astvalidation.OperationValidator
@@ -1737,9 +1740,9 @@ func NewFactory(executionContext context.Context, httpClient *http.Client, subsc
17371740
}, nil
17381741
}
17391742

1740-
// NewFactory (GRPC) creates a new factory for the GraphQL datasource planner
1743+
// NewFactoryGRPC creates a gRPC factory for the GraphQL datasource planner.
17411744
// Graphql Datasource could be stateful in case you are using subscriptions,
1742-
// make sure you are using the same execution context for all datasources
1745+
// make sure you are using the same execution context for all datasources.
17431746
func NewFactoryGRPC(executionContext context.Context, grpcClient grpc.ClientConnInterface) (*Factory[Configuration], error) {
17441747
if executionContext == nil {
17451748
return nil, fmt.Errorf("execution context is required")
@@ -1759,7 +1762,7 @@ func NewFactoryGRPC(executionContext context.Context, grpcClient grpc.ClientConn
17591762
// This factory is used when the gRPC client is provided by a function.
17601763
// This is useful when you don't want to provide a static client to the factory and let the consumer
17611764
// decide how to provide the client to the datasource.
1762-
// For example when you need to recreate the client in case of a connection error.
1765+
// For example, when you need to recreate the client in case of a connection error.
17631766
func NewFactoryGRPCClientProvider(executionContext context.Context, clientProvider func() grpc.ClientConnInterface) (*Factory[Configuration], error) {
17641767
if executionContext == nil {
17651768
return nil, fmt.Errorf("execution context is required")

0 commit comments

Comments
 (0)