diff --git a/src/index.js b/src/index.js index 325fd44c..e0ba03c0 100644 --- a/src/index.js +++ b/src/index.js @@ -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. @@ -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.'); } diff --git a/test.js b/test.js index 68560f66..08145f9d 100644 --- a/test.js +++ b/test.js @@ -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); + }) + }) + }); });