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

fix implementation type generation for fragment on a union #310

Merged
merged 6 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ When releasing a new version:
### Bug fixes:
- The presence of negative pointer directives, i.e., `# @genqlient(pointer: false)` are now respected even in the when `optional: pointer` is set in the configuration file.
- Made name collisions between query/mutation arguments and local function variables less likely.
- Fix generation issue related to golang type implementation of complex graphql union fragments

## v0.6.0

Expand Down
11 changes: 11 additions & 0 deletions generate/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,17 @@ func fragmentMatches(containingTypedef, fragmentTypedef *ast.Definition) bool {
return true
}
}

// Handle the special case where the fragment is on a union, then the
// fragment can match any of the types in the union.
if fragmentTypedef.Kind == ast.Union {
for _, typeName := range fragmentTypedef.Types {
if typeName == containingTypedef.Name {
return true
}
}
}

return false
}

Expand Down
31 changes: 31 additions & 0 deletions generate/testdata/queries/ComplexNamedFragments.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,34 @@ fragment ContentFields on Content {
query ComplexNamedFragments {
... on Query { ...QueryFragment }
}

## two fragments of different types with fields containing the same inline named fragment of a union
fragment SimpleLeafContent on LeafContent {
... on Article {
id
}
... on Video {
id
}
}

fragment UserLastContent on User {
lastContent {
... SimpleLeafContent
}
}

fragment TopicNewestContent on Topic {
newestContent {
... SimpleLeafContent
}
}

query ComplexNamedFragmentsWithInlineUnion {
user {
...UserLastContent
}
root {
...TopicNewestContent
}
}
4 changes: 4 additions & 0 deletions generate/testdata/queries/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type User {
pokemon: [Pokemon!]
greeting: Clip
birthdate: Date

lastContent: LeafContent
}

"""An audio clip, such as of a user saying hello."""
Expand Down Expand Up @@ -141,6 +143,8 @@ type Topic implements Content {
schoolGrade: String
next: Topic
related: [Topic!]

newestContent: LeafContent
}

input RecursiveInput {
Expand Down
Loading
Loading