mock-apollo-client
Advanced tools
Comparing version 0.5.0 to 0.6.0-feature-fragments.0
@@ -19,2 +19,3 @@ "use strict"; | ||
var printer_1 = require("graphql/language/printer"); | ||
var visitor_1 = require("graphql/language/visitor"); | ||
var MockLink = (function (_super) { | ||
@@ -28,11 +29,7 @@ __extends(MockLink, _super); | ||
MockLink.prototype.setRequestHandler = function (requestQuery, handler) { | ||
var identifiers = getIdentifiers(requestQuery); | ||
for (var _i = 0, identifiers_1 = identifiers; _i < identifiers_1.length; _i++) { | ||
var identifier = identifiers_1[_i]; | ||
var key = requestToKey(identifier); | ||
if (this.requestHandlers[key]) { | ||
throw new Error("Request handler already defined for query: " + format(identifier)); | ||
} | ||
this.requestHandlers[key] = handler; | ||
var key = requestToKey(requestQuery); | ||
if (this.requestHandlers[key]) { | ||
throw new Error("Request handler already defined for query: " + format(requestQuery)); | ||
} | ||
this.requestHandlers[key] = handler; | ||
}; | ||
@@ -70,16 +67,26 @@ MockLink.prototype.request = function (operation) { | ||
exports.MockLink = MockLink; | ||
var getIdentifiers = function (requestQuery) { | ||
if (!apollo_utilities_1.hasDirectives(['client', 'connection'], requestQuery)) { | ||
return [requestQuery]; | ||
} | ||
var withoutDirectives = apollo_utilities_1.removeClientSetsFromDocument(requestQuery); | ||
withoutDirectives = withoutDirectives !== null | ||
? apollo_utilities_1.removeConnectionDirectiveFromDocument(withoutDirectives) | ||
var normalise = function (requestQuery) { | ||
var stripped = apollo_utilities_1.removeClientSetsFromDocument(requestQuery); | ||
stripped = stripped !== null | ||
? apollo_utilities_1.removeConnectionDirectiveFromDocument(stripped) | ||
: null; | ||
return withoutDirectives === null | ||
? [requestQuery] | ||
: [requestQuery, withoutDirectives]; | ||
stripped = stripped !== null | ||
? stripTypenames(stripped) | ||
: null; | ||
return stripped === null | ||
? requestQuery | ||
: stripped; | ||
}; | ||
var stripTypenames = function (document) { | ||
return visitor_1.visit(document, { | ||
Field: { | ||
enter: function (node) { return node.name.value === '__typename' | ||
? null | ||
: undefined; }, | ||
}, | ||
}); | ||
}; | ||
var requestToKey = function (query) { | ||
var queryString = printer_1.print(query); | ||
var normalised = normalise(query); | ||
var queryString = printer_1.print(normalised); | ||
var requestKey = { query: queryString }; | ||
@@ -86,0 +93,0 @@ return JSON.stringify(requestKey); |
{ | ||
"name": "mock-apollo-client", | ||
"version": "0.5.0", | ||
"version": "0.6.0-feature-fragments.0", | ||
"description": "Library to help unit testing when using apollo-client", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16139
171