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

Nested Batches

Open Segelente opened this issue 1 year ago • 8 comments
trafficstars

Hello there, I wanted to know if and how it is currently possible to use the BatchMapping with only one request for this situation: Basically currently when mapping on type extension and field data it requested data from my server twice. Is it possible in this situation to avoid a second call and call everything in one go?

type Query{
  query: ReturnObject
}
type ReturnObject{
  id: ID
  header: [Header]
}
interface Header{
  id: ID
  description: String
}
interface Extension{
  id: ID
  data: String
}
type ObjectA implements Header & Extension{
  id: ID
  description: String
  data: String
}
type ObjectB implements Header {
  id: ID
  description: String
  objectC : [ObjectC ]
}
type ObjectC implements Extension{
  id: ID
  data: String
}

Segelente avatar Jun 14 '24 12:06 Segelente

There were improvements for interface mappings in 1.3, but overall it's not clear what you mean with one vs two requests. Could you provide more information, what version you're trying this with, maybe a sample query and explain what the two requests are?

rstoyanchev avatar Jul 03 '24 16:07 rstoyanchev

Thank you for your reply. The situation is as follows: I am returning two types of objects, both of which implement the Header interface (Object A and Object B). Object B contains a list of Object C. Now Object A and Object C (the one in Object B) both share a field data from the shared Interface Extension which I address with @Batchmapping(typeName= Extension, field= data) in my code. When the header is addressed, the function handles both objects at the same time and the function is only active once. For the interface Extension, the function is called twice. Once for object A and once for object C. Is there a way to avoid this?

Segelente avatar Jul 09 '24 17:07 Segelente

Rather than explaining that in plain text, could you show a sample schema, a request and a controller method signature? I'm afraid I don't understand the current description. Code snippets would be clearer.

bclozel avatar Jul 09 '24 19:07 bclozel

This is my Schema:

type Query{
  query: ReturnObject
}
type ReturnObject{
  id: ID
  header: [Header]
}
interface Header{
  id: ID
  description: String
}
interface Extension{
  id: ID
  data: String
}
type ObjectA implements Header & Extension{
  id: ID
  description: String
  data: String
}
type ObjectB implements Header {
  id: ID
  description: String
  objectC : [ObjectC ]
}
type ObjectC implements Extension{
  id: ID
  data: String
}

This is my BatchMapping for the Extension

@Batchmapping(typeName= Extension, field= data)
fun getExtensionData(extensionObjects: List<Extension>, context: GraphQLContext): Map<Extension, Data>{
    return requestToAPI() // not relevant
}

A query would be like:

query {
  query {
    header {
      ... on ObjectA {
        data
      }
      ... on ObjectB {
        objectC {
          data
        }
      }
    }
  }
}

Hope this helps. It's a really confusing situation for me too, unfortunately.

Segelente avatar Jul 09 '24 20:07 Segelente

I am having a similar issue and don't know if I should open a new ticket. @Segelente, You mean, that aliasing and passing arguments to the graphql query doesn't work?

I figured, that I can get the BatchLoaderEnvironment, GraphQLContext, Principle. But I don't see the information that I found in the DataFetchingEnvironment in the past. Does it mean, that BatchMapping is only for very simple use cases (almost none in my case)?

I would expect something like @Argument("filter") Map<FilterClass, List<OriginClass>> being possible....

mheidt avatar Jul 10 '24 15:07 mheidt

@Segelente could you please use https://start.spring.io to create a starter project with the relevant versions, and then add minimal code that helps to demonstrate the issue. As I said in my previous comment, the behavior depends on the version you're using and a number of other things, so it's best if you iterate over something until you can demonstrate the issue. If that's easy then it shouldn't take long. If it is not, then you can try different things and compare to your code until you can demonstrate it.

rstoyanchev avatar Jul 11 '24 11:07 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 22 '24 09:07 spring-projects-issues

Hello everyone, Thanks for your patience. I really appreciate your help. I have created a project: https://github.com/Segelente/demo-nested-batches I hope this helps. Unfortunately, I am not responsible for the design choices and cannot just change the way the schema is set up. If you can't help, I fully understand. Have a nice day.

Segelente avatar Jul 24 '24 17:07 Segelente