Open droidpl opened 4 years ago
Seems this could be an error related with #10 as there it says this case should be supported
I'm not sure if I understand what exactly you're asking about, so let me try to clarify.
When there are fields with same names in multiple inline fragments, it's normal that all of them will be populated. That's the intended behavior as implemented in PR #15.
Your code can do a query that populates EntryActor
and then check the Typename
field to know where to look for the rest of the content:
var ea EntryActor
// make a GraphQL query that populates ea ...
switch ea.Typename {
case "Bot":
// use ea.Bot ...
case "Organization":
// use ea.Organization ...
case "User":
// use ea.User ...
}
Is there something that you're not able to do this way? Or is this issue about something else?
The problem for my use case is that looking at the structure, you get an instance in all of them (Bot
, Organization
and User
). I wanted to marshal the structure as json
to send it somewhere else, however I get a lot of elements that are duplicated because of the parsing, so I have to go against the full tree to remove all the duplicities. Wouldn't be a way to tweak the parser so that only the right ones are there based on the Typename
?
@dmitshur Thanks for the nice library on graphql and GitHub.
What is the problem?
I am trying to query for the audit log actor and that part of the API uses unions. The relevant part of the query is the actor one so I will omit the rest. Actors can be bots, organizations or users, so it uses 3 types of unions.
The problem comes when each of this elements have fields that are common (like
login
,createdAt
orupdatedAt
), as the parser don't know where to do the parsing and if a specific field is repeated in multiple embedded structs it gets ommited (see documentation below).I also thought of creating a custom
unmarshal
function, but it seems not to work due to the wayjsonutil.unmarshalGraphQL
behaves as it used the custom decoder. See the function below:Is there a way I could workaround this so I an tell the parser how to parse each of those by
__typename
? With the current implementation seems hard to do properly queries against the auditLog which makes an intensive use of unions.Code and query
The graphql query I am testing would look like:
And all the go structs:
Result: using named properties
If I use
Bot
,User
andOrganization
named the result duplicated the values on each of them:While this should be only parsing the
User
part as__typename
references, not each of the repeated elementsResult: with unnamed properties
If I use as
EntryActor
the properties unnamed:It creates a collision and given the rule in the documentation, the field gets ignored and the result looks like: