You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@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.
typeEntryActorstruct {
Typenamestring`graphql:"__typename"`Bot`graphql:"...on Bot" json:",omitempty"`Organization`graphql:"... on Organization" json:",omitempty"`User`graphql:"... on User" json:",omitempty"`
}
The problem comes when each of this elements have fields that are common (like login, createdAt or updatedAt), 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).
The Go visibility rules for struct fields are amended for JSON when deciding which field to marshal or unmarshal. If there are multiple fields at the same level, and that level is the least nested (and would therefore be the nesting level selected by the usual Go rules), the following extra rules apply:
Of those fields, if any are JSON-tagged, only tagged fields are considered, even if there are multiple untagged fields that would otherwise conflict.
If there is exactly one field (tagged or not according to the first rule), that is selected.
Otherwise there are multiple fields, and all are ignored; no error occurs.
I also thought of creating a custom unmarshal function, but it seems not to work due to the way jsonutil.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.
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:
vareaEntryActor// make a GraphQL query that populates ea ...switchea.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:
The text was updated successfully, but these errors were encountered: