graphql-toolkit
Advanced tools
Comparing version 0.0.4-4a4de6a.0 to 0.0.4-65a7985.0
import { DocumentLoader, DocumentFile } from './document-loader'; | ||
import { DocumentNode } from 'graphql'; | ||
export declare const graphQLExtensions: string[]; | ||
export declare class DocumentsFromGlob implements DocumentLoader { | ||
@@ -5,0 +4,0 @@ canHandle(doc: string): Promise<boolean> | boolean; |
@@ -45,3 +45,4 @@ "use strict"; | ||
var extract_document_string_from_code_file_1 = require("../../utils/extract-document-string-from-code-file"); | ||
exports.graphQLExtensions = ['.graphql', '.graphqls', '.gql']; | ||
var VALID_DOCUMENT_KINDS = [graphql_1.Kind.OPERATION_DEFINITION, graphql_1.Kind.FRAGMENT_DEFINITION]; | ||
var GQL_EXTENSIONS = ['.graphql', '.graphqls', '.gql']; | ||
var DocumentsFromGlob = /** @class */ (function () { | ||
@@ -70,3 +71,7 @@ function DocumentsFromGlob() { | ||
var fileExt = path_1.extname(filePath); | ||
if (exports.graphQLExtensions.includes(fileExt)) { | ||
if (!fileContent || fileContent.trim() === '') { | ||
console['warn']("Empty file found: \"" + filePath + "\", skipping..."); | ||
return null; | ||
} | ||
if (GQL_EXTENSIONS.includes(fileExt)) { | ||
return graphql_1.parse(new graphql_1.Source(fileContent, filePath)); | ||
@@ -88,3 +93,15 @@ } | ||
var _this = this; | ||
return filePaths.map(function (filePath) { return ({ filePath: filePath, content: _this.loadFileContent(filePath) }); }).filter(function (result) { return result.content; }); | ||
return filePaths.map(function (filePath) { return ({ filePath: filePath, content: _this.loadFileContent(filePath) }); }).filter(function (result) { | ||
if (!result.content) { | ||
return false; | ||
} | ||
var invalidDefinitions = result.content.definitions.filter(function (definition) { return !VALID_DOCUMENT_KINDS.includes(definition.kind); }); | ||
if (invalidDefinitions.length === 0) { | ||
return true; | ||
} | ||
else { | ||
console['warn']("File \"" + result.filePath + "\" was filtered because it contains an invalid GraphQL document definition!"); | ||
return false; | ||
} | ||
}); | ||
}; | ||
@@ -91,0 +108,0 @@ DocumentsFromGlob.prototype.handle = function (doc) { |
import { SchemaLoader } from './schema-loader'; | ||
import { DocumentNode, Source } from 'graphql'; | ||
export declare const graphQLExtensions: string[]; | ||
import { DocumentNode } from 'graphql'; | ||
export declare class SchemaFromTypedefs implements SchemaLoader { | ||
canHandle(globPath: string): boolean; | ||
handle(globPath: string): DocumentNode; | ||
loadFileContent(filePath: string): Source; | ||
} |
@@ -9,21 +9,25 @@ "use strict"; | ||
var fs_1 = require("fs"); | ||
var path_1 = require("path"); | ||
var extract_document_string_from_code_file_1 = require("../../utils/extract-document-string-from-code-file"); | ||
exports.graphQLExtensions = ['.graphql', '.graphqls', '.gql']; | ||
var GQL_EXTENSIONS = ['.graphql', '.graphqls', '.gql']; | ||
var INVALID_SCHEMA_KINDS = [graphql_1.Kind.OPERATION_DEFINITION, graphql_1.Kind.FRAGMENT_DEFINITION]; | ||
function isGraphQLFile(globPath) { | ||
return exports.graphQLExtensions.some(function (ext) { return globPath.endsWith(ext); }); | ||
return GQL_EXTENSIONS.some(function (ext) { return globPath.endsWith(ext); }); | ||
} | ||
function loadSchemaFile(filepath) { | ||
var content = fs_1.readFileSync(filepath, { | ||
encoding: 'utf-8', | ||
}); | ||
if (/^\# import /i.test(content.trimLeft())) { | ||
var importSchema = require('graphql-import').importSchema; | ||
return importSchema(filepath); | ||
var content = fs_1.readFileSync(filepath, 'utf-8'); | ||
if (content && content.trim() !== '') { | ||
if (/^\#.*import /i.test(content.trimLeft())) { | ||
var importSchema = require('graphql-import').importSchema; | ||
return importSchema(filepath); | ||
} | ||
var foundDoc = extract_document_string_from_code_file_1.extractDocumentStringFromCodeFile(new graphql_1.Source(content, filepath)); | ||
if (foundDoc) { | ||
return foundDoc; | ||
} | ||
return content; | ||
} | ||
var foundDoc = extract_document_string_from_code_file_1.extractDocumentStringFromCodeFile(new graphql_1.Source(content, filepath)); | ||
if (foundDoc) { | ||
return foundDoc; | ||
else { | ||
console['warn']("Empty schema file found: \"" + filepath + "\", skipping..."); | ||
} | ||
return content; | ||
return null; | ||
} | ||
@@ -37,3 +41,2 @@ var SchemaFromTypedefs = /** @class */ (function () { | ||
SchemaFromTypedefs.prototype.handle = function (globPath) { | ||
var _this = this; | ||
var globFiles = glob.sync(globPath, { cwd: process.cwd() }); | ||
@@ -43,21 +46,21 @@ if (!globFiles || globFiles.length === 0) { | ||
} | ||
if (globFiles.length > 1) { | ||
return epoxy_1.mergeGraphQLSchemas(globFiles.map(function (filePath) { return _this.loadFileContent(filePath); }).filter(function (f) { return f; })); | ||
var filesContent = globFiles.map(function (filePath) { return ({ filePath: filePath, content: loadSchemaFile(filePath) }); }).filter(function (file) { | ||
if (!file.content) { | ||
return false; | ||
} | ||
var node = graphql_1.parse(file.content); | ||
var invalidSchemaDefinitions = node.definitions.filter(function (def) { return INVALID_SCHEMA_KINDS.includes(def.kind); }); | ||
if (invalidSchemaDefinitions.length === 0) { | ||
return true; | ||
} | ||
else { | ||
console['warn']("File \"" + file.filePath + "\" was filtered because it contains an invalid GraphQL schema definition!"); | ||
return false; | ||
} | ||
}); | ||
if (filesContent.length === 0) { | ||
throw new Error("All found files for glob expression \"" + globPath + "\" are not valid or empty, please check it and try again!"); | ||
} | ||
else { | ||
return graphql_1.parse(loadSchemaFile(globFiles[0])); | ||
} | ||
return epoxy_1.mergeGraphQLSchemas(filesContent.map(function (f) { return f.content; })); | ||
}; | ||
SchemaFromTypedefs.prototype.loadFileContent = function (filePath) { | ||
var fileContent = fs_1.readFileSync(filePath, 'utf8'); | ||
var fileExt = path_1.extname(filePath); | ||
if (exports.graphQLExtensions.includes(fileExt)) { | ||
return new graphql_1.Source(fileContent, filePath); | ||
} | ||
var foundDoc = extract_document_string_from_code_file_1.extractDocumentStringFromCodeFile(new graphql_1.Source(fileContent, filePath)); | ||
if (foundDoc) { | ||
return new graphql_1.Source(foundDoc, filePath); | ||
} | ||
return null; | ||
}; | ||
return SchemaFromTypedefs; | ||
@@ -64,0 +67,0 @@ }()); |
@@ -5,1 +5,3 @@ export * from './extract-document-string-from-code-file'; | ||
export * from './validate-documents'; | ||
export * from './get-schema-directive-from-directive-resolver'; | ||
export * from './resolvers-composition'; |
@@ -10,2 +10,4 @@ "use strict"; | ||
__export(require("./validate-documents")); | ||
__export(require("./get-schema-directive-from-directive-resolver")); | ||
__export(require("./resolvers-composition")); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "graphql-toolkit", | ||
"version": "0.0.4-4a4de6a.0", | ||
"version": "0.0.4-65a7985.0", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -24,3 +24,2 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"semver": "5.6.0", | ||
"@types/deepmerge": "2.2.0", | ||
@@ -31,3 +30,4 @@ "@types/glob": "7.1.1", | ||
"@types/jest": "23.3.13", | ||
"@types/node": "10.12.20", | ||
"@types/lodash": "4.14.120", | ||
"@types/node": "10.12.21", | ||
"@types/request": "2.48.1", | ||
@@ -39,4 +39,5 @@ "@types/valid-url": "1.0.2", | ||
"jest": "24.0.0", | ||
"semver": "5.6.0", | ||
"ts-jest": "23.10.5", | ||
"typescript": "3.2.4" | ||
"typescript": "3.3.1" | ||
}, | ||
@@ -50,2 +51,3 @@ "dependencies": { | ||
"is-valid-path": "0.1.1", | ||
"lodash": "4.17.11", | ||
"request": "2.88.0", | ||
@@ -52,0 +54,0 @@ "valid-url": "1.0.9" |
@@ -111,1 +111,74 @@ # graphql-toolkit | ||
This method accepts `GraphQLSchema` object and a name of a GraphQL interface, and returns an array of all the GraphQL types that are implementing the GraphQL `interface`. | ||
#### `getSchemaDirectiveFromDirectiveResolver` | ||
This method accepts a name of a GraphQL Directive and its resolver function; using this method you can easily generate `SchemaDirective` with a single resolver function. | ||
#### `composeResolvers` | ||
This method accepts `IResolvers` object and mappings for composition functions that would be run before resolver itself. | ||
Instead of doing this, | ||
```js | ||
const resolvers ={ | ||
Query: { | ||
myQuery: (root, args, context) => { | ||
// Make sure that the user is authenticated | ||
if (!context.currentUser) { | ||
throw new Error('You are not authenticated!'); | ||
} | ||
// Make sure that the user has the correct roles | ||
if (!context.currentUser.roles || context.currentUser.roles.includes('EDITOR')) { | ||
throw new Error('You are not authorized!'); | ||
} | ||
// Business logic | ||
if (args.something === '1') { | ||
return true; | ||
} | ||
return false; | ||
}, | ||
}, | ||
}; | ||
``` | ||
You can do; | ||
```js | ||
const resolvers ={ | ||
Query: { | ||
myQuery: (root, args, context) => { | ||
if (args.something === '1') { | ||
return true; | ||
} | ||
return false; | ||
}, | ||
}, | ||
}; | ||
const isAuthenticated = () => next => async (root, args, context, info) => { | ||
if (!context.currentUser) { | ||
throw new Error('You are not authenticated!'); | ||
} | ||
return next(root, args, context, info); | ||
}; | ||
const hasRole = (role: string) => next => async (root, args, context, info) => { | ||
if (!context.currentUser.roles || context.currentUser.roles.includes(role)) { | ||
throw new Error('You are not authorized!'); | ||
} | ||
return next(root, args, context, info); | ||
}; | ||
const resolversComposition = { | ||
'Query.myQuery': [isAuthenticated(), hasRole('EDITOR')], | ||
}; | ||
const composedResolvers = composeResolvers(resolvers, resolversComposition); | ||
``` |
Sorry, the diff of this file is not supported yet
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
140054
115
1819
183
10
16
+ Addedlodash@4.17.11
+ Addedlodash@4.17.11(transitive)
- Removedlodash@4.17.21(transitive)