@envelop/core
Advanced tools
Comparing version
77
index.js
@@ -302,6 +302,2 @@ 'use strict'; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
function isPromise(obj) { | ||
return obj != null && obj.then; | ||
} | ||
@@ -1095,78 +1091,16 @@ function createEnvelopOrchestrator(plugins) { | ||
}; | ||
const VALIDATE_FN = Symbol('VALIDATE_FN'); | ||
const useLazyLoadedSchema = (schemaLoader) => { | ||
let schemaSet$; | ||
return { | ||
onEnveloped({ setSchema, context }) { | ||
const schema$ = schemaLoader(context); | ||
if (isPromise(schema$)) { | ||
schemaSet$ = schema$.then(schemaObj => { | ||
setSchema(schemaObj); | ||
}); | ||
} | ||
else { | ||
setSchema(schema$); | ||
} | ||
setSchema(schemaLoader(context)); | ||
}, | ||
onValidate({ validateFn, setValidationFn, extendContext }) { | ||
if (schemaSet$) { | ||
extendContext({ | ||
[VALIDATE_FN]: validateFn, | ||
}); | ||
setValidationFn(() => []); | ||
} | ||
}, | ||
onExecute({ args: { schema, document, contextValue }, setResultAndStopExecution }) { | ||
if (schemaSet$) { | ||
const validateFn = contextValue[VALIDATE_FN]; | ||
const errors = validateFn(schema, document); | ||
if (errors === null || errors === void 0 ? void 0 : errors.length) { | ||
setResultAndStopExecution({ | ||
errors, | ||
}); | ||
} | ||
else { | ||
return schemaSet$; | ||
} | ||
} | ||
return undefined; | ||
}, | ||
}; | ||
}; | ||
const useAsyncSchema = (schema$) => { | ||
let schemaSet$; | ||
const useAsyncSchema = (schemaPromise) => { | ||
return { | ||
onPluginInit({ setSchema }) { | ||
if (isPromise(schema$)) { | ||
schemaSet$ = schema$.then(schemaObj => { | ||
setSchema(schemaObj); | ||
}); | ||
} | ||
else { | ||
setSchema(schema$); | ||
} | ||
schemaPromise.then(schemaObj => { | ||
setSchema(schemaObj); | ||
}); | ||
}, | ||
onValidate({ validateFn, setValidationFn, extendContext }) { | ||
if (schemaSet$) { | ||
extendContext({ | ||
[VALIDATE_FN]: validateFn, | ||
}); | ||
setValidationFn(() => []); | ||
} | ||
}, | ||
onExecute({ args: { schema, document, contextValue }, setResultAndStopExecution }) { | ||
if (schemaSet$) { | ||
const validateFn = contextValue[VALIDATE_FN]; | ||
const errors = validateFn(schema, document); | ||
if (errors === null || errors === void 0 ? void 0 : errors.length) { | ||
setResultAndStopExecution({ | ||
errors, | ||
}); | ||
} | ||
else { | ||
return schemaSet$; | ||
} | ||
} | ||
return undefined; | ||
}, | ||
}; | ||
@@ -1367,3 +1301,2 @@ }; | ||
exports.isPluginEnabled = isPluginEnabled; | ||
exports.isPromise = isPromise; | ||
exports.makeExecute = makeExecute; | ||
@@ -1370,0 +1303,0 @@ exports.makeSubscribe = makeSubscribe; |
{ | ||
"name": "@envelop/core", | ||
"version": "2.3.2-alpha-3ace273.0", | ||
"version": "2.3.2-alpha-763ffe6.0", | ||
"sideEffects": false, | ||
@@ -5,0 +5,0 @@ "peerDependencies": { |
import { Plugin } from '@envelop/types'; | ||
import { GraphQLError } from 'graphql'; | ||
import { GraphQLError, GraphQLErrorExtensions } from 'graphql'; | ||
export declare const DEFAULT_ERROR_MESSAGE = "Unexpected error."; | ||
export declare class EnvelopError extends GraphQLError { | ||
constructor(message: string, extensions?: Record<string, any>); | ||
constructor(message: string, extensions?: GraphQLErrorExtensions); | ||
} | ||
@@ -7,0 +7,0 @@ export declare type FormatErrorHandler = (error: GraphQLError | unknown, message: string, isDev: boolean) => GraphQLError; |
import { GraphQLSchema } from 'graphql'; | ||
import { DefaultContext, Maybe, Plugin, PromiseOrValue } from '@envelop/types'; | ||
import { DefaultContext, Maybe, Plugin } from '@envelop/types'; | ||
export declare const useSchema: (schema: GraphQLSchema) => Plugin; | ||
export declare const useLazyLoadedSchema: (schemaLoader: (context: Maybe<DefaultContext>) => PromiseOrValue<GraphQLSchema>) => Plugin; | ||
export declare const useAsyncSchema: (schema$: PromiseOrValue<GraphQLSchema>) => Plugin; | ||
export declare const useLazyLoadedSchema: (schemaLoader: (context: Maybe<DefaultContext>) => GraphQLSchema) => Plugin; | ||
export declare const useAsyncSchema: (schemaPromise: Promise<GraphQLSchema>) => Plugin; |
@@ -34,2 +34,1 @@ import { ASTNode, DocumentNode, OperationDefinitionNode, Source, ExecutionResult, SubscriptionArgs, ExecutionArgs } from 'graphql'; | ||
export declare function errorAsyncIterator<TInput>(source: AsyncIterable<TInput>, onError: (err: unknown) => void): AsyncGenerator<TInput>; | ||
export declare function isPromise<T>(obj: any): obj is Promise<T>; |
Sorry, the diff of this file is not supported yet
110344
-4.12%2738
-4.67%