From 752bbfdbb47a1fdd216cd88bcaff13877aa2c864 Mon Sep 17 00:00:00 2001 From: Poweranimal Date: Tue, 5 Mar 2024 15:30:08 +0100 Subject: [PATCH] added support for custom scalars --- apollo4.d.ts | 3 ++- index.d.ts | 7 ++++--- lib/query-validation-visitor.js | 2 +- lib/type-utils.js | 11 ++++++++--- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/apollo4.d.ts b/apollo4.d.ts index 1b1e8b3..5a79bcf 100644 --- a/apollo4.d.ts +++ b/apollo4.d.ts @@ -1,5 +1,6 @@ import {DocumentNode} from "graphql"; import {ApolloServerPlugin} from '@apollo/server'; +import { PluginOptions } from "."; /** * Constraint directive typeDef as a `string` @@ -16,4 +17,4 @@ export const constraintDirectiveTypeDefsGql: DocumentNode; * * @param options to setup plugin. */ -export function createApollo4QueryValidationPlugin ( options?: {} ) : ApolloServerPlugin; \ No newline at end of file +export function createApollo4QueryValidationPlugin ( options?: PluginOptions ) : ApolloServerPlugin; \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index 845a54c..1f6afa8 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,4 @@ -import {GraphQLSchema, GraphQLError, DocumentNode, ValidationContext} from "graphql"; +import {GraphQLSchema, GraphQLError, DocumentNode, ValidationContext, GraphQLScalarType} from "graphql"; import {OperationDefinitionNode, FragmentDefinitionNode, InlineFragmentNode, FieldNode, ArgumentNode} from 'graphql/language'; import {PluginDefinition} from "apollo-server-core"; @@ -40,7 +40,8 @@ interface DocumentationOptions { } interface PluginOptions { - formats: Record boolean>; + formats?: Record boolean>; + scalarTypeMappings?: Record; } /** @@ -64,7 +65,7 @@ export const constraintDirectiveTypeDefs: string * @param variables used in the query to validate * @param operationName optional name of the GraphQL operation to validate */ -export function validateQuery () : (schema: GraphQLSchema, query: DocumentNode, variables: Record, operationName?: string, pluginOptions?: {}) => Array; +export function validateQuery(schema: GraphQLSchema, query: DocumentNode, variables: Record, operationName?: string, pluginOptions?: {}): Array; /** * Create Apollo 3 plugin performing query validation. diff --git a/lib/query-validation-visitor.js b/lib/query-validation-visitor.js index 22f8e4b..a5dbf39 100644 --- a/lib/query-validation-visitor.js +++ b/lib/query-validation-visitor.js @@ -155,7 +155,7 @@ function validateScalarTypeValue (context, currentQueryField, typeDefWithDirecti const directiveArgumentMap = getDirectiveValues(constraintDirectiveTypeDefsObj, typeDefWithDirective.astNode) if (directiveArgumentMap) { - const st = getScalarType(valueTypeDef).scalarType + const st = getScalarType(valueTypeDef, options).scalarType const valueDelim = st === GraphQLString ? '"' : '' try { diff --git a/lib/type-utils.js b/lib/type-utils.js index f1053b8..b9d0cf4 100644 --- a/lib/type-utils.js +++ b/lib/type-utils.js @@ -40,15 +40,20 @@ function getConstraintValidateFn (type) { } } -function getScalarType (fieldConfig) { +function getScalarType (fieldConfig, options) { if (isScalarType(fieldConfig)) { + const scalarTypeMappings = options?.pluginOptions?.scalarTypeMappings + if (scalarTypeMappings != null) { + const scalarType = scalarTypeMappings[fieldConfig.name] + if (scalarType) return { scalarType: scalarType } + } return { scalarType: fieldConfig } } else if (isListType(fieldConfig)) { - return { ...getScalarType(fieldConfig.ofType), list: true } + return { ...getScalarType(fieldConfig.ofType, options), list: true } } else if (isNonNullType(fieldConfig) && isScalarType(fieldConfig.ofType)) { return { scalarType: fieldConfig.ofType, scalarNotNull: true } } else if (isNonNullType(fieldConfig)) { - return { ...getScalarType(fieldConfig.ofType.ofType), list: true, listNotNull: true } + return { ...getScalarType(fieldConfig.ofType.ofType, options), list: true, listNotNull: true } } else { throw new Error(`Not a valid scalar type: ${fieldConfig.toString()}`) }