Skip to content

Commit

Permalink
docs(README): create README
Browse files Browse the repository at this point in the history
  • Loading branch information
evenchange4 committed Jan 19, 2018
1 parent 7b474f2 commit cbaa5aa
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 12 deletions.
123 changes: 123 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# graphql.macro

> Compile GraphQL AST at build-time with babel-plugin-macros.
[![Travis][build-badge]][build]
[![Codecov Status][codecov-badge]][codecov]
[![npm package][npm-badge]][npm]
[![npm downloads][npm-downloads]][npm]
[![node][node]]()

[![Dependency Status][dependency-badge]][dependency]
[![devDependency Status][devdependency-badge]][devdependency]
[![peerDependency Status][peerdependency-badge]][peerdependency]

[![prettier][prettier-badge]][prettier]
[![license][license-badge]][license]

## Installation

```sh
$ yarn add graphql.macro
```

> Note: You'll also need to install and configure [babel-plugin-macros](https://github.com/kentcdodds/babel-plugin-macros) if you haven't already.
## Usage

### `gql`

```diff
-import gql from 'graphql-tag';
+import { gql } from 'graphql.macro';

const query = gql`
query User {
user(id: 5) {
lastName
...UserEntry1
}
}
`;

↓ ↓ ↓ ↓ ↓ ↓

const query = {
"kind": "Document",
"definitions": [{
...
```

### `loader`

