Are subgraph still autonomous?

Hello, I have a question regarding federation2 and subgraphs. Suppose I have 2 teams in my company and each of them manage its own GraphQL API :

  • The Product Team
  • The Inventory Team

Those 2 GraphQL API are reachable on their own URL :
http://product.api/graphql
http://inventory.api/graphql

That’s fine.

But one day, the company wants to create a supergraph to merge data between these 2 teams.

So, the Inventory team modifies its schema to “extend” the Product entity by adding the inStock data in it :

image

My questions are :

  1. Is it recommended to continue to call the subgraphs directly without going through the gateway?

  2. When I directly call the subgraph htp://inventory.api/graphql, is there a way to prohibit calling the Product type in a query? Because this Type is an extension that does not exist in the Inventory perimeter without the Product subgraph.

Thank you

Hello :wave:

  1. Is it recommended to continue to call the subgraphs directly without going through the gateway?

Whole point of federation is to expose single API endpoint through Gateway/Router so your apps/clients only have to integrate with one endpoint vs X separate endpoints. With Federation you should only call the gateway/router and not the underlying subgraphs.

  1. When I directly call the subgraph htp://inventory.api/graphql, is there a way to prohibit calling the Product type in a query? Because this Type is an extension that does not exist in the Inventory perimeter without the Product subgraph.

Each subgraph is a valid GraphQL server so by default* you will be able query whatever is exposed on your subgraph, e.g. if your inventory subgraph exposes Product as part of its query then you will be able to query it as-is

type Query {
  inventory: [Inventory!]!
}

type Inventory {
  // some fields here
  products: [Product!]! // this points to your product stub
}

In the example above, if you query your inventory subgraph directly you will only be able to get the product stub defined in your inventory subgraph. On the other hand if you send your queries through the router, it will be able to access ALL product fields that are defined across all subgraphs.

If you are just extending the (Product) entity and not exposing it as part of your queries, then it will be only accessible through Federated _entities query as that is the mechanism used by the router to get additional data for entities. While you can generate this query manually, in general it should be auto generated by the router based on the incoming requests.

*you probably could create some custom logic to prevent this behavior but I am unsure what would be the point

1 Like