Skip to content

Commit ed8e699

Browse files
committed
WIP
1 parent aee4754 commit ed8e699

File tree

6 files changed

+89
-59
lines changed

6 files changed

+89
-59
lines changed

.babelrc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
{
22
"presets": [
3-
"latest",
4-
"stage-0"
3+
["env", {
4+
"targets": {
5+
"node": 6
6+
},
7+
"whitelist": [
8+
"transform-object-rest-spread"
9+
]
10+
}]
511
]
612
}

.eslintrc.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
module.exports = {
22
extends: 'airbnb-base',
33
parser: 'babel-eslint',
4-
plugins: [
5-
'babel',
6-
],
74
rules: {
8-
'babel/func-params-comma-dangle': ['error', 'always-multiline'],
5+
'arrow-parens': ['error', 'always'],
96
},
107
};

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: node_js
22
node_js:
3-
- "4"
4-
- "5"
3+
- "7"
54
- "6"
65
script:
76
- npm test -- --coverage && cat coverage/lcov.info | node_modules/coveralls/bin/coveralls.js

README.md

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,96 @@
11
# graphql-utilities
2+
⚒ Utilities for GraphQL
3+
24
[![npm version](https://badge.fury.io/js/graphql-utilities.svg)](https://badge.fury.io/js/graphql-utilities) [![Build Status](https://travis-ci.org/bloveit/graphql-utilities.svg?branch=master)](https://travis-ci.org/bloveit/graphql-utilities) [![Coverage Status](https://coveralls.io/repos/github/bloveit/graphql-utilities/badge.svg?branch=master)](https://coveralls.io/github/bloveit/graphql-utilities?branch=master)
35

46
Inspired by [`graph.ql`](https://github.com/MatthewMueller/graph.ql), [`graphql-tools`](https://github.com/apollostack/graphql-tools), and [`graphql-helpers`](https://github.com/depop/graphql-helpers).
57

68
## Why?
7-
There are various libraries out there providing utilities for GraphQL and even the [reference implementation](https://github.com/graphql/graphql-js) itself is adding [new utilities](https://github.com/graphql/graphql-js/pull/471). So why do we need another one?
9+
If there are various libraries out there providing utilities for [GraphQL.js](https://github.com/graphql/graphql-js), why do we need another library?
810

9-
None of those libraries let you build GraphQL types using the schema language. This prevents gradual adoption of the tools and makes code separation and isolation a nightmare.
11+
None of those libraries let you build GraphQL types using the schema language. This prevents their gradual adoption and makes code separation and isolation a nightmare.
1012

11-
With `graphql-utilities` it's simple. `build` makes it extremely simple to build a GraphQL type.
13+
With `graphql-utilities`, it's simple. Here's how you can use [`build`](#build) to make a GraphQL type.
1214

1315
```js
14-
build(`
15-
type Query {
16-
ok: Boolean!
16+
const { User } = build(`
17+
type User {
18+
id: ID!
19+
email: String!
20+
# The user's full name.
21+
name: String!
1722
}
18-
`);
23+
`, {
24+
User: {
25+
name: ({ firstName, lastName }) => `${firstName} ${lastName}`,
26+
}
27+
});
1928

2029
/* is equivalent to */
2130

22-
new GraphQLObjectType({
23-
name: 'Query',
31+
const User = new GraphQLObjectType({
32+
name: 'User',
2433
fields: () => ({
25-
ok: { type: new GraphQLNonNull(GraphQLBoolean) },
34+
id: {
35+
type: new GraphQLNonNull(GraphQLID),
36+
},
37+
email: {
38+
type: new GraphQLNonNull(GraphQLString),
39+
},
40+
name: {
41+
type: new GraphQLNonNull(GraphQLString),
42+
resolve: ({ firstName, lastName }) => `${firstName} ${lastName}`,
43+
description: 'The user\'s full name.'
44+
},
2645
}),
27-
})
46+
});
2847
```
2948

30-
## Installation
49+
## Getting Started
50+
### Installation
3151
```
32-
npm install --save graphql-utilities
52+
npm install --save graphql graphql-utilities
3353
```
3454

35-
## Getting Started
55+
### Example
56+
Head over to the [example](https://github.com/bloveit/graphql-utilities/tree/master/example) folder to see an implementation of the GraphQL.js Star Wars example using `graphql-utilities`.
57+
58+
### I want to use `.graphql` files
3659
```js
37-
import { build } from 'graphql-utilities';
60+
const makeFileBuilder = (fn) => (...args) =>
61+
fn(fs.readFileSync(__dirname.replace(/\.js$/, '.graphql', 'utf-8')), ...args);
3862

39-
// you can build a type
40-
const Record = build(`
41-
interface Record {
42-
id: ID!
43-
}
44-
`);
63+
const fileBuild = makeFileBuilder(build);
64+
const fileBuildType = makeFileBuilder(buildType);
65+
const fileBuildSchema = makeFileBuilder(buildSchema);
66+
```
67+
```graphql
68+
# User.graphql
4569

46-
// or you can build a schema
47-
const Schema = build(`
48-
schema {
49-
query: Query
50-
}
70+
type User {
71+
id: ID!
72+
email: String!
73+
# The user's full name.
74+
name: String!
75+
}
76+
```
77+
```js
78+
/* User.js */
5179

52-
type Query {
53-
ok: Boolean!
54-
}
55-
`);
80+
fileBuildType({
81+
name: ({ firstName, lastName }) => `${firstName} ${lastName}`,
82+
});
5683
```
5784

58-
## TODO
59-
- [ ] Add detailed API docs.
60-
- [x] ~~Make `build` configuration interchangeable with types.~~
61-
- [x] ~~Allow `build` to accept a flag to skip inferred schema.~~
85+
## API
86+
### build
87+
```js
88+
function build(
89+
source: string,
90+
configMap?: BuildConfigMap,
91+
typeDependencies?: GraphQLTypesThunk | Array<GraphQLType>,
92+
): GraphQLType | GraphQLSchema
93+
```
6294

6395
## License
64-
MIT
96+
[MIT](https://github.com/bloveit/graphql-utilities/blob/master/LICENSE)

package.json

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "graphql-utilities",
33
"version": "0.0.14",
4-
"description": "Utilities for GraphQL",
4+
"description": "Utilities for GraphQL",
55
"author": "Miguel Oller <miguel@bloveit.com>",
66
"license": "MIT",
77
"repository": {
@@ -23,24 +23,20 @@
2323
"prepublish": "npm run lint && npm run test && npm run build"
2424
},
2525
"peerDependencies": {
26-
"graphql": ">=0.7.0"
26+
"graphql": "^0.8.2"
2727
},
2828
"devDependencies": {
2929
"babel-cli": "^6.14.0",
30-
"babel-eslint": "^6.1.2",
31-
"babel-jest": "^15.0.0",
32-
"babel-polyfill": "^6.13.0",
33-
"babel-preset-es2015": "^6.14.0",
34-
"babel-preset-es2017": "^6.14.0",
35-
"babel-preset-latest": "^6.14.0",
36-
"babel-preset-stage-0": "^6.5.0",
30+
"babel-eslint": "^7.1.1",
31+
"babel-jest": "^17.0.2",
32+
"babel-plugin-transform-object-rest-spread": "^6.19.0",
33+
"babel-preset-env": "0.0.9",
3734
"coveralls": "^2.11.12",
38-
"eslint": "^3.4.0",
39-
"eslint-config-airbnb-base": "^5.0.3",
40-
"eslint-plugin-babel": "^3.3.0",
41-
"eslint-plugin-import": "^1.14.0",
42-
"graphql": "^0.7.0",
43-
"jest": "^15.1.1"
35+
"eslint": "^3.11.1",
36+
"eslint-config-airbnb-base": "^10.0.1",
37+
"eslint-plugin-import": "^2.2.0",
38+
"graphql": "^0.8.2",
39+
"jest": "^17.0.3"
4440
},
4541
"jest": {
4642
"collectCoverageFrom": [

src/build/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export default function build(source, config = {}, typeDeps = [], infer = true)
7575
const firstDefinition = definitions[0];
7676
const shouldReturnOneType = (
7777
definitions.length === 1 &&
78-
~Object.keys(buildMap).indexOf(firstDefinition.kind) &&
78+
~Object.keys(buildMap).indexOf(firstDefinition.kind) && // eslint-disable-line no-bitwise
7979
!(firstDefinition.name.value === 'Query' && infer)
8080
);
8181
const types = ensureNoDuplicateTypes(buildTypes(

0 commit comments

Comments
 (0)