```js
import { loader } from 'graphql.macro';
const query = loader('./fixtures/query.graphql');

↓ ↓ ↓ ↓ ↓ ↓

const query = {
"kind": "Document",
"definitions": [{
...
```
## Alternative
* [Webpack preprocessing with graphql/loader](https://github.com/apollographql/graphql#webpack-preprocessing-with-graphqlloader)
* [Babel preprocessing](https://github.com/apollographql/graphql#babel-preprocessing)
* https://github.com/leoasis/graphql.macro
## Development
### Requirements
* node >= 9.4.0
* yarn >= 1.3.2
```sh
$ yarn install --pure-lockfile
```
## Test
```sh
$ yarn run format
$ yarn run eslint
$ yarn run flow
$ yarn run test:watch
$ yarn run build
```
---
## CONTRIBUTING
* ⇄ Pull requests and ★ Stars are always welcome.
* For bugs and feature requests, please create an issue.
* Pull requests must be accompanied by passing automated tests.
## [CHANGELOG](CHANGELOG.md)
## [LICENSE](LICENSE)
MIT: [http://michaelhsu.mit-license.org](http://michaelhsu.mit-license.org)
[build-badge]: https://img.shields.io/travis/evenchange4/graphql.macro/master.svg?style=flat-square
[build]: https://travis-ci.org/evenchange4/graphql.macro
[npm-badge]: https://img.shields.io/npm/v/graphql.macro.svg?style=flat-square
[npm]: https://www.npmjs.org/package/graphql.macro
[codecov-badge]: https://img.shields.io/codecov/c/github/evenchange4/graphql.macro.svg?style=flat-square
[codecov]: https://codecov.io/github/evenchange4/graphql.macro?branch=master
[node]: https://img.shields.io/node/v/graphql.macro.svg?style=flat-square
[npm-downloads]: https://img.shields.io/npm/dt/graphql.macro.svg?style=flat-square
[license-badge]: https://img.shields.io/npm/l/graphql.macro.svg?style=flat-square
[license]: http://michaelhsu.mit-license.org/
[dependency-badge]: https://david-dm.org/evenchange4/graphql.macro.svg?style=flat-square
[dependency]: https://david-dm.org/evenchange4/graphql.macro
[devdependency-badge]: https://david-dm.org/evenchange4/graphql.macro/dev-status.svg?style=flat-square
[devdependency]: https://david-dm.org/evenchange4/graphql.macro#info=devDependencies
[peerdependency-badge]: https://david-dm.org/evenchange4/graphql.macro/peer-status.svg?style=flat-square
[peerdependency]: https://david-dm.org/evenchange4/graphql.macro#info=peerDependencies
[prettier-badge]: https://img.shields.io/badge/styled_with-prettier-ff69b4.svg?style=flat-square
[prettier]: https://github.com/prettier/prettier
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@michaelhsu/graphql.macro",
"name": "graphql.macro",
"version": "0.1.0",
"description": "Compile GraphQL AST at build-time with babel-plugin-macros.",
"author": "Michael Hsu <evenchange4@gmail.com>",
Expand All @@ -18,7 +18,7 @@
"flow": "flow",
"precommit": "lint-staged",
"changelog":
"github-changes -o evenchange4 -r graphql-tag.macro -b master -f ./CHANGELOG.md --order-semver --use-commit-body"
"github-changes -o evenchange4 -r graphql.macro -b master -f ./CHANGELOG.md --order-semver --use-commit-body"
},
"dependencies": {
"babel-literal-to-ast": "2.0.0",
Expand Down Expand Up @@ -110,12 +110,12 @@
"node": ">=9.0.0"
},
"bugs": {
"url": "https://github.com/evenchange4/graphql-tag.macro/issues",
"url": "https://github.com/evenchange4/graphql.macro/issues",
"email": "evenchange4@gmail.com"
},
"homepage": "https://github.com/evenchange4/graphql-tag.macro#readme",
"homepage": "https://github.com/evenchange4/graphql.macro#readme",
"repository": {
"type": "git",
"url": "https://github.com/evenchange4/graphql-tag.macro.git"
"url": "https://github.com/evenchange4/graphql.macro.git"
}
}
8 changes: 4 additions & 4 deletions src/__tests__/__snapshots__/macro.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`macros [gql] with fragment: [gql] with fragment 1`] = `
import { gql } from 'graphql-tag.macro';
import { gql } from 'graphql.macro';
const userFragment = gql\`
fragment UserEntry1 on User {
firstName
Expand Down Expand Up @@ -133,7 +133,7 @@ const query = {

exports[`macros [gql] without fragment: [gql] without fragment 1`] = `
import { gql } from 'graphql-tag.macro';
import { gql } from 'graphql.macro';
const query = gql\`
query User {
user(id: 5) {
Expand Down Expand Up @@ -216,7 +216,7 @@ const query = {

exports[`macros [loader] with fragment: [loader] with fragment 1`] = `
import { loader } from 'graphql-tag.macro';
import { loader } from 'graphql.macro';
const query = loader('./fixtures/query1.graphql');
↓ ↓ ↓ ↓ ↓ ↓
Expand Down Expand Up @@ -369,7 +369,7 @@ const query = {

exports[`macros [loader] without fragment: [loader] without fragment 1`] = `
import { loader } from 'graphql-tag.macro';
import { loader } from 'graphql.macro';
const query = loader('./fixtures/query.graphql');
↓ ↓ ↓ ↓ ↓ ↓
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/macro.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import plugin from 'babel-plugin-macros';

expect.addSnapshotSerializer({
print(val) {
return val.replace(/..\/macro/, 'graphql-tag.macro');
return val.replace(/..\/macro/, 'graphql.macro');
},
test(val) {
return typeof val === 'string';
Expand Down
4 changes: 2 additions & 2 deletions src/macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ function graphqlTagMacro({
}): void {
const { gql = [], loader = [] } = references;

// Case 1: import { gql } from 'graphql-tag.macro'.
// Case 1: import { gql } from 'graphql.macro'.
gql.forEach(referencePath => {
const compiled = compileWithFragment(referencePath, t);
referencePath.parentPath.replaceWith(compiled);
});

// Case 2: import { loader } from 'graphql-tag.macro'
// Case 2: import { loader } from 'graphql.macro'
loader.forEach(referencePath => {
referencePath.parentPath.node.arguments.forEach(({ value }) => {
const queryPath = path.join(filename, '..', value);
Expand Down

0 comments on commit cbaa5aa

Please sign in to comment.