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

typename doesn't work on basic types. #130

Closed
csilvers opened this issue Oct 4, 2021 · 3 comments · Fixed by #133
Closed

typename doesn't work on basic types. #130

csilvers opened this issue Oct 4, 2021 · 3 comments · Fixed by #133
Labels
enhancement New feature or request

Comments

@csilvers
Copy link
Member

csilvers commented Oct 4, 2021

It seems like the typename directive only works on structs. But it would be great if it worked on basic types like string, so you could do:

query x {
    # @genqlient(typename: "RenderType")
    renderType
}

where renderType is a string in the graphql schema, and the generated code would be:

type RenderType string
struct x {
    renderType RenderType
}
@csilvers csilvers added the bug Something isn't working label Oct 4, 2021
@csilvers
Copy link
Member Author

csilvers commented Oct 4, 2021

I tried to implement this by modifying convertDefinition, but I don't think that will work: that function assumes that there's a 1-to-1 mapping from Go types to Graphql types, and that isn't necessarily true in this case. So maybe this is more a feature request than a bug.

I think the real issue is that directives apply not only to a node but also its children. So if you have:

  # @genqlient(typename: "User")
  users { id name }

then it tries to apply the typename: "User" to id and name as well.

I don't understand how this doesn't cause problems elsewhere, like if you had:

  # @genqlient(typename: "User")
  users { id name { first last } }

wouldn't it cause the type for name { first last } to be User as well? But it definitely causes problems for this!

@csilvers
Copy link
Member Author

csilvers commented Oct 4, 2021

OK, I think it is a bug in genqlient. WIll file separately.

@csilvers
Copy link
Member Author

csilvers commented Oct 4, 2021

I think this can't be implemented until #131 is resolved. But once it is, this diff might be sufficient:

--- a/generate/convert.go
+++ b/generate/convert.go
@@ -289,7 +289,7 @@ func (g *generator) convertDefinition(
                }, err
        }
        goBuiltinName, ok := builtinTypes[def.Name]
-       if ok {
+       if ok && options.TypeName == "" {
                return &goOpaqueType{GoRef: goBuiltinName, GraphQLName: def.Name
}, nil
        }
 
@@ -479,6 +479,12 @@ func (g *generator) convertDefinition(
                return g.addType(goType, goType.GoName, pos)
 
        case ast.Scalar:
+               if builtinTypes[def.Name] != "" {
+                       // In this case, the user asked for a custom Go type-nam
e
+                       // for a built-in type, e.g. `typename MyString string`.
+                       return &goOpaqueType{GoRef: name, GraphQLName: def.Name}
, nil
+               }
+

(plus changing the tests to test for it: I changed it liks this:

--- a/generate/testdata/queries/TypeNames.graphql
+++ b/generate/testdata/queries/TypeNames.graphql
@@ -3,7 +3,11 @@ query TypeNames {
   # @genqlient(typename: "User")
   user { id name }
   # @genqlient(typename: "Item")
-  randomItem { id name }
+  randomItem {
+     id
+     # @genqlient(typename: "NameType")
+     name
+  }
   # (ok to reuse the name as long as they match)
   # @genqlient(typename: "User")
   users { id name }

)

@csilvers csilvers added enhancement New feature or request and removed bug Something isn't working labels Oct 4, 2021
csilvers added a commit that referenced this issue Oct 4, 2021
This lets you write code like:
```
query x {
   # @genqlient(typename: "MyString")
   someStringField
}
```
and genqlient will do
```
typename MyString string
type x struct {
   someStringField MyString
}
```

This was not difficult to implement, though it required introducing a
new identifier type.  The main difficulty I had was weird test
failures, that it turns out was due to the tests putting a bunch of
fields on the same line, so that the genqlient directive on the
previous line applied to all of them, accidentally.  This became a
problem when `typename` suddenly started being respected for builtin
types!  I fixed it by just spreading out the queries a bit.

Fixes #130
csilvers added a commit that referenced this issue Oct 5, 2021
This lets you write code like:
```
query x {
   # @genqlient(typename: "MyString")
   someStringField
}
```
and genqlient will do
```
typename MyString string
type x struct {
   someStringField MyString
}
```

This was not difficult to implement, though it required introducing a
new identifier type.  The main difficulty I had was weird test
failures, that it turns out was due to the tests putting a bunch of
fields on the same line, so that the genqlient directive on the
previous line applied to all of them, accidentally.  This became a
problem when `typename` suddenly started being respected for builtin
types!  I fixed it by just spreading out the queries a bit.

Fixes #130
csilvers added a commit that referenced this issue Oct 5, 2021
## Summary:
This lets you write code like:
```
query x {
   # @genqlient(typename: "MyString")
   someStringField
}
```
and genqlient will do
```
typename MyString string
type x struct {
   someStringField MyString
}
```

This was not difficult to implement, though it required introducing a
new identifier type.  The main difficulty I had was weird test
failures, that it turns out was due to the tests putting a bunch of
fields on the same line, so that the genqlient directive on the
previous line applied to all of them, accidentally.  This became a
problem when `typename` suddenly started being respected for builtin
types!  I fixed it by just spreading out the queries a bit.

Fixes #130

## Test plan:
make check

Author: csilvers

Reviewers: dnerdy, StevenACoffman, benjaminjkraft

Required Reviewers:

Approved By: StevenACoffman

Checks: ✅ Test (1.17), ✅ Test (1.16), ✅ Test (1.15), ✅ Test (1.14), ✅ Lint, ✅ Test (1.17), ✅ Test (1.16), ✅ Test (1.15), ✅ Test (1.14), ✅ Lint

Pull Request URL: #133
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant