99designs / 99designs/gqlgen

Interface getters don't work correctly with methods with pointer

Open
#2,352 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
10.8k
Forks
1.3k
Avg merge
2d 36m
Merged PRs (30d)
26

Description

After adding getters, I had some problems.

If the struct method uses a pointer, the getters generator is still trying to take a pointer. As a result, for example, we get the following error: ```Cannot use '*this.Node' (type User) as the type Node Type does not implement 'Node' as the 'GetID' method has a pointer receiver```

More unpleasant changes for me:
- Also, naming GetID is not very convenient for me, I bind objects to proto generation, where the function creates the name GetId, it would be cool if the generator understood that the object has a different name.
- Let's say I make the interface Editable, my objects implement it, before it was enough for me to say that it really is (the function returns true if it implements), now all Editable objects must necessarily implement all methods. Perhaps from the point of view of logic in the language, this is correct, but I broke the way of interacting with graphql
```golang
type Editable interface {
IsEditable()
GetViewerCanEdit() bool
// more methods which for some reason I have to implement for all objects...
}
```
It seems there is a similar problem here #2311

### Minimal graphql.schema and models to reproduce

```graphql

scalar Cursor

directive @goModel(model: String, models: [String!]) on OBJECT | INPUT_OBJECT | SCALAR | ENUM | INTERFACE | UNION

interface Node {
id: ID!
}

interface Edge {
cursor: Cursor!
node: Node!
}

interface Connection {
edges: [Edge!]!
nodes: [Node!]!
pageInfo: PageInfo!
}

type PageInfo {
startCursor: Cursor
endCursor: Cursor
hasNextPage: Boolean!
hasPreviousPage: Boolean!
}

type User implements Node @goModel(model: "getters/graph/model.User") {
id: ID!
name: String!
}

type UserEdge implements Edge {
cursor: Cursor!
node: User!
}

type UsersConnection implements Connection {
edges: [UserEdge!]!
nodes: [User!]!
pageInfo: PageInfo!
}

```
+
```golang
type User struct {
Id string
}

func (x User) IsNode() {}

func (x *User) GetID() string {
return x.Id
}
```

### versions
gqlgen v0.17.16

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.