Skip to content

andimarek/graphql-analyzer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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;