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

Conversation

Poweranimal
Copy link

@Poweranimal Poweranimal commented Mar 5, 2024

Support for custom scalar validation.

In order to validate a custom scalar, a scalar type mapping needs to be created for one of the supported base scalar types.

E.g.:

import { ApolloServer, ApolloServerPlugin } from "@apollo/server";
import { constraintDirectiveTypeDefs, createApollo4QueryValidationPlugin } from "graphql-constraint-directive/apollo4"
import { makeExecutableSchema } from "@graphql-tools/schema";
import { EmailAddressResolver, typeDefs as scalarTypeDefs } from 'graphql-scalars';
import { GraphQLString } from "graphql";


const typeDefs = `
type Query {
  create(
    email: EmailAddress @constraint(maxLength: 40)
  ): Void
}
`

const schema = makeExecutableSchema({
    typeDefs: [constraintDirectiveTypeDefs, ...scalarTypeDefs, typeDefs],
    resolvers,
})

const plugins: ApolloServerPlugin[] = [
    createApollo4QueryValidationPlugin({
        scalarTypeMappings: {
            [EmailAddressResolver.name]: GraphQLString
        }
    })
]

const server = new ApolloServer({ 
    schema, 
    plugins,
});

Fixes: #62

@confuser
Copy link
Owner

confuser commented Mar 9, 2024

Hi @Poweranimal thanks for the PR. Can you add test cases please?

@coveralls
Copy link

Pull Request Test Coverage Report for Build 8158197605

Details

  • 5 of 7 (71.43%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.7%) to 94.192%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/type-utils.js 4 6 66.67%
Totals Coverage Status
Change from base Build 8119970561: -0.7%
Covered Lines: 360
Relevant Lines: 371

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for custom scalars
3 participants