Giter VIP home page Giter VIP logo

apollo-federation-mutation-demo's Introduction

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.

apollo-federation-mutation-demo's People

Contributors

filwaline avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

apollo-federation-mutation-demo's Issues

Unable to resolve reference

Hey there! I would like to test your approach for mutations but with example mutation from readme

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

I got an error

{
  "data": {},
  "errors": [
    {
      "message": "Unable to resolve reference for <class 'reviews.graphql.ReviewMutation'>",
      "locations": [
        {
          "line": 1,
          "column": 75
        }
      ],
      "path": [
        "review"
      ]
    }
  ],
  "extensions": {
    "valueCompletion": [
      {
        "message": "Cannot return null for non-nullable field ReviewMutation.comment",
        "path": [
          "review"
        ]
      },
      {
        "message": "Cannot return null for non-nullable field ReviewMutation!.review",
        "path": [
          "review"
        ]
      }
    ]
  }
}

Unfortunately, I'm not familiar with python and strawberry lib and I can't get what is the problem.

Thank you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.