gqlgen: Code Generation Fails When Binding Types: “Unable to bind to interface”

gqlgen: Code Generation Fails When Binding Types: “Unable to bind to interface”


0

I am facing an issue with gqlgen’s code generation in my Go project. I have a GraphQL schema and a Go interface where I’m trying to bind a type CourseProduct to a Go struct. However, I’m encountering an error during the code generation process.

Here’s a snippet of my GraphQL schema:

interface Product @goModel(
    model: "project.com/internal/service/product/model.Product"
) {
  id: Int!
}

type CourseProduct implements Product @goModel(
    model: "project.com/internal/service.CourseWrapper2"
) {
  id: Int!
  title: String!
}

Go Interface:
Here’s my internal/service/product/model/product.go file:

package model

import (
    "time"
)

type Product interface {
    GetID() int
    GetTitle() string
    GetCreatedAt() time.Time
}


type Course struct {
    *ProductData
    CourseID  string
}

type ProductData struct {
    ID_        int
    Title_     string
    CreatedAt_ time.Time
}


var _ Product = (*ProductData)(nil)

func (this ProductData) GetID() ProductID        { return this.ID_ }
func (this ProductData) GetTitle() string        { return this.Title_ }
func (this ProductData) GetCreatedAt() time.Time { return this.CreatedAt_ }

file internal/service/product/wrapper1.go (parent of model package):

package product

import (
    model "project.com/internal/service/product/model"
)

type CourseWrapper1 struct {
    *model.Course
}

func (CourseWrapper1) IsGetProductByIDResult() {}

and file internal/service/wrapper2.go (grandparent of model package):

package service

import (
    model "project.com/internal/service/product/model"
)

type CourseWrapper2 struct {
    *model.Course
}

func (CourseWrapper2) IsGetProductByIDResult() {}

When I run go generate ./..., the code generation fails with the following error:

merging type systems failed: unable to bind to interface: project.com/internal/service.CourseWrapper2 does not satisfy the interface project.com/internal/service/product/model.Product
exit status 3
resolver.go:17: running "go": exit status 1
generate: open /path/to/project/api/graphql/generated/generated.go: no such file or directory

Interestingly, if I bind CourseProduct to project.com/internal/service/product.CourseWrapper1 (which is essentially the same struct but one package closer to model.Product), the error disappears.

Additional Observation:
If I comment out the GetCreatedAt() time.Time method in the model.Product interface, the code generates successfully even when binding to project.com/internal/service.CourseWrapper2.

I’ve tried debugging this issue, but it goes deep into go/types, golang.org/x/tools/go/packages/packages.go, and other internals of how Go loads and resolves packages and types.

What could be causing this issue, and how can I resolve it?


Load 6 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *