Coder Social home page Coder Social logo

apollo-demos's Introduction

Store

Dev Set up

# install subgraphs & gateway deps
cd subgraphs/products && npm i 
cd subgraphs/customer && npm i 
cd subgraphs/shipping && npm i
cd gateway && npm i 

# start the subgraphs
npm run start:dev

# ๐Ÿš€ Subgraph customer running at http://localhost:5010/
# ๐Ÿš€ Subgraph products running at http://localhost:5020/
# ๐Ÿš€ Subgraph shipping running at http://localhost:5030/

# exportAPOLLO_KEY=service:SECRET
# start the gateway
npm run start:gw
# ๐Ÿš€ Gateway ready at http://localhost:4000/

Desired State

Desired State

# shipping subgraph
query {
  shippingReference {
    id
    orderNumber
    shippingEstimate # query planner calls customer & products
  }
}

Sample schema definition, utilizing @requires.

Example of @provides

We can use @provides on an field resolved by another subgraph, to override their value. In this example, we have the field Product.description. While "description" can be resolved in the Product subgraph, the Shipping subgraph has its own value for it, and wants to return it for queries calling this service.

Observe the query

query InStockCount{
  inStockCount(shippingId: "1") {
    quantity
    product {
      id
      description
    }
  }
}
// response
{
  "data": {
    "inStockCount": {
      "quantity": 100,
      "product": {
        "id": "1",
        "description": "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum."
      }
    }
  }
}

This yields the query plan below. It instructs the gateway that for this query, it doesn't need to invoke the Product subgraph because the field is resolved here.

QueryPlan {
  Fetch(service: "shipping") {
    {
      inStockCount(shippingId: $shippingId) {
        quantity
        product {
          id
          description
        }
      }
    }
  },
}

Now the Shipping subgraph doesn't return the Product.name for which the gateway then needs to call the Product subgraph

query InStockCount {
  inStockCount(shippingId: "1") {
    quantity
    product {
      id
      name
      description
    }
  }
}
//response
{
  "data": {
    "inStockCount": {
      "quantity": 100,
      "product": {
        "id": "1",
        "name": "Pants",
        "description": "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum."
      }
    }
  }
}

Execution plan:

QueryPlan {
  Sequence {
    Fetch(service: "shipping") {
      {
        inStockCount(shippingId: $shippingId) {
          product {
            __typename
            id
            description
          }
          quantity
        }
      }
    },
    Flatten(path: "inStockCount.product") {
      Fetch(service: "products") {
        {
          ... on Product {
            __typename
            id
          }
        } =>
        {
          ... on Product {
            name
          }
        }
      },
    },
  },
}

OpenTelemetry

export APOLLO_OTEL_EXPORTER_TYPE=collector
export APOLLO_OTEL_EXPORTER_HOST=collector
export APOLLO_OTEL_EXPORTER_PORT=4318

Collector

npm install @opentelemetry/node
@opentelemetry/plugin-http
@opentelemetry/plugin-express

apollo-demos's People

Contributors

mpiantella avatar

Watchers

 avatar

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.