Skip to content
Static analysis of GraphQL queries
TypeScript JavaScript
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
LICENSE
README.md
jest.config.js
package-lock.json
package.json
tsconfig.json

README.md

GraphQL analyzer

Static analysis of GraphQL queries (analysis without actually executing the query).

Please see this blog post for background and details: Static analysis of GraphQL queries

Usage

Add to your project:

npm install graphql-analyzer

graphql-analyzer exports three functions:

import {analyzeQuery, printDependencyGraph, traverseFieldVertices } from 'graphql-analyzer';

analyseQuery returns a the root FieldVertex of the dependency graph.

Details:

export interface FieldVertex {
    id: string;
    fields: Array<FieldNode>;
    objectType: GraphQLObjectType;
    fieldDefinition: GraphQLField<any, any>;
    dependsOn: Array<FieldVertex>;
    dependOnMe: Array<FieldVertex>;
}
export function analyzeQuery(
    document: DocumentNode, 
    schema: GraphQLSchema, 
    rawVariableValues?: { [key: string]: any; }, 
    validateQuery?: boolean)
    : FieldVertex;

printDependencyGraph returns all vertices and all edges for a dependency graph:

export interface DependencyEdge {
    from: FieldVertex;
    to: FieldVertex;
    conditional: boolean;
}
export function printDependencyGraph(
    root: FieldVertex)
    : [Array<FieldVertex>, Array<DependencyEdge>];

traverseFieldVertices lets you traverse the graph returned by analyzeQuery:

export function traverseFieldVertices(
    root: FieldVertex, 
    visitor: (vertex: FieldVertex) => void)
    : void;
You can’t perform that action at this time.