Skip to content

Latest commit

 

History

History
69 lines (49 loc) · 1.5 KB

README.md

File metadata and controls

69 lines (49 loc) · 1.5 KB

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;