Merge an interface from one subgraph with a type from another subgraph using GQL Stitching

Merge an interface from one subgraph with a type from another subgraph using GQL Stitching


0

I have 2 services in my system, which both have their own subgraphs.

Product Service, which is "smart" i.e. knows about different product types and Suggestions Service which is rather "dumb" i.e. only knows that there are some products, but nothing about specific types. So the 2 subgraphs look something like the following:

Product Service

interface Product {
   id: String!
   name: String!
}

type FoodProduct implements Product {
  id: String!
  name: String!
  expiryDate: String!
}

type ToolProduct implements Product {
  id: String!
  name: String!
  durabilityRating: Float!
}

type Query {
  products: [Product]
  productById(id: String!): Product
}

Suggestions Service

type Product {
   id: String!
   similarProducts: [Product]
}

type Query {
  similarProducts(productId: String!): [Product]
}

I need to keep the Suggestions Service "dumb" also in the future i.e. when new Product types are added in the Product Service, I do not have to change anything on the Suggestions Service.

Now when I query the gateway / supergraph, I would need to be able to run a query like this:

query Products {
  products {
    id
    name
    ... on FoodProduct {
      expiryDate
      similarProducts {
        id
        name
        ... on FoodProduct {
          expiryDate
        }
      }
    }
    ... on ToolProduct {
      durabilityRating
      similarProducts {
        id
        name
        ... on ToolProduct {
          durabilityRating
        }
      }
    }
  }
}

Does anyone have ideas how I could achieve this using Stitching?

I have used Apollo Federation with the @interfaceObject directive (https://www.apollographql.com/docs/federation/federated-types/interfaces/) which works very well in that situation actually, but unfortunately because of some other reasons, we need to move away from using Apollo Federation.

I also have experimented with GQL Fusion (https://chillicream.com/blog/2023/08/15/graphql-fusion), but it seem that they are not yet at this point, where such use case can be implemented.

Now looking into Stitching options (https://the-guild.dev/graphql/stitching/docs), but have not found any example that would support my use case… maybe I am just not seeing it right now, thus asking for some help / ideas


Load 3 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *