Skip to content

This repository is a demo of using Apollo Federation for handling mutation that access data from external services/subgraphs.

License

Notifications You must be signed in to change notification settings

filwaline/Apollo-Federation-Mutation-Demo

Repository files navigation

Apollo Federation Mutation Demo

This repository is a demonstration of using Apollo Federation with the Python library Strawberry-GraphQL for handling mutation that access data from external services/subgraphs.

type ReviewMutation @key(fields: "productId") {
  productId: String!
  product: Product @external
  currentUser: User @external
  comment(body: String!): Review! @requires(fields: "product{upc} currentUser{id}")
}

type Mutation {
  review(productId: String!): ReviewMutation!
}

Included in the project is an example mutation, comment, that demonstrates how to fetch data from external services (product.upc from the products subgraph and currentUser.id from the accounts subgraph) in order to ensure consistency of the review.

Note that this approach requires the external key (such as productId) to be provided earlier in the mutation rather than packed into a single input type.

An example mutation query is provided for reference.

mutation ExampleMutation($productId: String!, $body: String!) {
  review(productId: $productId){
    comment(body: $body) {
      ...
    }
  }
}

Setup

  1. Clone the repository
  2. Start the services: docker-compose up
  3. Access the sandbox at http://localhost:4000
  4. [Optional] View a full example at this share link

Note: If you make any changes to Strawberry's schema, please run bash ./apollo-router/supergraph_compose.sh to update the *.graphql files.


For reference, the implementation of the ReviewMutation can be found in the following files:

Please take a look at the above links for more information about how the code works.

About

This repository is a demo of using Apollo Federation for handling mutation that access data from external services/subgraphs.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published