imranolas / graphql-ast-types

Functional helpers for working with a GraphQL AST

Home Page:https://www.npmjs.com/package/graphql-ast-types

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

graphql-ast-types

Autogenerated helper functions for generating a GraphQL AST

This project generates helpers functions from the graphql/language AST flow descriptions. It is intended to help with building a valid GraphQL AST.

Getting Started

yarn add graphql-ast-types

import * as t from 'graphql-ast-types';

The implementation here mimics that of babel-types. Thanks Babel team.

Usage

The following is an example of how to build a simple query with AST types.

import * as t from 'graphql-ast-types';
import { print } from 'graphql/language';

const ast = t.document([
  t.operationDefinition(
    'query',
    t.selectionSet([
      t.field(t.name('foo')),
      t.field(t.name('bar'))
    ])
  )
]);

print(ast);

/*
query {
  foo
  bar
}
*/

In addition, method calls are validated for correctness and accompanied by is and assert helpers.

t.isName(t.name('Hello')); // true

t.isName({ kind: 'Name' }); // true
t.assert({ kind: 'Name' }); // no error

t.isName({ kind: 'IntValue' }); // false
t.assert({ kind: 'IntValue' }); // error

The full API can be found here.

About

Functional helpers for working with a GraphQL AST

https://www.npmjs.com/package/graphql-ast-types


Languages

Language:JavaScript 100.0%