Skip to content

Commit

Permalink
fix(astprinter): implement transitive interface output
Browse files Browse the repository at this point in the history
Just copied relevant part from EnterObjectTypeDefinition()

Also add tests

Fixes wundergraph#1018
  • Loading branch information
lorf committed Dec 29, 2024
1 parent 7d86ddc commit 9c63f7c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/astprinter/astprinter.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,20 @@ func (p *printVisitor) EnterInterfaceTypeDefinition(ref int) {
p.write(p.document.InterfaceTypeDefinitionNameBytes(ref))
p.write(literal.SPACE)

if len(p.document.InterfaceTypeDefinitions[ref].ImplementsInterfaces.Refs) != 0 {
p.write(literal.IMPLEMENTS)
p.write(literal.SPACE)
for i, j := range p.document.InterfaceTypeDefinitions[ref].ImplementsInterfaces.Refs {
if i != 0 {
p.write(literal.SPACE)
p.write(literal.AND)
p.write(literal.SPACE)
}
p.must(p.document.PrintType(j, p.out))
}
p.write(literal.SPACE)
}

p.inputValueDefinitionOpener = literal.LPAREN
p.inputValueDefinitionCloser = literal.RPAREN
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/astprinter/astprinter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@ vary: [String]! = []) on QUERY directive @include(if: Boolean!) repeatable on FI
`scalar Date schema {query: Query} type Query {me: User! user(id: ID!): User allUsers: [User] search(term: String!): [SearchResult!]! myChats: [Chat!]!} enum Role {USER ADMIN} interface Node {id: ID!} union SearchResult = User | Chat | ChatMessage type User implements Node {id: ID! username: String! email: String! role: Role!} type Chat implements Node {id: ID! users: [User!]! messages: [ChatMessage!]!} type ChatMessage implements Node {id: ID! content: String! time: Date! user: User!}`)
})
})
t.Run("transitive interfaces", func(t *testing.T) {
run(t, "interface I1 {id: ID!} interface I2 implements I1 {id: ID!} interface I3 implements I1 & I2 {id: ID!}",
"interface I1 {id: ID!} interface I2 implements I1 {id: ID!} interface I3 implements I1 & I2 {id: ID!}")
})
}

func TestPrintArgumentWithBeforeAfterValue(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions v2/pkg/astprinter/astprinter.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,20 @@ func (p *printVisitor) EnterInterfaceTypeDefinition(ref int) {
p.write(p.document.InterfaceTypeDefinitionNameBytes(ref))
p.write(literal.SPACE)

if len(p.document.InterfaceTypeDefinitions[ref].ImplementsInterfaces.Refs) != 0 {
p.write(literal.IMPLEMENTS)
p.write(literal.SPACE)
for i, j := range p.document.InterfaceTypeDefinitions[ref].ImplementsInterfaces.Refs {
if i != 0 {
p.write(literal.SPACE)
p.write(literal.AND)
p.write(literal.SPACE)
}
p.must(p.document.PrintType(j, p.out))
}
p.write(literal.SPACE)
}

p.inputValueDefinitionOpener = literal.LPAREN
p.inputValueDefinitionCloser = literal.RPAREN
}
Expand Down
4 changes: 4 additions & 0 deletions v2/pkg/astprinter/astprinter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,10 @@ fragment NameFragment on Dog {
`scalar Date schema {query: Query} type Query {me: User! user(id: ID!): User allUsers: [User] search(term: String!): [SearchResult!]! myChats: [Chat!]!} enum Role {USER ADMIN} interface Node {id: ID!} union SearchResult = User | Chat | ChatMessage type User implements Node {id: ID! username: String! email: String! role: Role!} type Chat implements Node {id: ID! users: [User!]! messages: [ChatMessage!]!} type ChatMessage implements Node {id: ID! content: String! time: Date! user: User!}`)
})
})
t.Run("transitive interfaces", func(t *testing.T) {
run(t, "interface I1 {id: ID!} interface I2 implements I1 {id: ID!} interface I3 implements I1 & I2 {id: ID!}",
"interface I1 {id: ID!} interface I2 implements I1 {id: ID!} interface I3 implements I1 & I2 {id: ID!}")
})
}

func TestPrintArgumentWithBeforeAfterValue(t *testing.T) {
Expand Down

0 comments on commit 9c63f7c

Please sign in to comment.