99designs / gqlgen

go generate based graphql server library

Home Page:https://gqlgen.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

generate cannot resolve custom slice type

JustinDFuller opened this issue · comments

What happened?

I used autobind to connect graphql to my custom types. My custom types include a field that is a type with the same underlying slice. However, when I do this, the graph/schema.resolvers.go now includes a resolver function for that type.

What did you expect?

If I change my custom type to use []User instead of Users then the custom resolver goes away.

I believe gqlgen should understand that this is the same type and should not require me to implement a custom resolver.

Minimal graphql.schema and models to reproduce

type Todo {
  users: [User!]!
}

type User {
  id: ID!
  name: String!
}

type Query {
  todos: [Todo!]!
}
package graphql

type Todo struct {
        Users Users
}

type Users []User

type User struct {
        ID string
        Username string
        Extra string
}
autobind:
  - "path/to/my/types"

versions

  • go run github.com/99designs/gqlgen version? v0.17.40
  • go version? go version go1.21.3 linux/amd64

Extra Notes

Is this due to the type resolution not checking the underlying type for matches? If so, I am happy to try to implement this. I've started looking through the code but would appreciate pointers for where to look.

You can use scalar for example: scalar Users

And

package model

import (
	"encoding/json"
	"io"
)

type Users []User

func (u Users) MarshalGQL(w io.Writer) {
	data, _ := json.Marshal(u)
	w.Write(data)
}

func (u *Users) UnmarshalGQL(v interface{}) error {
	return nil
}

yml =>


models:
  ID:
    model:
      - github.com/99designs/gqlgen/graphql.Uint64
      - github.com/99designs/gqlgen/graphql.ID
      - github.com/99designs/gqlgen/graphql.Int
      - github.com/99designs/gqlgen/graphql.Int32
  Int:
    model:
      - github.com/99designs/gqlgen/graphql.Int
      - github.com/99designs/gqlgen/graphql.Int64
      - github.com/99designs/gqlgen/graphql.Int32
  Users:
    model:
      - user/graph/model.Users

and then run generate