Skip to content

Dangerous issue about Go anonymous composition #553

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

Open
alimoli opened this issue Jul 16, 2020 · 1 comment
Open

Dangerous issue about Go anonymous composition #553

alimoli opened this issue Jul 16, 2020 · 1 comment

Comments

@alimoli
Copy link

alimoli commented Jul 16, 2020

I discovered this bug trying to resolve this problem #183 (comment) .

Essentially, this library does not allow to have anonymous fields for compositions since those fields are not evaluated.

Example

type Car struct {
       Speed int `json:"speed"`
       Product
}

type Product struct {
       Price int `json:"price"`
       Description string `json:"description"`
}

GraphQL schema

carType := graphql.NewObject(graphql.ObjectConfig{
	Name: "Car",
	Fields: graphql.Fields{
		"speed": &graphql.Field{
			Type:        graphql.NewNonNull(graphql.Int),
		},
		"price": &graphql.Field{
			Type:        graphql.NewNonNull(graphql.Int),
		},
		"description": &graphql.Field{
			Type:        graphql.NewNonNull(graphql.String),
		},
	},
})

Result

The price and description fields has a null value for GraphQL library and this is not allowed since we declare a graphql.NewNonNull field. Indeed, we get the following error:

  • Cannot return null for non-nullable field price.
  • Cannot return null for non-nullable field description.

Temporary solution

The only way to fix this problem at the moment is to duplicate code:

type Car struct {
       Speed int `json:"speed"`
       Price int `json:"price"`
       Description string `json:"description"`
}

type Product struct {
       Price int `json:"price"`
       Description string `json:"description"`
}

Or changing a lot of things both internal and external (also the public fields to API consumers).

type Car struct {
       Speed int `json:"speed"`
       Product Product `json:"product"`
}

type Product struct {
       Price int `json:"price"`
       Description string `json:"description"`
}

Instead of:

{
   speed
   price
   description
}

We have to provide this:

{
   speed
   product {
      price
      description
   }
}

Both solutions are really limited and go against clean code.

@Nemesisesq
Copy link

I found this as well the issue. In DefaultResolveFn the struct that's reflected isn't "flattened" so it' never accesses the tag on the struct field.

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