MCPcopy Index your code
hub / github.com/SamHatoum/federation-testing-tool

github.com/SamHatoum/federation-testing-tool @v3.0.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release v3.0.2 ↗ · + Follow
23 symbols 46 edges 8 files 0 documented · 0% updated 3y agov3.0.2 · 2020-06-11★ 623 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

federation-testing-tool

Test your Apollo GraphQL Gateway / Federation micro services. With this package you don't have to worry about the whole complexity that comes with joining the GraphQL federated micro services and preparing them for testing.

Install it with

npm install --save-dev federation-testing-tool

Example Usage, for the Federation Demo From Apollo.

data flow

Test services in isolation:

const typeDefs = gql`
  extend type Product @key(fields: "upc") {
    upc: String! @external
    weight: Int @external
    price: Int @external
    inStock: Boolean
    shippingEstimate: Int @requires(fields: "price weight")
  }
`;

let inventory = [
  { upc: "1", inStock: true },
  { upc: "2", inStock: false },
  { upc: "3", inStock: true }
];

const resolvers = {
  Product: {
    __resolveReference(object) {
      return {
        ...object,
        ...inventory.find(product => product.upc === object.upc)
      };
    },
    shippingEstimate: object => {
      if (object.price > 1000) return 0;
      return object.weight * 0.5;
    }
  }
};

const service = {
  typeDefs,
  resolvers
};

describe("Based on the data from the external service", () => {
  const query = gql`
    {
      _getProduct {
        inStock
        shippingEstimate
      }
    }
  `;

  it("should set the shippingEstimate at 0 for an expensive item and retrieve inStock", async () => {
    const mocks = {
      Product: () => ({
        upc: "1",
        weight: 10,
        price: 14000,
      })
    };

    const result = await executeGraphql({ query, mocks, service });

    expect(result.data._getProduct.shippingEstimate).toEqual(0);
    expect(result.data._getProduct).toEqual({
      inStock: true,
      shippingEstimate: 0
    });
  });

  it("should calculate the shipping estimate for cheap item", async () => {
    const mocks = {
      Product: () => ({
        upc: "1",
        weight: 10,
        price: 10,
      })
    };

    const result = await executeGraphql({ query, mocks, service });
    expect(result.data._getProduct.shippingEstimate).toEqual(5);
  });
});

Test services together:

const { executeGraphql } = require("federation-testing-tool");
const { gql } = require("apollo-server");

const { typeDefs } = require("./schema");
const { resolvers } = require("./resolvers");

const { typeDefs: typeDefsProducts } = require("../products/schema");

const services = [
  { inventory: { typeDefs, resolvers } },
  {
    products: {
      typeDefs: typeDefsProducts
    }
  }
];

describe("Based on the data from the external service", () => {
  const query = gql`
    {
      topProducts {
        name
        inStock
        shippingEstimate
      }
    }
  `;

  it("should calculate the shipping estimate", async () => {
    const mocks = {
      Product: () => ({
        upc: "1",
        name: "Table",
        weight: 10,
        price: 10,
        elo: "",
        __typename: "Product"
      })
    };

    const result = await executeGraphql({ query, mocks, services });
    expect(result.data.topProducts[0]).toEqual({
      name: "Table",
      inStock: true,
      shippingEstimate: 5
    });
  });
  it("should set the shippingEstimate at 0 for an expensive item", async () => {
    const mocks = {
      Product: () => ({
        upc: "1",
        name: "Table",
        weight: 10,
        price: 14000,
        elo: "",
        __typename: "Product"
      })
    };

    const result = await executeGraphql({ query, mocks, services });
    expect(result.data.topProducts[0]).toEqual({
      name: "Table",
      inStock: true,
      shippingEstimate: 0
    });
  });
});

Core symbols most depended-on inside this repo

executeGraphql
called by 13
index.js
sendOperation
called by 2
helpers/executeQueryPlan.js
dateToProtoTimestamp
called by 2
helpers/executeQueryPlan.js
buildLocalService
called by 1
index.js
isEmpty
called by 1
index.js
buildRequestContext
called by 1
index.js
prepareProviderService
called by 1
index.js
setupSchema
called by 1
index.js

Shape

Function 23

Languages

TypeScript100%

Modules by API surface

index.js11 symbols
helpers/executeQueryPlan.js9 symbols
tests/singleService.test.js1 symbols
tests/multipleServices.test.js1 symbols
tests/differentContexts.test.js1 symbols

For agents

$ claude mcp add federation-testing-tool \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page