Skip to content

Commit

Permalink
Merge pull request #14 from dustinfarris/inject-cashay
Browse files Browse the repository at this point in the history
[BREAKING]: Receive injected cashay
  • Loading branch information
dustinfarris authored Nov 25, 2016
2 parents 966943e + b5bb90e commit 76919d8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

Convert a server graphql schema into a client-safe schema to use with [Cashay][].

Compatible with graphql ^0.7.1 and cashay ^0.22.0. (You must install them yourself)

```
npm i --save graphql
npm i --save cashay
```


## Installation

Expand Down Expand Up @@ -49,15 +56,17 @@ path that you specify.

## Usage

Note: You must bring your own graphql
Note: You must bring your own graphql and cashay

```js
var cashaySchemaGenerator = require('broccoli-cashay-schema');
var cashay = require('cashay');
var graphql = require('graphql');

// The plugin will look for a schema.js file in the node you provide
// You can override with the option `serverSchemaPath`
var node = cashaySchemaGenerator(inputNode, {
cashay: cashay,
graphql: graphql,
clientSchemaPath: 'client/schema.js'
});
Expand All @@ -75,6 +84,7 @@ This can be done easily with [broccoli-babel-transpiler](https://github.com/babe
var esTranspiler = require('broccoli-babel-transpiler');

var node = cashaySchemaGenerator(esTranspiler(inputNode), {
cashay: cashay,
graphql: graphql,
clientSchemaPath: 'client/schema.js'
});
Expand Down
21 changes: 16 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,39 @@ var Filter = require('broccoli-filter');
var fs = require('fs');
var mkdirp = require('mkdirp');
var path = require('path');
var transformSchema = require('cashay').transformSchema;

module.exports = CashaySchema;

CashaySchema.prototype = Object.create(Filter.prototype);
CashaySchema.constructor = CashaySchema;

function checkOptions(options) {
if (!options.graphql) {
throw new Error(
'You must provide the graphql package to broccoli-cashay-schema');
}

if (!options.cashay) {
throw new Error(
'You must provide the cashay package to broccoli-cashay-schema');
}
}

function CashaySchema(inputNode, _options) {
var options = _options || {};

if (!(this instanceof CashaySchema)) {
return new CashaySchema(inputNode, options);
}

if (!options.graphql) {
throw new Error('You must provide the graphql package');
}
checkOptions(options);

Filter.call(this, inputNode, {
annotation: options.annotation
});

// Cashay package
this.cashay = options.cashay;
// GraphQL package
this.graphql = options.graphql;
// Server schema path relative to `inputNode`
Expand All @@ -53,7 +64,7 @@ CashaySchema.prototype.processFile = function(srcDir, destDir, relativePath) {
var rootSchema = require(fullPath)(this.graphql);

// Generate a client-safe schema
return transformSchema(rootSchema, this.graphql.graphql).
return this.cashay.transformSchema(rootSchema, this.graphql.graphql).
then(function(clientSchema) {
var content = 'export default ' + JSON.stringify(clientSchema);
mkdirp.sync(path.dirname(outputPath));
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
"homepage": "https://github.com/dustinfarris/broccoli-cashay-schema#readme",
"dependencies": {
"broccoli-filter": "^1.2.4",
"cashay": "^0.22.0",
"mkdirp": "^0.5.1",
"string.prototype.startswith": "^0.2.0"
},
"devDependencies": {
"eslint": "^3.10.2"
},
"peerDependencies": {
"cashay": "^0.22.0",
"graphql": "^0.7.1"
}
}

0 comments on commit 76919d8

Please sign in to comment.