graphql-go-tools
graphql-go-tools copied to clipboard
Bug: removing a directive from a FieldDefinition removes the type as well
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}