Skip to content

joe-re/graphql-cost-calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQL Query Cost Calculator

This library offers a means to calculate the cost of a query, facilitating rate limiting similar to the Github GraphQL API.

NOTE: Currently, it's only compatible with schemas adhering to GraphQL Cursor Connections Specification.

Installation

You can install the package using either npm or yarn.

npm install graphql-cost-calculator
yarn add graphql-cost-calculator

Usage

import { calculateCost } from "graphql-cost-calculator";
import schema from "./schema"; // Import or build your schema

const result = calculateCost({
  schema,
  query: `
    query {
      viewer {
        login
        repositories(first: 100) {
          edges {
            node {
              id
              issues(first: 50) {
                edges {
                  node {
                    id
                    labels(first: 60) {
                      edges {
                        node {
                          id
                          name
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  `
})

console.log(result) // { maxNode: 305101, cost: 51 }

Result

  • MaxNode: This represents the estimated maximum node count.
  • Cost: The computed cost. For more on this concept, refer to the Github document.

Arguments

{
  schema: GraphQLSchema;
  query: string;
  variables?: Record<string, any>;
  typeCostMap?: Record<string, number>;
}

typeCostMap

This lets you assign an additional weight to specific object types. If your query contains these specified object types, the cost calculator will append their respective weights.

const result = calculateCost({
  schema,
  query: `
    query {
      viewer {
        login
        repositories(first: 100) {
          edges {
            node {
              id
              issues(first: 50) {
                edges {
                  node {
                    id
                    labels(first: 60) {
                      edges {
                        node {
                          id
                          name
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  `,
  typeCostMap: { RepositoryConnection: 10 }
})

// (1 + 100 + 5000 + 1000(RepositoryConnection cost) / 100 = 61
console.log(result) // { maxNode: 305101, cost: 61 }

About

cost calculator for GraphQL query

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published