@graphql-tools/graphql-file-loader
Advanced tools
Comparing version 5.0.1-alpha-b7ec6b7.0 to 5.0.1-alpha-b86f0f0.0
@@ -6,4 +6,12 @@ 'use strict'; | ||
const utils = require('@graphql-tools/utils'); | ||
const path = require('path'); | ||
const fsExtra = require('fs-extra'); | ||
const process = require('process'); | ||
const _import = require('@graphql-tools/import'); | ||
const FILE_EXTENSIONS = ['.gql', '.gqls', '.graphql', '.graphqls']; | ||
function isGraphQLImportFile(rawSDL) { | ||
const trimmedRawSDL = rawSDL.trim(); | ||
return trimmedRawSDL.startsWith('# import') || trimmedRawSDL.startsWith('#import'); | ||
} | ||
class GraphQLFileLoader { | ||
@@ -14,13 +22,15 @@ loaderId() { | ||
async canLoad(pointer, options) { | ||
return this.canLoadSync(pointer, options); | ||
if (utils.isValidPath(pointer)) { | ||
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) { | ||
const normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd, pointer); | ||
return new Promise(resolve => fsExtra.exists(normalizedFilePath, resolve)); | ||
} | ||
} | ||
return false; | ||
} | ||
canLoadSync(pointer, options) { | ||
if (utils.isValidPath(pointer) && options.path && options.fs) { | ||
const { resolve, isAbsolute } = options.path; | ||
if (utils.isValidPath(pointer)) { | ||
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) { | ||
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || process.cwd(), pointer); | ||
const { existsSync } = options.fs; | ||
if (existsSync(normalizedFilePath)) { | ||
return true; | ||
} | ||
const normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd, pointer); | ||
return fsExtra.existsSync(normalizedFilePath); | ||
} | ||
@@ -31,10 +41,23 @@ } | ||
async load(pointer, options) { | ||
return this.loadSync(pointer, options); | ||
const normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd, pointer); | ||
const rawSDL = await fsExtra.readFile(normalizedFilePath, { encoding: 'utf8' }); | ||
if (isGraphQLImportFile(rawSDL)) { | ||
return { | ||
location: pointer, | ||
document: _import.processImport(pointer, options.cwd), | ||
}; | ||
} | ||
return utils.parseGraphQLSDL(pointer, rawSDL.trim(), options); | ||
} | ||
loadSync(pointer, options) { | ||
const { resolve, isAbsolute } = options.path; | ||
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || process.cwd(), pointer); | ||
const { readFileSync } = options.fs; | ||
const rawSDL = readFileSync(normalizedFilePath, 'utf-8').trim(); | ||
return utils.parseGraphQLSDL(pointer, rawSDL, options); | ||
const cwd = options.cwd || process.cwd(); | ||
const normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(cwd, pointer); | ||
const rawSDL = fsExtra.readFileSync(normalizedFilePath, { encoding: 'utf8' }); | ||
if (isGraphQLImportFile(rawSDL)) { | ||
return { | ||
location: pointer, | ||
document: _import.processImport(pointer, options.cwd), | ||
}; | ||
} | ||
return utils.parseGraphQLSDL(pointer, rawSDL.trim(), options); | ||
} | ||
@@ -41,0 +64,0 @@ } |
import { Source, UniversalLoader, DocumentPointerSingle, SchemaPointerSingle, SingleFileOptions } from '@graphql-tools/utils'; | ||
export interface GraphQLFileLoaderOptions extends SingleFileOptions { | ||
fs?: typeof import('fs'); | ||
path?: typeof import('path'); | ||
} | ||
@@ -6,0 +4,0 @@ export declare class GraphQLFileLoader implements UniversalLoader<GraphQLFileLoaderOptions> { |
import { isValidPath, parseGraphQLSDL } from '@graphql-tools/utils'; | ||
import { isAbsolute, resolve } from 'path'; | ||
import { exists, existsSync, readFile, readFileSync } from 'fs-extra'; | ||
import { cwd } from 'process'; | ||
import { processImport } from '@graphql-tools/import'; | ||
const FILE_EXTENSIONS = ['.gql', '.gqls', '.graphql', '.graphqls']; | ||
function isGraphQLImportFile(rawSDL) { | ||
const trimmedRawSDL = rawSDL.trim(); | ||
return trimmedRawSDL.startsWith('# import') || trimmedRawSDL.startsWith('#import'); | ||
} | ||
class GraphQLFileLoader { | ||
@@ -9,13 +17,15 @@ loaderId() { | ||
async canLoad(pointer, options) { | ||
return this.canLoadSync(pointer, options); | ||
if (isValidPath(pointer)) { | ||
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) { | ||
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd, pointer); | ||
return new Promise(resolve => exists(normalizedFilePath, resolve)); | ||
} | ||
} | ||
return false; | ||
} | ||
canLoadSync(pointer, options) { | ||
if (isValidPath(pointer) && options.path && options.fs) { | ||
const { resolve, isAbsolute } = options.path; | ||
if (isValidPath(pointer)) { | ||
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) { | ||
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || process.cwd(), pointer); | ||
const { existsSync } = options.fs; | ||
if (existsSync(normalizedFilePath)) { | ||
return true; | ||
} | ||
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd, pointer); | ||
return existsSync(normalizedFilePath); | ||
} | ||
@@ -26,10 +36,23 @@ } | ||
async load(pointer, options) { | ||
return this.loadSync(pointer, options); | ||
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd, pointer); | ||
const rawSDL = await readFile(normalizedFilePath, { encoding: 'utf8' }); | ||
if (isGraphQLImportFile(rawSDL)) { | ||
return { | ||
location: pointer, | ||
document: processImport(pointer, options.cwd), | ||
}; | ||
} | ||
return parseGraphQLSDL(pointer, rawSDL.trim(), options); | ||
} | ||
loadSync(pointer, options) { | ||
const { resolve, isAbsolute } = options.path; | ||
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || process.cwd(), pointer); | ||
const { readFileSync } = options.fs; | ||
const rawSDL = readFileSync(normalizedFilePath, 'utf-8').trim(); | ||
return parseGraphQLSDL(pointer, rawSDL, options); | ||
const cwd$1 = options.cwd || cwd(); | ||
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(cwd$1, pointer); | ||
const rawSDL = readFileSync(normalizedFilePath, { encoding: 'utf8' }); | ||
if (isGraphQLImportFile(rawSDL)) { | ||
return { | ||
location: pointer, | ||
document: processImport(pointer, options.cwd), | ||
}; | ||
} | ||
return parseGraphQLSDL(pointer, rawSDL.trim(), options); | ||
} | ||
@@ -36,0 +59,0 @@ } |
{ | ||
"name": "@graphql-tools/graphql-file-loader", | ||
"version": "5.0.1-alpha-b7ec6b7.0", | ||
"version": "5.0.1-alpha-b86f0f0.0", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -9,3 +9,5 @@ "peerDependencies": { | ||
"dependencies": { | ||
"@graphql-tools/utils": "5.0.1-alpha-b7ec6b7.0", | ||
"@graphql-tools/import": "5.0.1-alpha-b86f0f0.0", | ||
"@graphql-tools/utils": "5.0.1-alpha-b86f0f0.0", | ||
"fs-extra": "9.0.0", | ||
"tslib": "1.11.1" | ||
@@ -22,2 +24,5 @@ }, | ||
}, | ||
"devDependencies": { | ||
"@types/fs-extra": "8.1.0" | ||
}, | ||
"publishConfig": { | ||
@@ -24,0 +29,0 @@ "access": "public" |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
16263
128
5
1
3
+ Addedfs-extra@9.0.0
+ Added@graphql-tools/import@5.0.1-alpha-b86f0f0.0(transitive)
+ Added@graphql-tools/utils@5.0.1-alpha-b86f0f0.0(transitive)
+ Addedat-least-node@1.0.0(transitive)
+ Addedfs-extra@9.0.0(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedjsonfile@6.1.0(transitive)
+ Addedresolve-from@5.0.0(transitive)
+ Addeduniversalify@1.0.02.0.1(transitive)
- Removed@graphql-tools/utils@5.0.1-alpha-b7ec6b7.0(transitive)