annotated-graphql
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -45,4 +45,11 @@ 'use strict'; | ||
var schemaAnnotations = _annotatedGraphQLSche.schemaAnnotations; | ||
var query = (0, _type2.default)((0, _parse2.default)(schemaText), createImplementation(schemaAnnotations)).objectTypes[findQueryTypeName(schemaAnnotations)]; | ||
var schemaDocument = (0, _parse2.default)(schemaText);var schemaImplementation = {}; | ||
buildImplementation(schemaAnnotations, schemaImplementation, schemaDocument); | ||
var schemaTypes = (0, _type2.default)(schemaDocument, schemaImplementation), | ||
query = schemaTypes.objectTypes[findQueryTypeName(schemaAnnotations)]; | ||
annotateTypes(schemaAnnotations, schemaTypes, schemaDocument); | ||
return new _graphql.GraphQLSchema({ | ||
@@ -60,12 +67,18 @@ query: query | ||
function createImplementation(schemaAnnotations) { | ||
return schemaAnnotations.reduce(applySchemaAnnotation, {}); | ||
function buildImplementation(schemaAnnotations, schemaImplementation, schemaDocument) { | ||
schemaAnnotations.reduce(applySchemaAnnotation, schemaImplementation); | ||
function applySchemaAnnotation(implementation, schemaAnnotation) { | ||
schemaAnnotation.apply(implementation); | ||
function applySchemaAnnotation(schemaImplementation, schemaAnnotation) { | ||
schemaAnnotation.onBuildImplementation(schemaImplementation, schemaDocument); | ||
return implementation; | ||
return schemaImplementation; | ||
} | ||
} | ||
function annotateTypes(schemaAnnotations, schemaTypes, schemaDocument) { | ||
schemaAnnotations.forEach(function (schemaAnnotation) { | ||
return schemaAnnotation.onAnnotateTypes(schemaTypes, schemaDocument); | ||
}); | ||
} | ||
function findQueryTypeName(schemaAnnotations) { | ||
@@ -72,0 +85,0 @@ var queryAnnotation = schemaAnnotations.find(function (schemaAnnotation) { |
@@ -29,2 +29,12 @@ "use strict"; | ||
function extractAnnotations(annotationExtractors, annotatedSchemaText, schemaAnnotations) { | ||
annotationExtractors = annotationExtractors.reduce(function (annotationExtractors, annotationExtractor) { | ||
if (annotationExtractor.length > 0) { | ||
annotationExtractors = annotationExtractors.concat(annotationExtractor); | ||
} else { | ||
annotationExtractors.push(annotationExtractor); | ||
} | ||
return annotationExtractors; | ||
}, []); | ||
return annotationExtractors.reduce(function (schemaText, annotationExtractor) { | ||
@@ -31,0 +41,0 @@ return applyExtractor(annotationExtractor, schemaText, schemaAnnotations); |
@@ -25,6 +25,6 @@ 'use strict'; | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(_class).call(this, '\\btype\\s*(\\w*)\\s*\\{[^}]*?(@' + annotationTypeName + '\\s*\\(([\\s|\\S]*?)\\))\\s*(\\w*).*', extractorFactory)); | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(_class).call(this, '\\btype\\s*(\\w*)\\s*\\{[^}]*?(@' + annotationTypeName + '\\s*\\(([\\s|\\S]*?)\\))\\s*(@\\w*\\s*\\([\\s|\\S]*?\\)\\s*)*(\\w*).*', extractorFactory)); | ||
function extractorFactory(annotations) { | ||
return function (match, typeName, wholeAnnotation, attributes, fieldName) { | ||
return function (match, typeName, wholeAnnotation, attributes, otherAnnotations, fieldName) { | ||
var annotation = new AnnotationClass(typeName, fieldName); | ||
@@ -31,0 +31,0 @@ |
@@ -66,2 +66,16 @@ 'use strict'; | ||
}); | ||
it('should extract first field annotation', function () { | ||
var annotations = []; | ||
var text = fieldAnnotationExtractor.extract('type Bar { bar() } type Foo { @foo(bar: "bar") @bar(baz: "baz") foo() }', annotations); | ||
text.should.equal('type Bar { bar() } type Foo { @bar(baz: "baz") foo() }'); | ||
annotations.should.be.deepEqual([{ | ||
typeName: 'Foo', | ||
fieldName: 'foo', | ||
'bar': 'bar' | ||
}]); | ||
annotations[0].should.be.instanceOf(FooAnnotation); | ||
}); | ||
}); |
@@ -13,2 +13,6 @@ 'use strict'; | ||
var _fieldAnnotationExtractor = require('./field-annotation-extractor'); | ||
var _fieldAnnotationExtractor2 = _interopRequireDefault(_fieldAnnotationExtractor); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -22,17 +26,32 @@ | ||
value: function createExtractor() { | ||
return new _typeAnnotationExtractor2.default('graphql', GraphQLSchemaAnnotation); | ||
return [new _typeAnnotationExtractor2.default('graphql', GraphQLSchemaAnnotation), new _fieldAnnotationExtractor2.default('graphql', GraphQLSchemaAnnotation)]; | ||
} | ||
}]); | ||
function GraphQLSchemaAnnotation(typeName) { | ||
function GraphQLSchemaAnnotation(typeName, fieldName) { | ||
_classCallCheck(this, GraphQLSchemaAnnotation); | ||
this.typeName = typeName; | ||
this.fieldName = fieldName; | ||
} | ||
_createClass(GraphQLSchemaAnnotation, [{ | ||
key: 'apply', | ||
value: function apply(schemaImplementation) { | ||
key: 'onBuildImplementation', | ||
value: function onBuildImplementation(schemaImplementation) { | ||
// noop | ||
} | ||
}, { | ||
key: 'onAnnotateTypes', | ||
value: function onAnnotateTypes(schemaTypes) { | ||
if (this.description) { | ||
var itemNode = findNode(schemaTypes, this.typeName, this.fieldName); | ||
if (itemNode) { | ||
// TODO: description does not work for fields | ||
itemNode.description = this.description; | ||
} else { | ||
console.log('Document node for \'typeName\': ' + this.typeName + ', \'fieldName\': ' + fieldName + '.'); | ||
} | ||
} | ||
} | ||
}]); | ||
@@ -43,2 +62,15 @@ | ||
exports.default = GraphQLSchemaAnnotation; | ||
exports.default = GraphQLSchemaAnnotation; | ||
function findNode(schemaTypes, typeName, fieldName) { | ||
var type = schemaTypes.objectTypes[typeName]; | ||
if (type) { | ||
if (fieldName) { | ||
return type['_typeConfig'].fields()[fieldName]; | ||
} else { | ||
return type; | ||
} | ||
} | ||
} |
@@ -12,6 +12,8 @@ 'use strict'; | ||
it('should not modify anything when no annotations match', function () { | ||
var graphqlSchemaAnnotationExtractor = _graphqlSchemaAnnotation2.default.createExtractor(), | ||
var graphqlSchemaAnnotationExtractors = _graphqlSchemaAnnotation2.default.createExtractor(), | ||
schemaAnnotations = []; | ||
var schemaText = graphqlSchemaAnnotationExtractor.extract('foo bar', schemaAnnotations); | ||
var schemaText = graphqlSchemaAnnotationExtractors.reduce(function (schemaText, extractor) { | ||
return extractor.extract(schemaText, schemaAnnotations); | ||
}, 'foo bar'); | ||
@@ -22,7 +24,9 @@ schemaText.should.be.equal('foo bar'); | ||
it('should extract the matching annotation', function () { | ||
var graphqlSchemaAnnotationExtractor = _graphqlSchemaAnnotation2.default.createExtractor(), | ||
schemaAnnotations = []; | ||
it('should extract the matching annotations', function () { | ||
var graphqlSchemaAnnotationExtractors = _graphqlSchemaAnnotation2.default.createExtractor(); | ||
var schemaText = graphqlSchemaAnnotationExtractor.extract('@graphql(role: "query") type Foo { foo() }', schemaAnnotations); | ||
var schemaAnnotations = [], | ||
schemaText = graphqlSchemaAnnotationExtractors.reduce(function (schemaText, extractor) { | ||
return extractor.extract(schemaText, schemaAnnotations); | ||
}, '@graphql(role: "query") type Foo { foo() }'); | ||
@@ -32,6 +36,19 @@ schemaText.should.be.equal('type Foo { foo() }'); | ||
typeName: 'Foo', | ||
fieldName: undefined, | ||
role: 'query' | ||
}]); | ||
schemaAnnotations = []; | ||
schemaText = graphqlSchemaAnnotationExtractors.reduce(function (schemaText, extractor) { | ||
return extractor.extract(schemaText, schemaAnnotations); | ||
}, 'type Foo { @graphql(description: "description") foo() }'); | ||
schemaText.should.be.equal('type Foo { foo() }'); | ||
schemaAnnotations.should.be.deepEqual([{ | ||
typeName: 'Foo', | ||
fieldName: 'foo', | ||
description: 'description' | ||
}]); | ||
}); | ||
}); | ||
}); |
@@ -39,4 +39,4 @@ 'use strict'; | ||
_createClass(RestSchemaAnnotation, [{ | ||
key: 'apply', | ||
value: function apply(schemaImplementation) { | ||
key: 'onBuildImplementation', | ||
value: function onBuildImplementation(schemaImplementation) { | ||
var type = getOrCreate(schemaImplementation, this.typeName); | ||
@@ -46,2 +46,7 @@ | ||
} | ||
}, { | ||
key: 'onAnnotateTypes', | ||
value: function onAnnotateTypes(schemaTypes) { | ||
// noop | ||
} | ||
}]); | ||
@@ -48,0 +53,0 @@ |
@@ -18,3 +18,3 @@ 'use strict'; | ||
describe('GraphQLSchemaAnnotation', function () { | ||
describe('apply()', function () { | ||
describe('onBuildImplementation()', function () { | ||
it('should add a resolver function to the field bar of the type foo', function () { | ||
@@ -24,3 +24,3 @@ var restSchemaAnnotation = new _restSchemaAnnotation2.default('foo', 'bar'), | ||
restSchemaAnnotation.apply(implementation); | ||
restSchemaAnnotation.onBuildImplementation(implementation); | ||
@@ -66,3 +66,3 @@ implementation.foo.bar.should.be.Function(); | ||
restSchemaAnnotation = new _restSchemaAnnotation2.default('foo', 'bar'); | ||
restSchemaAnnotation.apply(implementation); | ||
restSchemaAnnotation.onBuildImplementation(implementation); | ||
@@ -69,0 +69,0 @@ resolver = implementation.foo.bar; |
@@ -25,6 +25,6 @@ 'use strict'; | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(_class).call(this, '@' + annotationTypeName + '\\s*\\(([\\s|\\S]*?)\\)\\s*(type\\s*(\\w*)\\s*{[\\s|\\S]*?\\})', extractorFactory)); | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(_class).call(this, '@' + annotationTypeName + '\\s*\\(([\\s|\\S]*?)\\)\\s*(@\\w*\\s*\\([\\s|\\S]*?\\)\\s*)*(type\\s*(\\w*)\\s*{[\\s|\\S]*?\\})', extractorFactory)); | ||
function extractorFactory(annotations) { | ||
return function (match, attributes, typeDeclaration, typeName) { | ||
return function (match, attributes, otherAnnotations, typeDeclaration, typeName) { | ||
var annotation = new AnnotationClass(typeName); | ||
@@ -31,0 +31,0 @@ |
@@ -50,2 +50,15 @@ 'use strict'; | ||
}); | ||
it('should extract first type annotation', function () { | ||
var annotations = []; | ||
var text = typeAnnotationExtractor.extract('@foo(bar: "bar") @bar(baz: "baz") type Foo { foo() }', annotations); | ||
text.should.equal('type Foo { foo() }'); | ||
annotations.should.be.deepEqual([{ | ||
typeName: 'Foo', | ||
'bar': 'bar' | ||
}]); | ||
annotations[0].should.be.instanceOf(FooAnnotation); | ||
}); | ||
}); |
{ | ||
"name": "annotated-graphql", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Annotated GraphQL", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
74067
1711