Last active
March 8, 2020 12:54
Apollo GraphQL Email Scalar Type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { gql } from 'apollo-server-express' | |
import { GraphQLError } from 'graphql/error' | |
import { Kind } from 'graphql/language' | |
const EMAIL_REGEX = new RegExp( | |
/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/ | |
) | |
const Email = gql` | |
"Scalar data representing email" | |
scalar Email | |
` | |
/* eslint-disable no-underscore-dangle */ | |
export const resolver = { | |
Email: { | |
__parseValue(value) { | |
if (typeof value !== 'string') { | |
throw new TypeError('Value is not string') | |
} | |
if (!EMAIL_REGEX.test(value)) { | |
throw new TypeError(`Value is not a valid email address: ${value}`) | |
} | |
return value | |
}, | |
__serialize(value) { | |
return value | |
}, | |
__parseLiteral(ast) { | |
if (ast.kind !== Kind.STRING) { | |
throw new GraphQLError( | |
`Value is not string : ${ast.kind}` | |
) | |
} | |
if (!EMAIL_REGEX.test(ast.value)) { | |
throw new GraphQLError(`Value is not a valid email address: ${ast.value}`) | |
} | |
return ast.value | |
} | |
} | |
} | |
export default () => [ | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Kind } from 'graphql/language' | |
import { resolver } from '../src/graphql/schema/types/scalar/email' | |
/* eslint-disable no-underscore-dangle */ | |
const email = 'test@test' | |
const emailComplete = 'test@test.com' | |
describe('Email scalar', () => { | |
describe('when email is valid', () => { | |
it('serialize correctly with complete email', () => { | |
expect(resolver.Email.__serialize(emailComplete)) | |
.to | |
.eql(emailComplete) | |
}) | |
it('serialize correctly', () => { | |
expect(resolver.Email.__serialize(email)) | |
.to | |
.eql(email) | |
}) | |
it('parseValue correctly with complete email', () => { | |
expect(resolver.Email.__parseValue(emailComplete)) | |
.to | |
.eql(emailComplete) | |
}) | |
it('parseValue correctly', () => { | |
expect(resolver.Email.__parseValue(email)) | |
.to | |
.eql(email) | |
}) | |
it('parseLiteral correctly with complete email', () => { | |
expect( | |
resolver.Email.__parseLiteral({ | |
value: emailComplete, | |
kind: Kind.STRING | |
}, {}) | |
) | |
.to | |
.eql(emailComplete) | |
}) | |
it('parseLiteral correctly', () => { | |
expect( | |
resolver.Email.__parseLiteral({ | |
value: email, | |
kind: Kind.STRING | |
}, {}) | |
) | |
.to | |
.eql(email) | |
}) | |
}) | |
describe('when email is invalid', () => { | |
describe('and not a string', () => { | |
it('parseValue', () => { | |
expect(() => resolver.Email.__parseValue(435)) | |
.to | |
.throw( | |
'Value is not string' | |
) | |
}) | |
it('parseLiteral', () => { | |
expect(() => resolver.Email.__parseLiteral({ | |
value: '453', | |
kind: Kind.INT | |
}, {})) | |
.to | |
.throw('Value is not string : IntValue') | |
}) | |
}) | |
describe('and not an email address', () => { | |
it('parseValue', () => { | |
expect(() => resolver.Email.__parseValue('this is a test')) | |
.to | |
.throw('Value is not a valid email address: this is a test') | |
}) | |
it('parseLiteral', () => { | |
expect(() => resolver.Email.__parseLiteral({ | |
value: 'this is a test', | |
kind: Kind.STRING | |
}, {})) | |
.to | |
.throw('Value is not a valid email address') | |
}) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment