Skip to content

Commit

Permalink
feat(descriptors): add support for descriptors (#99)
Browse files Browse the repository at this point in the history
* feat(descriptors): add support for descriptors

* Improve descriptor test with more assertions
  • Loading branch information
jamiter authored and jnwng committed Jun 16, 2017
1 parent cb13e60 commit 78ed906
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var parser = require('graphql/language/parser');
var buildASTSchema = require('graphql/utilities/buildASTSchema');

var parse = parser.parse;
var getDescription = buildASTSchema.getDescription;

// Strip insignificant whitespace
// Note that this could do a lot more, such as reorder fields etc.
Expand Down Expand Up @@ -83,6 +85,14 @@ function stripLoc(doc, removeLocAtThisLevel) {
});
}

if (!doc.description) {
var description = getDescription(doc);

if (description) {
doc.description = description;
}
}

if (docType !== '[object Object]') {
throw new Error('Unexpected input.');
}
Expand Down
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,5 +433,21 @@ const assert = require('chai').assert;
// `);
// });

describe('descriptors', function() {
it('adds comments as descriptors', function() {
const ast = gql`
# This is a type descriptor
type Foo {
# This is a field descriptor
bar: String
baz: Int
}
`;
assert.equal(ast.definitions[0].description, 'This is a type descriptor');
assert.equal(ast.definitions[0].fields[0].description, 'This is a field descriptor');
assert.equal(ast.definitions[0].fields[1].description, undefined);
})
})

});
});

0 comments on commit 78ed906

Please sign in to comment.