graphql-go-tools icon indicating copy to clipboard operation
graphql-go-tools copied to clipboard

Bug: removing a directive from a FieldDefinition removes the type as well

Open darren-west opened this issue 1 year ago • 0 comments

Hi I was looking at whats possible with the astparser and ran into an issue where calling RemoveDirectiveByName also removed the type from the field.

I was running out of time but have knocked together a very quick test to demonstrate

func TestShowBug(t *testing.T) {
	doc, report := astparser.ParseGraphqlDocumentString(`directive @foo on OBJECT | FIELD_DEFINITION type Query{ hello: String! @foo }`)
	if report.HasErrors() {
		t.Fatal(report.Error())
	}
	schemaAST := &doc

	walker := astvisitor.NewWalker(5)

	visitor := removeFieldDirectiveVisitor{
		definition: schemaAST,
	}
	walker.RegisterLeaveDirectiveVisitor(visitor)

	walker.Walk(schemaAST, nil, new(operationreport.Report))

	output, err := astprinter.PrintString(schemaAST, nil)
	if err != nil {
		panic(err)
	}
	if output != `directive @foo on OBJECT | FIELD_DEFINITION type Query{ hello: String! }` {
		t.Fatal("got = ", output)
	}
}

type removeFieldDirectiveVisitor struct {
	definition *ast.Document
}

func (r removeFieldDirectiveVisitor) LeaveDirective(ref int) {
	r.definition.FieldDefinitions[ref].Directives.RemoveDirectiveByName(r.definition, "foo")
}


What you end up with is this

got = directive @foo on OBJECT | FIELD_DEFINITION type Query {hello}

darren-west avatar Oct 10 '23 19:10 darren-west