Skip to content

Commit

Permalink
fix implementation type generation for fragment on a union (#310)
Browse files Browse the repository at this point in the history
before the fix

```
=== RUN   TestGenerate/ComplexNamedFragments.graphql/Build
# command-line-arguments
/genqlient/generate/testdata/tmp/ComplexNamedFragments.graphql_727791265.go:2008:8: cannot use new(TopicNewestContentNewestContentArticle) (value of type *TopicNewestContentNewestContentArticle) as type TopicNewestContentNewestContentLeafContent in assignment:
	*TopicNewestContentNewestContentArticle does not implement TopicNewestContentNewestContentLeafContent (missing implementsGraphQLInterfaceSimpleLeafContent method)
/genqlient/generate/testdata/tmp/ComplexNamedFragments.graphql_727791265.go:2011:8: cannot use new(TopicNewestContentNewestContentVideo) (value of type *TopicNewestContentNewestContentVideo) as type TopicNewestContentNewestContentLeafContent in assignment:
	*TopicNewestContentNewestContentVideo does not implement TopicNewestContentNewestContentLeafContent (missing implementsGraphQLInterfaceSimpleLeafContent method)
/genqlient/generate/testdata/tmp/ComplexNamedFragments.graphql_727791265.go:2026:7: impossible type switch case: *TopicNewestContentNewestContentArticle
	(*v) (variable of type TopicNewestContentNewestContentLeafContent) cannot have dynamic type *TopicNewestContentNewestContentArticle (missing implementsGraphQLInterfaceSimpleLeafContent method)
/genqlient/generate/testdata/tmp/ComplexNamedFragments.graphql_727791265.go:2034:7: impossible type switch case: *TopicNewestContentNewestContentVideo
	(*v) (variable of type TopicNewestContentNewestContentLeafContent) cannot have dynamic type *TopicNewestContentNewestContentVideo (missing implementsGraphQLInterfaceSimpleLeafContent method)
/genqlient/generate/testdata/tmp/ComplexNamedFragments.graphql_727791265.go:2177:8: cannot use new(UserLastContentLastContentArticle) (value of type *UserLastContentLastContentArticle) as type UserLastContentLastContentLeafContent in assignment:
	*UserLastContentLastContentArticle does not implement UserLastContentLastContentLeafContent (missing implementsGraphQLInterfaceSimpleLeafContent method)
/genqlient/generate/testdata/tmp/ComplexNamedFragments.graphql_727791265.go:2180:8: cannot use new(UserLastContentLastContentVideo) (value of type *UserLastContentLastContentVideo) as type UserLastContentLastContentLeafContent in assignment:
	*UserLastContentLastContentVideo does not implement UserLastContentLastContentLeafContent (missing implementsGraphQLInterfaceSimpleLeafContent method)
/genqlient/generate/testdata/tmp/ComplexNamedFragments.graphql_727791265.go:2195:7: impossible type switch case: *UserLastContentLastContentArticle
	(*v) (variable of type UserLastContentLastContentLeafContent) cannot have dynamic type *UserLastContentLastContentArticle (missing implementsGraphQLInterfaceSimpleLeafContent method)
/genqlient/generate/testdata/tmp/ComplexNamedFragments.graphql_727791265.go:2203:7: impossible type switch case: *UserLastContentLastContentVideo
	(*v) (variable of type UserLastContentLastContentLeafContent) cannot have dynamic type *UserLastContentLastContentVideo (missing implementsGraphQLInterfaceSimpleLeafContent method)
    /genqlient/generate/generate_test.go:120: generated code does not compile: exit status 2
```

after fix

https://github.com/Khan/genqlient/pull/310/files#diff-3eaa9f1b68120a67e7558f37dd5984e995a6df5d454656180befca84c2dbf53aR2164


I also took a look at #64 which
is the real underlaying issue i wanted to fix but that seems to be
extremely complicated and probably needs multiple weeks of work. the
nested interface issues came up on a query with union spreading. this PR
fixes the problem of spreading when the union is the same fragment,
however if the spread contains different fragment we run into #64 .
  • Loading branch information
zzh8829 committed Feb 16, 2024
1 parent 20a8c5e commit c14574a
Show file tree
Hide file tree
Showing 6 changed files with 980 additions and 76 deletions.
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

0 comments on commit c14574a

Please sign in to comment.