spring-graphql icon indicating copy to clipboard operation
spring-graphql copied to clipboard

Are composite objects supported?

Open budjb opened this issue 3 years ago • 3 comments
trafficstars

I would like to create a object that is a composite from multiple data sources. For example, I'd like to create an object such as:

type Foo {
    sourceA1: String!
    sourceA2: String!
    sourceB1: String!
    sourceB2: String!
}

As the names suggest, different properties will be sourced from 2 different sources (sourceA and sourceB).

This object will be queried over multiple parent objects, and so I would like to use the data loader functionality to avoid the N+1 problem.

I can not find native support for this kind of functionality (but please let me know if I've missed anything!), however, I can write my own framework around querying the different data sources and aggregating their individual components into a final object that meets the schema requirement.

Another optimization is that I would only like to query a datasource if the query's selection set actually requested at least one property associated with that data source. For example, if properties sourceB1 and sourceB2 were requested, I only want to query sourceB since nothing from sourceA was requested.

The gap I've found is that I can not create a @BatchMapping method, nor manually register a data loader instance, in such a way that I can receive the DataFetchingFieldSelectionSet object as a @SchemaMapping method may. If I had the selection set available (for the level of the query currently being executed), I would have everything needed to inspect the request and accomplish my goals.

So I guess there are 2 questions here:

  1. Is there some kind of native support for what I'm trying to do?
  2. How can I retrieve the selection set in a data loader?

budjb avatar Jun 10 '22 17:06 budjb

It's not very clear what you're asking, given that the batch loading mechanism applies independently to each nested field. You're showing String as the type for each field. That's probably for simplification reasons, but it makes it hard to understand the use case. Could you sketch a more representative schema and show batch mapping methods?

rstoyanchev avatar Jun 30 '22 10:06 rstoyanchev

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-projects-issues avatar Jul 07 '22 10:07 spring-projects-issues

@rstoyanchev I think that's what I'm trying to solve for: the ability for a batch loader to apply to multiple fields of some object. Hopefully this simple graphic will illustrate.

image

budjb avatar Jul 08 '22 15:07 budjb

Apologies for the delayed response, but I'm still missing is a description of what you've actually tried and what the outcome was? For example, it's not clear why a @BatchMapping method doesn't work or how it fails specifically, or why you need to build your own framework.

Ideally, please use start.spring.io to create a small, isolated sample that demonstrates the issue.

rstoyanchev avatar Oct 18 '22 10:10 rstoyanchev

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-projects-issues avatar Oct 25 '22 10:10 spring-projects-issues

Hey @rstoyanchev, I'm unable to build an example project because the original question was whether it's even possible to accomplish the diagram above.

I want to essentially build an object from multiple data sources (for example, combine some data from a database call and some data from an API). This allows me to build a meaningful, composite object from differing sources of truth.

I'd like to be able to use batch-type functionality to do it. The more I've dug into this, I'm fairly certain this is not possible.

I think a workaround is that my batch mapper itself can handle calling both data sources in a concurrent fashion, merge both data sets into my resulting object, and return that from the batch mapper. This is essentially what I've done.

If there's some method I've overlooked that will help me accomplish this in a simpler fashion, please let me know. This issue can be closed if not. Thanks!

budjb avatar Oct 25 '22 21:10 budjb

What I understand is that you have multiple data sources, and that you want to use batch loading. What I don't understand is why this can't work? In such cases, a more detailed example can help to illustrate in code. Even if a sample can't to do what you want, it helps to illustrate what you're expecting, and what the actual outcome is.

I'm afraid for now I need to close this, but happy to re-open if you provide more details.

rstoyanchev avatar Oct 26 '22 16:10 rstoyanchev