Skip to content

Repository files navigation

GraphQL stitching with federated services using Stitching Directives

The service set-up of this demo is based on the Stitching directives SDL example from the Schema Stitching Handbook that is in turn based the official demonstration repository for Apollo Federation.

It demonstrates using the GraphQL Stitching Directives in Spring Boot 3 GraphQL services that are reachable through an API Gateway constructed using GraphQL Yoga.

Aside from demonstrating the Stitching Directives it also demonstrates:

The Spring GraphQL implementation doesn't support file uploads yet, see this PR). The GraphQL Spring Boot kickstarter project does support file uploads. Thats why the userservice doesn't use the Spring GraphQL implementation but the kickstarter project.

Setup

I did not put much effort in a clean project structure or build environment. Building the services from the command line is also possible if you have the correct tooling installed.

To build and launch the services and gateway, execute the following command(s):

docker-compose build # Build all projects
docker-compose up # Launch all containers

After that, the GraphiQL interface of all the services are reachable under the following urls:

GraphQL Queries to test

Please note: all services use an embedded database, the data is gone after a restart. All services provide some initial data on start-up

# Get all products from product, inventory and review info (this query fetches data from all of the services)
query products {
  products {
    id
    inStock
    price
    description
    shippingEstimate
    reviews {
      body
      author {
        name
        username
      }
    }
  }
}
# Subscribe to new reviews. This also fetches data from 2 services (review service and user service)
subscription newReviews {
    newReviews{
        id
        body
        author {
            name
        }
    }
}

# Use this mutation to create a new review after subscribing
mutation createReview {
    createReview(review: {
        body: "This is a test"
        product: 1
        author: 1
    }) {
        id
    }
}

File upload to user service

The following curl command can be used to test file uploads to the user service. Since Yoga does not have file upload support in the gateway (yet) it won't work through the gateway but only when directly executing it on the userservice.

Note: the user service currently doesn't do anything with the uploaded file

curl localhost:9004/graphql \
  -F operations='{ "query": "mutation ($user: ID!, $file: Upload!) { addAvatar(user: $user, file: $file) }", "variables": { "file": null, "user": "1" } }' \
  -F map='{ "0": ["variables.file"] }' \
  -F 0=@graphql.png

About

GraphQL stitching

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages