@graphql-toolkit/common
Advanced tools
Comparing version 0.9.13-alpha-7bda454.3 to 0.9.13-alpha-7c7da6d.9
import { GraphQLSchema, DocumentNode } from 'graphql'; | ||
import { IResolvers } from './types'; | ||
import { IResolvers } from 'graphql-tools-fork'; | ||
export interface ExtractResolversFromSchemaOptions { | ||
@@ -4,0 +4,0 @@ selectedTypeDefs?: DocumentNode; |
import { GraphQLSchema, BuildSchemaOptions } from 'graphql'; | ||
import { Options } from 'graphql/utilities/schemaPrinter'; | ||
export declare function fixSchemaAst(schema: GraphQLSchema, options: BuildSchemaOptions & Options): GraphQLSchema; | ||
import { SchemaPrintOptions } from './types'; | ||
export declare function fixSchemaAst(schema: GraphQLSchema, options: BuildSchemaOptions & SchemaPrintOptions): GraphQLSchema; |
@@ -1,1 +0,2 @@ | ||
export declare function getSchemaDirectiveFromDirectiveResolver<TSource, TContext, TArgs>(): void; | ||
import { DirectiveResolverFn, SchemaDirectiveVisitor } from 'graphql-tools-fork'; | ||
export declare function getSchemaDirectiveFromDirectiveResolver<TSource, TContext, TArgs>(directiveResolver: DirectiveResolverFn<TSource, TContext>): typeof SchemaDirectiveVisitor; |
108
index.cjs.js
@@ -27,2 +27,3 @@ 'use strict'; | ||
const graphql = require('graphql'); | ||
const graphqlToolsFork = require('graphql-tools-fork'); | ||
const AggregateError = _interopDefault(require('aggregate-error')); | ||
@@ -297,18 +298,14 @@ const lodash = require('lodash'); | ||
// import { SchemaDirectiveVisitor } from 'graphql-tools'; | ||
// import { GraphQLField, defaultFieldResolver } from 'graphql'; | ||
// import { DirectiveResolverFn } from './types'; | ||
function getSchemaDirectiveFromDirectiveResolver() { | ||
// directiveResolver: DirectiveResolverFn<TSource, TContext> | ||
// return class extends SchemaDirectiveVisitor { | ||
// public visitFieldDefinition(field: GraphQLField<TSource, TContext, TArgs>) { | ||
// const resolver = directiveResolver; | ||
// const originalResolver = field.resolve || defaultFieldResolver; | ||
// const directiveArgs = this.args; | ||
// field.resolve = (...args: any[]) => { | ||
// const [source /* original args */, , context, info] = args; | ||
// return resolver(async () => originalResolver.apply(field, args), source, directiveArgs, context, info); | ||
// }; | ||
// } | ||
// } as typeof SchemaDirectiveVisitor; | ||
function getSchemaDirectiveFromDirectiveResolver(directiveResolver) { | ||
return class extends graphqlToolsFork.SchemaDirectiveVisitor { | ||
visitFieldDefinition(field) { | ||
const resolver = directiveResolver; | ||
const originalResolver = field.resolve || graphql.defaultFieldResolver; | ||
const directiveArgs = this.args; | ||
field.resolve = (...args) => { | ||
const [source /* original args */, , context, info] = args; | ||
return resolver(async () => originalResolver.apply(field, args), source, directiveArgs, context, info); | ||
}; | ||
} | ||
}; | ||
} | ||
@@ -340,2 +337,3 @@ | ||
function printSchemaWithDirectives(schema, options = {}) { | ||
var _a; | ||
const typesMap = schema.getTypeMap(); | ||
@@ -356,9 +354,4 @@ const result = [ | ||
} | ||
if (type.astNode) { | ||
result.push(graphql.print(type.extensionASTNodes ? extendDefinition(type) : type.astNode)); | ||
} | ||
else { | ||
// KAMIL: we might want to turn on descriptions in future | ||
result.push(graphql.printType(correctType(typeName, typesMap), { commentDescriptions: options.commentDescriptions })); | ||
} | ||
// KAMIL: we might want to turn on descriptions in future | ||
result.push(graphql.print((_a = correctType(typeName, typesMap)) === null || _a === void 0 ? void 0 : _a.astNode)); | ||
} | ||
@@ -392,2 +385,18 @@ const directives = schema.getDirectives(); | ||
type.name = typeName.toString(); | ||
if (type.astNode && type.extensionASTNodes) { | ||
type.astNode = type.extensionASTNodes ? extendDefinition(type) : type.astNode; | ||
} | ||
const doc = graphql.parse(graphql.printType(type)); | ||
const fixedAstNode = doc.definitions[0]; | ||
const originalAstNode = type === null || type === void 0 ? void 0 : type.astNode; | ||
if (originalAstNode) { | ||
fixedAstNode.directives = originalAstNode === null || originalAstNode === void 0 ? void 0 : originalAstNode.directives; | ||
if ('fields' in fixedAstNode && 'fields' in originalAstNode) { | ||
for (const fieldDefinitionNode of fixedAstNode.fields) { | ||
const originalFieldDefinitionNode = originalAstNode.fields.find((field) => field.name.value === fieldDefinitionNode.name.value); | ||
fieldDefinitionNode.directives = originalFieldDefinitionNode === null || originalFieldDefinitionNode === void 0 ? void 0 : originalFieldDefinitionNode.directives; | ||
} | ||
} | ||
} | ||
type.astNode = fixedAstNode; | ||
return type; | ||
@@ -400,3 +409,3 @@ } | ||
const allFragments = []; | ||
documentFiles.forEach(documentFile => { | ||
documentFiles.forEach((documentFile) => { | ||
if (documentFile.document) { | ||
@@ -414,6 +423,7 @@ for (const definitionNode of documentFile.document.definitions) { | ||
kind: graphql.Kind.DOCUMENT, | ||
definitions: [...allFragments, ...documentFile.document.definitions].filter((d, index, arr) => { | ||
if (d.kind === graphql.Kind.FRAGMENT_DEFINITION) { | ||
const foundIndex = arr.findIndex(i => i.kind === graphql.Kind.FRAGMENT_DEFINITION && i.name.value === d.name.value); | ||
if (foundIndex !== index) { | ||
definitions: [...allFragments, ...documentFile.document.definitions].filter((definition, index, list) => { | ||
if (definition.kind === graphql.Kind.FRAGMENT_DEFINITION) { | ||
const firstIndex = list.findIndex((def) => def.kind === graphql.Kind.FRAGMENT_DEFINITION && def.name.value === definition.name.value); | ||
const isDuplicated = firstIndex !== index; | ||
if (isDuplicated) { | ||
return false; | ||
@@ -425,3 +435,4 @@ } | ||
}; | ||
const errors = graphql.validate(schema, documentToValidate, effectiveRules); | ||
let errors = graphql.validate(schema, documentToValidate, effectiveRules); | ||
skipUnusedFragmentsInNonOperations(documentToValidate, errors); | ||
if (errors.length > 0) { | ||
@@ -445,3 +456,3 @@ allErrors.push({ | ||
error.stack = error.message; | ||
graphQLError.locations.forEach(location => (error.stack += `\n at ${loadDocumentError.filePath}:${location.line}:${location.column}`)); | ||
graphQLError.locations.forEach((location) => (error.stack += `\n at ${loadDocumentError.filePath}:${location.line}:${location.column}`)); | ||
errors.push(error); | ||
@@ -453,2 +464,18 @@ } | ||
} | ||
/** | ||
* GraphQL 15.0.0 throws an error ("unused fragment") when DocumentNode includes only a FragmentDefinitionNode | ||
* In previous versions, it was valid. | ||
* That's why we need to filter out "unused fragment" errors when a document has only fragments | ||
* | ||
* @deprecated | ||
*/ | ||
function skipUnusedFragmentsInNonOperations(doc, errors) { | ||
if (!errors.length) { | ||
return; | ||
} | ||
const isFragmentOnly = doc.definitions.some((def) => def.kind === 'OperationDefinition') === false; | ||
if (isFragmentOnly) { | ||
errors = errors.filter((error) => error.message.includes('is never used') === false); | ||
} | ||
} | ||
@@ -997,22 +1024,2 @@ function resolveRelevantMappings(resolvers, path, allMappings) { | ||
let version; | ||
if (graphql.versionInfo != null && graphql.versionInfo.major >= 15) { | ||
version = 15; | ||
} | ||
else if (graphql.getOperationRootType != null) { | ||
version = 14; | ||
} | ||
else if (graphql.lexicographicSortSchema != null) { | ||
version = 13; | ||
} | ||
else if (graphql.printError != null) { | ||
version = 12; | ||
} | ||
else { | ||
version = 11; | ||
} | ||
function getGraphQLVersion() { | ||
return version; | ||
} | ||
exports.asArray = asArray; | ||
@@ -1034,3 +1041,2 @@ exports.buildOperationNodeForField = buildOperationNodeForField; | ||
exports.getFieldsWithDirectives = getFieldsWithDirectives; | ||
exports.getGraphQLVersion = getGraphQLVersion; | ||
exports.getImplementingTypes = getImplementingTypes; | ||
@@ -1037,0 +1043,0 @@ exports.getSchemaDirectiveFromDirectiveResolver = getSchemaDirectiveFromDirectiveResolver; |
@@ -23,2 +23,1 @@ export * from './loaders'; | ||
export * from './types'; | ||
export * from './graphql-version'; |
111
index.esm.js
@@ -1,2 +0,3 @@ | ||
import { parse, buildASTSchema, isScalarType, isObjectType, isInterfaceType, isEnumType, isUnionType, getDirectiveValues, Kind, isSpecifiedScalarType, isIntrospectionType, print, printType, specifiedRules, validate, buildSchema, buildClientSchema, Source, isListType, isNonNullType, getNamedType, versionInfo, getOperationRootType, lexicographicSortSchema, printError } from 'graphql'; | ||
import { parse, buildASTSchema, isScalarType, isObjectType, isInterfaceType, isEnumType, isUnionType, getDirectiveValues, Kind, defaultFieldResolver, isSpecifiedScalarType, isIntrospectionType, print, printType, specifiedRules, validate, buildSchema, buildClientSchema, Source, isListType, isNonNullType, getNamedType } from 'graphql'; | ||
import { SchemaDirectiveVisitor } from 'graphql-tools-fork'; | ||
import AggregateError from 'aggregate-error'; | ||
@@ -271,18 +272,14 @@ import { get, set } from 'lodash'; | ||
// import { SchemaDirectiveVisitor } from 'graphql-tools'; | ||
// import { GraphQLField, defaultFieldResolver } from 'graphql'; | ||
// import { DirectiveResolverFn } from './types'; | ||
function getSchemaDirectiveFromDirectiveResolver() { | ||
// directiveResolver: DirectiveResolverFn<TSource, TContext> | ||
// return class extends SchemaDirectiveVisitor { | ||
// public visitFieldDefinition(field: GraphQLField<TSource, TContext, TArgs>) { | ||
// const resolver = directiveResolver; | ||
// const originalResolver = field.resolve || defaultFieldResolver; | ||
// const directiveArgs = this.args; | ||
// field.resolve = (...args: any[]) => { | ||
// const [source /* original args */, , context, info] = args; | ||
// return resolver(async () => originalResolver.apply(field, args), source, directiveArgs, context, info); | ||
// }; | ||
// } | ||
// } as typeof SchemaDirectiveVisitor; | ||
function getSchemaDirectiveFromDirectiveResolver(directiveResolver) { | ||
return class extends SchemaDirectiveVisitor { | ||
visitFieldDefinition(field) { | ||
const resolver = directiveResolver; | ||
const originalResolver = field.resolve || defaultFieldResolver; | ||
const directiveArgs = this.args; | ||
field.resolve = (...args) => { | ||
const [source /* original args */, , context, info] = args; | ||
return resolver(async () => originalResolver.apply(field, args), source, directiveArgs, context, info); | ||
}; | ||
} | ||
}; | ||
} | ||
@@ -314,2 +311,3 @@ | ||
function printSchemaWithDirectives(schema, options = {}) { | ||
var _a; | ||
const typesMap = schema.getTypeMap(); | ||
@@ -330,9 +328,4 @@ const result = [ | ||
} | ||
if (type.astNode) { | ||
result.push(print(type.extensionASTNodes ? extendDefinition(type) : type.astNode)); | ||
} | ||
else { | ||
// KAMIL: we might want to turn on descriptions in future | ||
result.push(printType(correctType(typeName, typesMap), { commentDescriptions: options.commentDescriptions })); | ||
} | ||
// KAMIL: we might want to turn on descriptions in future | ||
result.push(print((_a = correctType(typeName, typesMap)) === null || _a === void 0 ? void 0 : _a.astNode)); | ||
} | ||
@@ -366,2 +359,18 @@ const directives = schema.getDirectives(); | ||
type.name = typeName.toString(); | ||
if (type.astNode && type.extensionASTNodes) { | ||
type.astNode = type.extensionASTNodes ? extendDefinition(type) : type.astNode; | ||
} | ||
const doc = parse(printType(type)); | ||
const fixedAstNode = doc.definitions[0]; | ||
const originalAstNode = type === null || type === void 0 ? void 0 : type.astNode; | ||
if (originalAstNode) { | ||
fixedAstNode.directives = originalAstNode === null || originalAstNode === void 0 ? void 0 : originalAstNode.directives; | ||
if ('fields' in fixedAstNode && 'fields' in originalAstNode) { | ||
for (const fieldDefinitionNode of fixedAstNode.fields) { | ||
const originalFieldDefinitionNode = originalAstNode.fields.find((field) => field.name.value === fieldDefinitionNode.name.value); | ||
fieldDefinitionNode.directives = originalFieldDefinitionNode === null || originalFieldDefinitionNode === void 0 ? void 0 : originalFieldDefinitionNode.directives; | ||
} | ||
} | ||
} | ||
type.astNode = fixedAstNode; | ||
return type; | ||
@@ -374,3 +383,3 @@ } | ||
const allFragments = []; | ||
documentFiles.forEach(documentFile => { | ||
documentFiles.forEach((documentFile) => { | ||
if (documentFile.document) { | ||
@@ -388,6 +397,7 @@ for (const definitionNode of documentFile.document.definitions) { | ||
kind: Kind.DOCUMENT, | ||
definitions: [...allFragments, ...documentFile.document.definitions].filter((d, index, arr) => { | ||
if (d.kind === Kind.FRAGMENT_DEFINITION) { | ||
const foundIndex = arr.findIndex(i => i.kind === Kind.FRAGMENT_DEFINITION && i.name.value === d.name.value); | ||
if (foundIndex !== index) { | ||
definitions: [...allFragments, ...documentFile.document.definitions].filter((definition, index, list) => { | ||
if (definition.kind === Kind.FRAGMENT_DEFINITION) { | ||
const firstIndex = list.findIndex((def) => def.kind === Kind.FRAGMENT_DEFINITION && def.name.value === definition.name.value); | ||
const isDuplicated = firstIndex !== index; | ||
if (isDuplicated) { | ||
return false; | ||
@@ -399,3 +409,4 @@ } | ||
}; | ||
const errors = validate(schema, documentToValidate, effectiveRules); | ||
let errors = validate(schema, documentToValidate, effectiveRules); | ||
skipUnusedFragmentsInNonOperations(documentToValidate, errors); | ||
if (errors.length > 0) { | ||
@@ -419,3 +430,3 @@ allErrors.push({ | ||
error.stack = error.message; | ||
graphQLError.locations.forEach(location => (error.stack += `\n at ${loadDocumentError.filePath}:${location.line}:${location.column}`)); | ||
graphQLError.locations.forEach((location) => (error.stack += `\n at ${loadDocumentError.filePath}:${location.line}:${location.column}`)); | ||
errors.push(error); | ||
@@ -427,2 +438,18 @@ } | ||
} | ||
/** | ||
* GraphQL 15.0.0 throws an error ("unused fragment") when DocumentNode includes only a FragmentDefinitionNode | ||
* In previous versions, it was valid. | ||
* That's why we need to filter out "unused fragment" errors when a document has only fragments | ||
* | ||
* @deprecated | ||
*/ | ||
function skipUnusedFragmentsInNonOperations(doc, errors) { | ||
if (!errors.length) { | ||
return; | ||
} | ||
const isFragmentOnly = doc.definitions.some((def) => def.kind === 'OperationDefinition') === false; | ||
if (isFragmentOnly) { | ||
errors = errors.filter((error) => error.message.includes('is never used') === false); | ||
} | ||
} | ||
@@ -971,22 +998,2 @@ function resolveRelevantMappings(resolvers, path, allMappings) { | ||
let version; | ||
if (versionInfo != null && versionInfo.major >= 15) { | ||
version = 15; | ||
} | ||
else if (getOperationRootType != null) { | ||
version = 14; | ||
} | ||
else if (lexicographicSortSchema != null) { | ||
version = 13; | ||
} | ||
else if (printError != null) { | ||
version = 12; | ||
} | ||
else { | ||
version = 11; | ||
} | ||
function getGraphQLVersion() { | ||
return version; | ||
} | ||
export { asArray, buildOperationNodeForField, chainFunctions, checkValidationErrors, compareNodes, compareStrings, composeResolvers, createSchemaDefinition, debugLog, extractFieldResolversFromObjectType, extractResolversFromSchema, fixSchemaAst, fixWindowsPath, flattenArray, getDirectives, getFieldsWithDirectives, getGraphQLVersion, getImplementingTypes, getSchemaDirectiveFromDirectiveResolver, getUserTypesFromSchema, isDocumentString, isEqual, isNotEqual, isValidPath, nodeToString, parseGraphQLJSON, parseGraphQLSDL, printSchemaWithDirectives, resolveBuiltinModule, resolveBuiltinModuleSync, validateGraphQlDocuments }; | ||
export { asArray, buildOperationNodeForField, chainFunctions, checkValidationErrors, compareNodes, compareStrings, composeResolvers, createSchemaDefinition, debugLog, extractFieldResolversFromObjectType, extractResolversFromSchema, fixSchemaAst, fixWindowsPath, flattenArray, getDirectives, getFieldsWithDirectives, getImplementingTypes, getSchemaDirectiveFromDirectiveResolver, getUserTypesFromSchema, isDocumentString, isEqual, isNotEqual, isValidPath, nodeToString, parseGraphQLJSON, parseGraphQLSDL, printSchemaWithDirectives, resolveBuiltinModule, resolveBuiltinModuleSync, validateGraphQlDocuments }; |
{ | ||
"name": "@graphql-toolkit/common", | ||
"version": "0.9.13-alpha-7bda454.3+7bda454", | ||
"version": "0.9.13-alpha-7c7da6d.9+7c7da6d", | ||
"description": "Common package containting utils and types for GraphQL Toolkit", | ||
@@ -20,2 +20,3 @@ "peerDependencies": { | ||
"camel-case": "4.1.1", | ||
"graphql-tools-fork": "9.0.1", | ||
"lodash": "4.17.15" | ||
@@ -22,0 +23,0 @@ }, |
import { ParseOptions } from 'graphql'; | ||
import { Source } from '.'; | ||
import { GraphQLSchemaValidationOptions } from 'graphql/type/schema'; | ||
import { Options } from 'graphql/utilities/schemaPrinter'; | ||
export declare function parseGraphQLJSON(location: string, jsonContent: string, options: Options & ParseOptions & GraphQLSchemaValidationOptions): Source; | ||
import { Source } from './loaders'; | ||
import { SchemaPrintOptions } from './types'; | ||
export declare function parseGraphQLJSON(location: string, jsonContent: string, options: SchemaPrintOptions & ParseOptions & GraphQLSchemaValidationOptions): Source; |
import { GraphQLSchema } from 'graphql'; | ||
import { Options } from 'graphql/utilities/schemaPrinter'; | ||
export declare function printSchemaWithDirectives(schema: GraphQLSchema, options?: Options): string; | ||
import { SchemaPrintOptions } from './types'; | ||
export declare function printSchemaWithDirectives(schema: GraphQLSchema, options?: SchemaPrintOptions): string; |
@@ -1,2 +0,2 @@ | ||
import { IResolvers, IFieldResolver } from './types'; | ||
import { IResolvers, IFieldResolver } from 'graphql-tools-fork'; | ||
export declare type ResolversComposition<Resolver extends IFieldResolver<any, any> = IFieldResolver<any, any>> = (next: Resolver) => Resolver; | ||
@@ -3,0 +3,0 @@ export declare type ResolversComposerMapping<Resolvers extends IResolvers = IResolvers> = { |
@@ -1,25 +0,11 @@ | ||
import { GraphQLResolveInfo, GraphQLIsTypeOfFn, GraphQLTypeResolver, GraphQLScalarType } from 'graphql'; | ||
export interface IResolvers<TSource = any, TContext = any> { | ||
[key: string]: (() => any) | IResolverObject<TSource, TContext> | IResolverOptions<TSource, TContext> | GraphQLScalarType | IEnumResolver; | ||
export interface SchemaPrintOptions { | ||
/** | ||
* Descriptions are defined as preceding string literals, however an older | ||
* experimental version of the SDL supported preceding comments as | ||
* descriptions. Set to true to enable this deprecated behavior. | ||
* This option is provided to ease adoption and will be removed in v16. | ||
* | ||
* Default: false | ||
*/ | ||
commentDescriptions?: boolean; | ||
} | ||
export interface IResolverObject<TSource = any, TContext = any, TArgs = any> { | ||
[key: string]: IFieldResolver<TSource, TContext, TArgs> | IResolverOptions<TSource, TContext> | IResolverObject<TSource, TContext>; | ||
} | ||
export declare type IFieldResolver<TSource, TContext, TArgs = Record<string, any>> = (source: TSource, args: TArgs, context: TContext, info: GraphQLResolveInfo) => any; | ||
export interface IResolverOptions<TSource = any, TContext = any, TArgs = any> { | ||
fragment?: string; | ||
resolve?: IFieldResolver<TSource, TContext, TArgs>; | ||
subscribe?: IFieldResolver<TSource, TContext, TArgs>; | ||
__resolveType?: GraphQLTypeResolver<TSource, TContext>; | ||
__isTypeOf?: GraphQLIsTypeOfFn<TSource, TContext>; | ||
} | ||
export interface IEnumResolver { | ||
[key: string]: string | number; | ||
} | ||
export declare type NextResolverFn = () => Promise<any>; | ||
export declare type DirectiveResolverFn<TSource = any, TContext = any> = (next: NextResolverFn, source: TSource, args: { | ||
[argName: string]: any; | ||
}, context: TContext, info: GraphQLResolveInfo) => any; | ||
export interface IDirectiveResolvers<TSource = any, TContext = any> { | ||
[directiveName: string]: DirectiveResolverFn<TSource, TContext>; | ||
} |
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
85267
5
26
+ Addedgraphql-tools-fork@9.0.1
+ Added@wry/equality@0.1.11(transitive)
+ Addedapollo-link@1.2.14(transitive)
+ Addedapollo-link-http-common@0.2.16(transitive)
+ Addedapollo-utilities@1.3.4(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addeddeprecated-decorator@0.1.6(transitive)
+ Addedextract-files@7.0.0(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedform-data@3.0.2(transitive)
+ Addedgraphql-tools-fork@9.0.1(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addednode-fetch@2.7.0(transitive)
+ Addedtr46@0.0.3(transitive)
+ Addedts-invariant@0.4.4(transitive)
+ Addeduuid@7.0.3(transitive)
+ Addedwebidl-conversions@3.0.1(transitive)
+ Addedwhatwg-url@5.0.0(transitive)
+ Addedzen-observable@0.8.15(transitive)
+ Addedzen-observable-ts@0.8.21(transitive)