Skip to content
Babel plugin that compile GraphQL tagged template strings
JavaScript
Latest commit 5d4b150 May 26, 2015 @ooflorent 2.0.2
Failed to load latest commit information.
src Fix plugin loading May 26, 2015
test Fix plugin loading May 26, 2015
.babelrc First commit Apr 23, 2015
.editorconfig Update parser May 21, 2015
.eslintrc First commit Apr 23, 2015
.gitignore First commit Apr 23, 2015
CHANGELOG.md Update parser May 21, 2015
LICENSE First commit Apr 23, 2015
README.md Fix transformer name May 26, 2015
package.json 2.0.2 May 26, 2015

README.md

babel-plugin-graphql

Babel plugin that compile GraphQL tagged template strings.

Issues related to GraphQL parsing should be reporter on graphql-parser issue-tracker.

Install

npm install --save-dev babel-plugin-graphql

Usage

Run:

babel --plugins graphql script.js

Or add the plugin to your .babelrc configuration:

{
  "plugins": [ "graphql" ]
}

Note: Due to current API limitations you need to enable es7.objectRestSpread transformer or stage 1 transformers.

Example

The plugin will compile the following code:

const IMAGE_WIDTH = 80
const IMAGE_HEIGHT = 80

const PostFragment = graphql`
  {
    post {
      title,
      published_at
    }
  }
`

const UserQuery = graphql`
  {
    user(id: <id>) {
      nickname,
      avatar(width: ${IMAGE_WIDTH}, height: ${IMAGE_HEIGHT}) {
        url
      },
      posts(first: <count>) {
        count,
        edges {
          node {
            ${ PostFragment() }
          }
        }
      }
    }
  }
`

into:

var IMAGE_WIDTH = 80;
var IMAGE_HEIGHT = 80;

var PostFragment = function PostFragment(params) {
  return {
    fields: {
      post: {
        fields: {
          title: {},
          published_at: {}
        }
      }
    }
  };
};

var UserQuery = function UserQuery(params) {
  return {
    fields: {
      user: {
        params: {
          id: params.id
        },
        fields: {
          nickname: {},
          avatar: {
            params: {
              width: IMAGE_WIDTH,
              height: IMAGE_HEIGHT
            },
            fields: {
              url: {}
            }
          },
          posts: {
            params: {
              first: params.count
            },
            fields: {
              count: {},
              edges: {
                fields: {
                  node: {
                    fields: _extends({}, PostFragment().fields)
                  }
                }
              }
            }
          }
        }
      }
    }
  };
};
Something went wrong with that request. Please try again.