Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for custom scalars #228

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apollo4.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {DocumentNode} from "graphql";
import {ApolloServerPlugin} from '@apollo/server';
import { PluginOptions } from ".";

/**
* Constraint directive typeDef as a `string`
Expand All @@ -16,4 +17,4 @@ export const constraintDirectiveTypeDefsGql: DocumentNode;
*
* @param options to setup plugin.
*/
export function createApollo4QueryValidationPlugin ( options?: {} ) : ApolloServerPlugin;
export function createApollo4QueryValidationPlugin ( options?: PluginOptions ) : ApolloServerPlugin;
7 changes: 4 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -40,7 +40,8 @@ interface DocumentationOptions {
}

interface PluginOptions {
formats: Record<string, (value: unknown) => boolean>;
formats?: Record<string, (value: unknown) => boolean>;
scalarTypeMappings?: Record<string, GraphQLScalarType>;
}

/**
Expand All @@ -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<string, any>, operationName?: string, pluginOptions?: {}) => Array<GraphQLError>;
export function validateQuery(schema: GraphQLSchema, query: DocumentNode, variables: Record<string, any>, operationName?: string, pluginOptions?: {}): Array<GraphQLError>;

/**
* Create Apollo 3 plugin performing query validation.
Expand Down
2 changes: 1 addition & 1 deletion lib/query-validation-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 8 additions & 3 deletions lib/type-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`)
}
Expand Down
Loading