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

Issue Using Fragments on Union Types #399

Closed
bannzai opened this issue Oct 27, 2018 · 3 comments
Closed

Issue Using Fragments on Union Types #399

bannzai opened this issue Oct 27, 2018 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@bannzai
Copy link

bannzai commented Oct 27, 2018

Expected Behaviour

Response JSON

{
  "data": {
    "Q": {
      "__typename": "A",
      "a": "1"
    }
  }
}

Actual Behavior

Actual Response JSON

{
  "data": {
    "Q": {
      "__typename": "A"
    }
  }
}

Minimal graphql.schema and models to reproduce

schema

type Query {
  Q(): U!
}

type A {
 a: String!
}
type B {
 b: Boolean!
}

union U = A | B

Query

query {
  Q {
    __typename
    ...F
  }
}

fragment F on U {
  __typename
   ... on A {
    a
  }
  ... on B {
    b
  }
}

Resolver

func (r *queryResolver) Q(ctx context.Context) (model.U, error) {
	return model.A{A: "1"}, nil
}

Description

I want to get for Expected Behaviour json when client request query.
But I can't receive Expected Behaviour json. So, It is not exists "a": "1" json field.
Please tell me if there is a solution on this.

@bannzai bannzai changed the title [Question] Could not find json struct. [Question] Could not find json field. Oct 27, 2018
@mathewbyrne mathewbyrne added the bug Something isn't working label Nov 2, 2018
@mbranch
Copy link
Contributor

mbranch commented Dec 6, 2018

I ran into this as well, and I think the root issue has to do with fragment usage. The above query, with the fragment removed, does seem to work:

query {
  Q {
    __typename
    ... on A {
	  a
	}
    ... on B {
	  b
	}
  }
}

@mbranch
Copy link
Contributor

mbranch commented Dec 6, 2018

I've found the issue can be resolved (though incorrectly) by omitting the following lines.

gqlgen/graphql/exec.go

Lines 62 to 64 in 5c870a4

if !instanceOf(fragment.TypeCondition, satisfies) {
continue
}

It seems to me that a fragment on a union isn't truly supported.

(edit: a fragment on a union appears unsupported, but if the fragment is on a common interface over the union type, this will work as expected)

This is a very superficial look into this that may help someone with more knowledge understand the issue quickly. When I have some time, I may look into it further as we really like this tool so far, but this bug is a showstopper unfortunately.

@mbranch
Copy link
Contributor

mbranch commented Dec 10, 2018

Following up on this, I see a related remark in pull request #335:

RFC probably doesn't handle the case where the interface is not generated, but the models are, what should happen there?

  • This case is actually kind of insane, so upon further thought let's not handle it.

@lwc Would you mind weighing-in on this issue, your comment seems to suggest there's something fundamentally wrong with returning a list of heterogeneous types without an common interface. I don't see why, but maybe I'm missing something.

@mathewbyrne mathewbyrne changed the title [Question] Could not find json field. Issue Using Fragments on Union Types Jan 23, 2019
@mathewbyrne mathewbyrne self-assigned this Feb 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants