Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Printing GraphQL documents is slow #674

Open
jturkel opened this issue Jul 26, 2023 · 1 comment · May be fixed by #682
Open

Printing GraphQL documents is slow #674

jturkel opened this issue Jul 26, 2023 · 1 comment · May be fixed by #682

Comments

@jturkel
Copy link

jturkel commented Jul 26, 2023

We did some benchmarking of a possible GraphQL gateway built on top of this library and noticed that serializing queries takes hundreds of milliseconds. Here's a benchmark that demonstrate the issue and compares results to wundergraph/graphql-go-tools:

package main

import (
	"os"
	"testing"

	"github.com/graphql-go/graphql/language/parser"
	"github.com/graphql-go/graphql/language/printer"

	"github.com/wundergraph/graphql-go-tools/pkg/astparser"
	"github.com/wundergraph/graphql-go-tools/pkg/astprinter"
)

func BenchmarkPrinter_GraphQLGo(b *testing.B) {
	query, err := os.ReadFile("benchmark_query.graphql")
	if err != nil {
		panic(err)
	}

	document, err := parser.Parse(parser.ParseParams{
		Source: string(query),
	})
	if err != nil {
		panic(err)
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		if printer.Print(document) == "" {
			panic("print failed")
		}
	}
}

func BenchmarkPrinter_GraphQLGoTools(b *testing.B) {
	query, err := os.ReadFile("benchmark_query.graphql")
	if err != nil {
		panic(err)
	}

	document, report := astparser.ParseGraphqlDocumentBytes(query)
	if report.HasErrors() {
		panic(report.Error())
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_, err := astprinter.PrintString(&document, nil)
		if err != nil {
			panic(err)
		}
	}
}

The benchmark query can be found here and is an anonymized version of a real query in our system. On average graphql-go/graphql takes 303ms to print this query and wundergraph/graphql-go-tools takes 0.018 ms.

@adelsz
Copy link

adelsz commented Oct 25, 2023

We had just run into the same issue. Printer is terribly slow.

@adelsz adelsz linked a pull request Oct 25, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants