diff --git a/index.js b/index.js index febfd590..dd993241 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,40 @@ var parse = require('./parser').parse; var cache = {}; +function stripLoc (doc) { + var docType = Object.prototype.toString.call(doc); + + if (docType === '[object Array]') { + return doc.map(stripLoc); + } + + if (docType !== '[object Object]') { + throw new Error('Unexpected input.'); + } + + if (doc.loc) { + delete doc.loc; + } + + var keys = Object.keys(doc); + var key; + var value; + var valueType; + + for (key in keys) { + if (keys.hasOwnProperty(key)) { + value = doc[keys[key]]; + valueType = Object.prototype.toString.call(value); + + if (valueType === '[object Object]' || valueType === '[object Array]') { + doc[keys[key]] = stripLoc(value); + } + } + } + + return doc; +} + function parseDocument(doc) { if (cache[doc]) { return cache[doc]; @@ -13,6 +47,8 @@ function parseDocument(doc) { throw new Error('Not a valid GraphQL document.'); } + parsed = stripLoc(parsed); + cache[doc] = parsed; return parsed; diff --git a/package.json b/package.json index 92d3999a..ec7b6a84 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,6 @@ "babel-register": "^6.9.0", "chai": "^3.5.0", "graphql": "^0.6.0", - "lodash": "^4.13.1", "mocha": "^2.5.3", "webpack": "^1.13.1" } diff --git a/test.js b/test.js index c183b499..86933c18 100644 --- a/test.js +++ b/test.js @@ -1,23 +1,6 @@ var gqlRequire = require('./index'); var gqlDefault = require('./index').default; import { assert } from 'chai'; -import _ from 'lodash'; - -function stripLoc(obj) { - if (_.isArray(obj)) { - return obj.map(stripLoc); - } - - if (! _.isObject(obj)) { - return obj; - } - - const omitted = _.omit(obj, ['loc']); - - return _.mapValues(omitted, (value) => { - return stripLoc(value); - }); -} [gqlRequire, gqlDefault].forEach((gql, i) => { describe(`gql ${i}`, () => { @@ -30,14 +13,14 @@ function stripLoc(obj) { }); it('is correct for a simple query', () => { - const ast = stripLoc(gql` + const ast = gql` { user(id: 5) { firstName lastName } } - `); + `; assert.deepEqual(ast, { "kind": "Document",