Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graphql-import

Package Overview
Dependencies
Maintainers
3
Versions
431
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-import - npm Package Compare versions

Comparing version 0.1.9 to 0.2.0

43

dist/index.js

@@ -24,3 +24,3 @@ "use strict";

var matches = importLine.match(/^import (\*|(.*)) from ('|")(.*)('|")$/);
if (matches.length !== 6) {
if (!matches || matches.length !== 6 || !matches[4]) {
throw new Error("Too few regex matches: " + matches);

@@ -63,3 +63,26 @@ }

// Post processing of the final schema (missing types, unused types, etc.)
document.definitions = definition_1.completeDefinitionPool(lodash_1.flatten(allDefinitions), typeDefinitions[0], lodash_1.flatten(typeDefinitions));
// Query, Mutation and Subscription should be merged
// And should always be in the first set, to make sure they
// are not filtered out.
var typesToFilter = ['Query', 'Mutation', 'Subscription'];
var firstTypes = lodash_1.flatten(typeDefinitions).filter(function (d) { return typesToFilter.includes(d.name.value); });
var otherFirstTypes = typeDefinitions[0].filter(function (d) { return !typesToFilter.includes(d.name.value); });
var firstSet = otherFirstTypes.concat(firstTypes);
var processedTypeNames = [];
var mergedFirstTypes = [];
var _loop_1 = function (type) {
if (!processedTypeNames.includes(type.name.value)) {
processedTypeNames.push(type.name.value);
mergedFirstTypes.push(type);
}
else {
var existingType = mergedFirstTypes.find(function (t) { return t.name.value === type.name.value; });
existingType.fields = existingType.fields.concat(type.fields);
}
};
for (var _i = 0, firstSet_1 = firstSet; _i < firstSet_1.length; _i++) {
var type = firstSet_1[_i];
_loop_1(type);
}
document.definitions = definition_1.completeDefinitionPool(lodash_1.flatten(allDefinitions), firstSet, lodash_1.flatten(typeDefinitions));
// Return the schema as string

@@ -120,2 +143,3 @@ return graphql_1.print(document);

function filterImportedDefinitions(imports, typeDefinitions) {
// This should do something smart with fields
var filteredDefinitions = filterTypeDefinitions(typeDefinitions);

@@ -126,3 +150,16 @@ if (imports.includes('*')) {

else {
return filteredDefinitions.filter(function (d) { return imports.includes(d.name.value); });
var result = filteredDefinitions.filter(function (d) { return imports.map(function (i) { return i.split('.')[0]; }).includes(d.name.value); });
var fieldImports = imports
.filter(function (i) { return i.split('.').length > 1; });
var groupedFieldImports = lodash_1.groupBy(fieldImports, function (x) { return x.split('.')[0]; });
var _loop_2 = function (rootType) {
var fields = groupedFieldImports[rootType].map(function (x) { return x.split('.')[1]; });
filteredDefinitions.find(function (def) { return def.name.value === rootType; }).fields =
filteredDefinitions.find(function (def) { return def.name.value === rootType; }).fields
.filter(function (f) { return fields.includes(f.name.value) || fields.includes('*'); });
};
for (var rootType in groupedFieldImports) {
_loop_2(rootType);
}
return result;
}

@@ -129,0 +166,0 @@ }

@@ -11,2 +11,8 @@ "use strict";

});
ava_1.default('parseImportLine: invalid', function (t) {
t.throws(function () { return _1.parseImportLine("import from \"schema.graphql\""); }, Error);
});
ava_1.default('parseImportLine: invalid 2', function (t) {
t.throws(function () { return _1.parseImportLine("import A from \"\""); }, Error);
});
ava_1.default('parseImportLine: parse multi import', function (t) {

@@ -95,2 +101,36 @@ t.deepEqual(_1.parseImportLine("import A, B from \"schema.graphql\""), {

});
ava_1.default('root field imports', function (t) {
var expectedSDL = "type Dummy {\n field: String\n}\n\ntype Query {\n posts(filter: PostFilter): [Post]\n}\n\ninput PostFilter {\n field3: Int\n}\n\ntype Post {\n field1: String\n}\n";
var actualSDL = _1.importSchema('fixtures/root-fields/a.graphql');
t.is(actualSDL, expectedSDL);
});
ava_1.default('merged root field imports', function (t) {
var expectedSDL = "type Dummy {\n field: String\n}\n\ntype Query {\n helloA: String\n posts(filter: PostFilter): [Post]\n hello: String\n}\n\ninput PostFilter {\n field3: Int\n}\n\ntype Post {\n field1: String\n}\n";
var actualSDL = _1.importSchema('fixtures/merged-root-fields/a.graphql');
t.is(actualSDL, expectedSDL);
});
ava_1.default('missing type on type', function (t) {
var err = t.throws(function () { return _1.importSchema('fixtures/type-not-found/a.graphql'); }, Error);
t.is(err.message, "Field test: Couldn't find type Post in any of the schemas.");
});
ava_1.default('missing type on interface', function (t) {
var err = t.throws(function () { return _1.importSchema('fixtures/type-not-found/b.graphql'); }, Error);
t.is(err.message, "Field test: Couldn't find type Post in any of the schemas.");
});
ava_1.default('missing type on input type', function (t) {
var err = t.throws(function () { return _1.importSchema('fixtures/type-not-found/c.graphql'); }, Error);
t.is(err.message, "Field myfield: Couldn't find type Post in any of the schemas.");
});
ava_1.default('missing interface type', function (t) {
var err = t.throws(function () { return _1.importSchema('fixtures/type-not-found/d.graphql'); }, Error);
t.is(err.message, "Couldn't find interface MyInterface in any of the schemas.");
});
ava_1.default('missing union type', function (t) {
var err = t.throws(function () { return _1.importSchema('fixtures/type-not-found/e.graphql'); }, Error);
t.is(err.message, "Couldn't find type C in any of the schemas.");
});
ava_1.default('missing type on input type', function (t) {
var err = t.throws(function () { return _1.importSchema('fixtures/type-not-found/f.graphql'); }, Error);
t.is(err.message, "Field myfield: Couldn't find type Post in any of the schemas.");
});
//# sourceMappingURL=index.test.js.map
{
"name": "graphql-import",
"version": "0.1.9",
"version": "0.2.0",
"license": "MIT",

@@ -14,6 +14,25 @@ "repository": "git@github.com:graphcool/graphql-import.git",

},
"nyc": {
"extension": [
".ts"
],
"require": [
"ts-node/register"
],
"include": [
"src/**/*.ts"
],
"exclude": [
"**/*.d.ts",
"**/*.test.ts"
],
"all": true,
"sourceMap": true,
"instrument": true
},
"scripts": {
"prepare": "npm run build",
"build": "rm -rf dist && tsc -d",
"test-only": "npm run build && ava --verbose dist/**/*.test.js",
"testlocal": "npm run build && nyc --reporter lcov --reporter text ava-ts --verbose src/**/*.test.ts",
"test-only": "npm run build && nyc --reporter lcov ava-ts --verbose src/**/*.test.ts --tap | tap-xunit > ~/reports/ava.xml",
"test": "tslint src/**/*.ts && npm run test-only",

@@ -24,4 +43,8 @@ "docs": "typedoc --out docs src/index.ts --hideGenerator --exclude **/*.test.ts",

"devDependencies": {
"@types/node": "8.5.2",
"@types/node": "8.5.5",
"ava": "0.24.0",
"ava-ts": "0.23.0",
"nyc": "11.4.1",
"tap-xunit": "2.2.0",
"ts-node": "4.1.0",
"tslint": "5.8.0",

@@ -28,0 +51,0 @@ "tslint-config-standard": "7.0.0",

3

README.md
# graphql-import
[![Build Status](https://travis-ci.org/graphcool/graphql-import.svg?branch=master)](https://travis-ci.org/graphcool/graphql-import) [![npm version](https://badge.fury.io/js/graphql-import.svg)](https://badge.fury.io/js/graphql-import)
[![CircleCI](https://circleci.com/gh/graphcool/graphql-import.svg?style=shield)](https://circleci.com/gh/graphcool/graphql-import) [![npm version](https://badge.fury.io/js/graphql-import.svg)](https://badge.fury.io/js/graphql-import)
Import &amp; export definitions in GraphQL SDL (also refered to as GraphQL modules)

@@ -6,0 +7,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc