A simple prototype using Apollo GraphQL Federation to combine two small elixir Graphql services
We have 3 services. Start the elixir services first, then run the federator.
product_api
cd product_api && mix deps.get && iex -S mix
- runs on port
3010
by default (seeconfig/config.exs
) - example query:
query test {
product: product(id: 1) {
name
brand
}
user_api
cd user_api && mix deps.get && iex -S mix
- runs on port
3020
by default (seeconfig/config.exs
) - example query:
query test {
user: fetchUserById(id: 3) {
email
}
}
federator
cd federator && npm install && npm start server
- runs on port
4000
by default
Send this query to the federator:
query test {
# from the user service
user: fetchUserById(id: 2) {
email
}
# from the product service
product: product(id: 2) {
name
brand
}
# from both, using federation
user_with_products: fetchUserById(id: 1) {
products {
name
brand
priceUSD
}
email
}
}
You should see this response
{
"data": {
"user": {
"email": "[email protected]"
},
"product": {
"name": "Blue Hat",
"brand": "Balenciaga"
},
"user_with_products": {
"products": [
{
"name": "Red Belt",
"brand": "Gucci",
"priceUSD": 1000
},
{
"name": "Blue Hat",
"brand": "Balenciaga",
"priceUSD": 2000
},
{
"name": "Green Scarf",
"brand": "Vetements",
"priceUSD": 3000
}
],
"email": "[email protected]"
}
}
}
Released under the MIT License.