CaminoDS
CaminoDS

Reputation: 51

How can I provide input field arguments with apollo federation 2?

I am trying to federate two of my micro services with apollo gql federation 2. I have successfully connected the two services through the federation with the following schemas:

Subgraph1 - Product

type Product @key(fields: "id") {
    id: ID!
    title: String!
    description: String
    price: Int!
    category: [Category!]
  }

type Category @key(fields: "id") {
    id: ID!
  }

type Query {
    product(id: ID!): Product
  }

Subgraph 2 - Category

type Category @key(fields: "id") {
    id: ID!
    title: String
  }

and the following query

query Product($productId: ID!) {
  product(id: $productId) {
    id
    title
    category {
      id
      title
    }
  }
}

gives a desired result

However, what if I wanted to add some filter on the returned categories for a given product. Lets say I only wanted to have the ones with title "sport", so the query would look like this instead:

query Product($productId: ID!) {
  product(id: $productId) {
    id
    title
    category(searchTerm: "sport") {
      id
      title
    }
  }
}

A normal way of doing the input argument would be simply just

type Product @key(fields: "id") {
    id: ID!
    title: String!
    description: String
    price: Int!
    category(searchTerm: String): [Category!]
  }

Is this achievable when federating the services? I am not sure how the input field is provided to the second subgraph?

I tried to add the input as a part of the type in the first subgraph, however it does not seem to pass the search term to the next graph.

Upvotes: 2

Views: 434

Answers (1)

Salinda Hettiarachchi
Salinda Hettiarachchi

Reputation: 11

Yes, you could define the GraphQL schema like that. And then you would have to specify 'searchTerm' as an input parameter to your data fetching method in the second micro service.

For example if you use GDS framework for the federation

import com.netflix.graphql.dgs.*;


@DgsData(parentType = "Product")
public List<Category> category(@InputArgument String searchTerm){
    // Filter categories based on 'searchTerm'
}

Upvotes: 1

Related Questions