Skip to content

andimarek/graphql-analyzer

master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 

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;

About

Static analysis of GraphQL queries

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published