Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/andimarek/graphql-analyzer

Static analysis of GraphQL queries
https://github.com/andimarek/graphql-analyzer

graphql

Last synced: 20 days ago
JSON representation

Static analysis of GraphQL queries

Awesome Lists containing this project

README

        

# 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
](https://www.graphql.de/blog/static-query-analysis/)

# Usage

Add to your project:

```sh
npm install graphql-analyzer
```

`graphql-analyzer` exports three functions:

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

`analyseQuery` returns a the root `FieldVertex` of the dependency graph.

Details:
```typescript
export interface FieldVertex {
id: string;
fields: Array;
objectType: GraphQLObjectType;
fieldDefinition: GraphQLField;
dependsOn: Array;
dependOnMe: Array;
}
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:

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

`traverseFieldVertices` lets you traverse the graph returned by `analyzeQuery`:

```typescript
export function traverseFieldVertices(
root: FieldVertex,
visitor: (vertex: FieldVertex) => void)
: void;
```