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

[Bug] GraphQL server returns interface type for __typename when UseFieldResolvers is enabled #630

Open
magicoder10 opened this issue Jan 21, 2024 · 2 comments

Comments

@magicoder10
Copy link

Input

Schema

interface Animal {
    name: String!
}

type Dog implements Animal {
    name: String!
    breed: String!
}

type Cat implements Animal {
    name: String!
    livesLeft: Int!
}

type Query {
    animals: [Animal!]!
}

Query

query {
  animals {
    name
    __typename
  }
}

Output

Without UseFieldResolvers

schema := graphql.MustParseSchema(schemaString, &query{})
	http.Handle("/graphql", &relay.Handler{Schema: schema})
	log.Fatal(http.ListenAndServe(":8080", nil))
{
  "data": {
    "animals": [
      {
        "name": "Buddy",
        "__typename": "Dog"
      },
      {
        "name": "Whiskers",
        "__typename": "Cat"
      }
    ]
  }
}

With UseFieldResolvers

schema := graphql.MustParseSchema(schemaString, &query{}, graphql.UseFieldResolvers())
	http.Handle("/graphql", &relay.Handler{Schema: schema})
	log.Fatal(http.ListenAndServe(":8080", nil))
{
  "data": {
    "animals": [
      {
        "name": "Buddy",
        "__typename": "Animal"
      },
      {
        "name": "Whiskers",
        "__typename": "Animal"
      }
    ]
  }
}

This bug leads to

{
  "errors": [
    {
      "message": "panic occurred: {{\"Dog\" {'\\x05' '\\f'}}} does not implement \"Animal\""
    }
  ]
}
@magicoder10 magicoder10 changed the title [Bug] GraphQL will returns interface type for __typename when UseFieldResolvers is enabled [Bug] GraphQL returns interface type for __typename when UseFieldResolvers is enabled Jan 21, 2024
@magicoder10 magicoder10 changed the title [Bug] GraphQL returns interface type for __typename when UseFieldResolvers is enabled [Bug] GraphQL server returns interface type for __typename when UseFieldResolvers is enabled Jan 21, 2024
@pavelnikolov
Copy link
Member

@magicoder10 thank you for reporting this. Can I, please, have the code for the resolver as well?

@pavelnikolov
Copy link
Member

Initially this library was developed without field resolvers. There had to be a corresponding method for each GraphQL field. Therefore it made sense to represent GraphQL interfaces by corresponding Go interfaces which have a method for each of the GraphQL interface fields. If we choose to use field resolvers in our schema we would need to still use methods for the GraphQL interface fields. Here is a working example:


type Cat struct {
	name      string
	LivesLeft int32
}

func (c *Cat) Name() string {
	return c.name
}

type Dog struct {
	name  string
	Breed string
}

func (c *Dog) Name() string {
	return c.name
}

type Animal interface {
	Name() string
}

type AnimalsQuery struct {
	animals []Animal
}

func (q *AnimalsQuery) Animals() []*animalResolver {
	return toAnimalResolvers(q.animals)
}

type animalResolver struct {
	Animal
}

func (r *animalResolver) ToDog() (*Dog, bool) {
	d, ok := r.Animal.(*Dog)
	return d, ok
}

func (r *animalResolver) ToCat() (*Cat, bool) {
	c, ok := r.Animal.(*Cat)
	return c, ok
}

func toAnimalResolvers(animals []Animal) []*animalResolver {
	resolvers := make([]*animalResolver, len(animals))
	for i, animal := range animals {
		resolvers[i] = &animalResolver{Animal: animal}
	}
	return resolvers
}

func Test_issue630(t *testing.T) {

	schema := `
	interface Animal {
		name: String!
	}

	type Dog implements Animal {
		name: String!
		breed: String!
	}

	type Cat implements Animal {
		name: String!
		livesLeft: Int!
	}

	type Query {
		animals: [Animal!]!
	}
	`

	animals := []Animal{
		&Dog{name: "Fido", Breed: "Beagle"},
		&Cat{name: "Whiskers", LivesLeft: 9},
	}
	gqltesting.RunTests(t, []*gqltesting.Test{
		{
			Schema: graphql.MustParseSchema(schema, &AnimalsQuery{animals: animals}, graphql.UseFieldResolvers()),
			Query: `
				query {
					animals {
						name
						__typename
					}
				}
			`,
			ExpectedResult: `
				{"animals":[{"__typename":"Dog","name":"Fido"},{"__typename":"Cat","name":"Whiskers"}]}
			`,
		},
	})
}

I understand that this might be confusing and maybe this needs to be either better documented or the library needs to be changed in a way to allow field resolution of GraphQL interface fields 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants