Skip to content

Commit c32be21

Browse files
darren128darren.richards
authored andcommitted
Add ast toString returning query string
1 parent 449834f commit c32be21

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ function resetCaches() {
2424
fragmentSourceMap = {};
2525
}
2626

27+
// For defining doc.toString
28+
function toString(loc) {
29+
return loc.source.body;
30+
31+
}
32+
2733
// Take a unstripped parsed document (query/mutation or even fragment), and
2834
// check all fragment definitions, checking for name->source uniqueness.
2935
// We also want to make sure only unique fragments exist in the document.
@@ -135,6 +141,7 @@ function parseDocument(doc) {
135141
// existing fragments of the same name
136142
parsed = processFragments(parsed);
137143
parsed = stripLoc(parsed, false);
144+
parsed.toString = toString.bind(null, parsed.loc);
138145
docCache[cacheKey] = parsed;
139146

140147
return parsed;

test/graphql.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,13 @@ const assert = require('chai').assert;
438438
});
439439
});
440440

441+
it('returned document toString returns query string', () => {
442+
const query = `{ user(id: 5) { firstName lastName } }`;
443+
const doc = gql(query);
444+
445+
assert.equal(doc.toString(), query);
446+
});
447+
441448
// How to make this work?
442449
// it.only('can reference a fragment passed as a document via shorthand', () => {
443450
// const ast = gql`

0 commit comments

Comments
 (0)