Socket
Socket
Sign inDemoInstall

graphql-cost-calculator

Package Overview
Dependencies
9
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    graphql-cost-calculator

cost calculator for GraphQL query


Version published
Maintainers
1
Created

Readme

Source

GraphQL Query Cost Calculator

This library provides query cost calculation for implemting rate limiting like Github GraphQL API.

NOTE: For now it only works for schemas that follow GraphQL Cursor Connections Specification.

Installation

Install the package via 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: Estimated max node count.
  • Cost: Calculated cost. (see Github document to know the concept.)

Arguments

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

typeCostMap

You can set an additional object type weight for some objects. When your query includes some mached object types, the cost calculator adds weight for them.

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 }

Keywords

FAQs

Last updated on 11 Sep 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc