question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Union query requests in client code with conditional fragments

See original GitHub issue

I am trying to run a query returning a union type (e.g. the union Vehicle = Car | Bike).

Is there a way to run this query with the current library? In a raw GraphQL call this would be achieved using conditional fragments like so:

query allVehicles {
    allVehicles {
        ...on Car {
            brand
        }
        ...on Bike {
            weight
        }
    }
}

I can’t seem to find a way to run the query allVehicles with the current library. Car and Bike both extend the class Vehicle, but Vehicle is a completely empty stub class.

I would expect to have a VehicleUnionResponseProjection with builder options like “.onCar(CarResponseProjection)” and ".onBike(BikeResponseProjection). Would this be possible? Or is there even currently a way to achieve this with a different apporach?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
kobylynskyicommented, Jul 4, 2020

The feature will be released next week. Stay tuned! 😃

2reactions
elebeaupcommented, Jul 1, 2020

@kobylynskyi It is already possible to define custom serializer with customAnnotationsMapping (but I may be wrong).

For instance, the configuration

<customAnnotationsMapping>
  <Vehicle>com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = com.test.jackson.VehiclesDeserializer.class</Vehicle>
</customAnnotationsMapping>

generates the following code :

public class Query implements java.io.Serializable {

    @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = com.test.jackson.VehicleDeserializer.class)
    private Vehicle allVehicles;

So, the only need is to add __typename to the projection of the union and generate the union class. For instance, like :

public class VehicleUnionResponseProjection extends GraphQLResponseProjection {

    public VehicleUnionResponseProjection() {
    }

    public VehicleUnionResponseProjection car(CarResponseProjection subProjection) {
        return car(null, subProjection);
    }

    public VehicleUnionResponseProjection car(String alias, CarResponseProjection subProjection) {
        fields.add(new GraphQLResponseField("...on Car").alias(alias).projection(subProjection));
        return this;
    }

    public VehicleUnionResponseProjection bike(BikeResponseProjection subProjection) {
        return bike(null, subProjection);
    }

    public VehicleUnionResponseProjection bike(String alias, BikeResponseProjection subProjection) {
        fields.add(new GraphQLResponseField("...on Bike").alias(alias).projection(subProjection));
        return this;
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Unions and interfaces - Apollo GraphQL Docs
This query uses inline fragments to fetch a Result 's title (if it's a Book ) or its name (if it's an Author...
Read more >
Nested Fragment on Union type field removes data ... - GitHub
When using nested fragments on a union type, the properties from the subfragments are removed from the response without warning or error, ...
Read more >
GraphQL query, use fragment depending on some condition ...
The task is to request only metrics that can be viewed by the user. React application is using graphQL file loader and it...
Read more >
How to query your schema with GraphQL fragments
A fragment is basically a reusable piece of query. In GraphQL, you often need to query for the same data fields in different...
Read more >
Use a union query to combine multiple queries into a single ...
If you've never created a union query before, you might find it useful to first study a working example in the Northwind Access...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found