graphql-import
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -29,2 +29,4 @@ /** | ||
*/ | ||
export declare function importSchema(filePath: string): string; | ||
export declare function importSchema(schema: string, schemas?: { | ||
[key: string]: string; | ||
}): string; |
@@ -8,10 +8,10 @@ "use strict"; | ||
var definition_1 = require("./definition"); | ||
var read = function (schema, schemas) { | ||
if (isFile(schema)) { | ||
return fs.readFileSync(schema, { encoding: 'utf8' }); | ||
} | ||
return schemas ? schemas[schema] : schema; | ||
}; | ||
var isFile = function (f) { return f.endsWith('.graphql'); }; | ||
/** | ||
* Read a schema file from disk | ||
* | ||
* @param f Filename | ||
* @returns File contents | ||
*/ | ||
var read = function (f) { return fs.readFileSync(f, { encoding: 'utf8' }); }; | ||
/** | ||
* Parse a single import line and extract imported types and schema filename | ||
@@ -57,7 +57,7 @@ * | ||
*/ | ||
function importSchema(filePath) { | ||
var sdl = read(filePath); | ||
function importSchema(schema, schemas) { | ||
var sdl = read(schema, schemas) || schema; | ||
var document = graphql_1.parse(sdl, { noLocation: true }); | ||
// Recursively process the imports, starting by importing all types from the initial schema | ||
var _a = collectDefinitions(['*'], sdl, path.resolve(filePath)), allDefinitions = _a.allDefinitions, typeDefinitions = _a.typeDefinitions; | ||
var _a = collectDefinitions(['*'], sdl, schema, schemas), allDefinitions = _a.allDefinitions, typeDefinitions = _a.typeDefinitions; | ||
// Post processing of the final schema (missing types, unused types, etc.) | ||
@@ -105,7 +105,7 @@ // Query, Mutation and Subscription should be merged | ||
*/ | ||
function collectDefinitions(imports, sdl, filePath, processedFiles, typeDefinitions, allDefinitions) { | ||
function collectDefinitions(imports, sdl, filePath, schemas, processedFiles, typeDefinitions, allDefinitions) { | ||
if (processedFiles === void 0) { processedFiles = new Set(); } | ||
if (typeDefinitions === void 0) { typeDefinitions = []; } | ||
if (allDefinitions === void 0) { allDefinitions = []; } | ||
var key = path.resolve(filePath); | ||
var key = isFile(filePath) ? path.resolve(filePath) : filePath; | ||
var dirname = path.dirname(filePath); | ||
@@ -127,5 +127,5 @@ // Get TypeDefinitionNodes from current schema | ||
// If it was not yet processed (in case of circular dependencies) | ||
var moduleFilePath = path.resolve(path.join(dirname, m.from)); | ||
var moduleFilePath = isFile(filePath) ? path.resolve(path.join(dirname, m.from)) : m.from; | ||
if (!processedFiles.has(moduleFilePath)) { | ||
collectDefinitions(m.imports, read(moduleFilePath), moduleFilePath, processedFiles, typeDefinitions, allDefinitions); | ||
collectDefinitions(m.imports, read(moduleFilePath, schemas), moduleFilePath, schemas, processedFiles, typeDefinitions, allDefinitions); | ||
} | ||
@@ -132,0 +132,0 @@ }); |
@@ -60,2 +60,31 @@ "use strict"; | ||
}); | ||
ava_1.default('importSchema: import all from objects', function (t) { | ||
var schemaC = "\n type C1 {\n id: ID!\n }\n\n type C2 {\n id: ID!\n }\n\n type C3 {\n id: ID!\n }"; | ||
var schemaB = "\n # import * from 'schemaC'\n\n type B {\n hello: String!\n c1: C1\n c2: C2\n }"; | ||
var schemaA = "\n # import B from 'schemaB'\n\n type A {\n # test 1\n first: String @first\n second: Float\n b: B\n }"; | ||
var schemas = { | ||
schemaA: schemaA, schemaB: schemaB, schemaC: schemaC | ||
}; | ||
var expectedSDL = "type A {\n first: String @first\n second: Float\n b: B\n}\n\ntype B {\n hello: String!\n c1: C1\n c2: C2\n}\n\ntype C1 {\n id: ID!\n}\n\ntype C2 {\n id: ID!\n}\n"; | ||
t.is(_1.importSchema(schemaA, schemas), expectedSDL); | ||
}); | ||
ava_1.default("importSchema: single object schema", function (t) { | ||
var schemaA = "\n type A {\n field: String\n }"; | ||
var expectedSDL = "type A {\n field: String\n}\n"; | ||
t.is(_1.importSchema(schemaA), expectedSDL); | ||
}); | ||
ava_1.default("importSchema: import all mix 'n match", function (t) { | ||
var schemaB = "\n # import C1, C2 from 'fixtures/import-all/c.graphql'\n\n type B {\n hello: String!\n c1: C1\n c2: C2\n }"; | ||
var schemaA = "\n # import * from \"schemaB\"\n\n type A {\n # test 1\n first: String @first\n second: Float\n b: B\n }"; | ||
var schemas = { | ||
schemaB: schemaB | ||
}; | ||
var expectedSDL = "type A {\n first: String @first\n second: Float\n b: B\n}\n\ntype B {\n hello: String!\n c1: C1\n c2: C2\n}\n\ntype C1 {\n id: ID!\n}\n\ntype C2 {\n id: ID!\n}\n"; | ||
t.is(_1.importSchema(schemaA, schemas), expectedSDL); | ||
}); | ||
ava_1.default("importSchema: import all mix 'n match 2", function (t) { | ||
var schemaA = "\n # import * from \"fixtures/import-all/b.graphql\"\n\n type A {\n # test 1\n first: String @first\n second: Float\n b: B\n }"; | ||
var expectedSDL = "type A {\n first: String @first\n second: Float\n b: B\n}\n\ntype B {\n hello: String!\n c1: C1\n c2: C2\n}\n\ntype C1 {\n id: ID!\n}\n\ntype C2 {\n id: ID!\n}\n"; | ||
t.is(_1.importSchema(schemaA), expectedSDL); | ||
}); | ||
ava_1.default('importSchema: unions', function (t) { | ||
@@ -62,0 +91,0 @@ var expectedSDL = "type A {\n b: B\n}\n\nunion B = C1 | C2\n\ntype C1 {\n c1: ID\n}\n\ntype C2 {\n c2: ID\n}\n"; |
{ | ||
"name": "graphql-import", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"license": "MIT", | ||
@@ -44,3 +44,3 @@ "repository": "git@github.com:graphcool/graphql-import.git", | ||
"@types/graphql": "0.11.7", | ||
"@types/lodash": "4.14.85", | ||
"@types/lodash": "4.14.92", | ||
"ava": "0.24.0", | ||
@@ -47,0 +47,0 @@ "ava-ts": "0.23.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
48655
535