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

adding support for the id scalar constrained as a string #205

Merged
merged 4 commits into from
Jan 10, 2024
Merged
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
7 changes: 4 additions & 3 deletions lib/type-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ const {
GraphQLString,
isNonNullType,
isScalarType,
isListType
isListType,
GraphQLID
} = require('graphql')
const { ConstraintStringType, validate: validateStringFn } = require('../scalars/string')
const { ConstraintNumberType, validate: validateNumberFn } = require('../scalars/number')

function getConstraintTypeObject (fieldName, type, uniqueTypeName, directiveArgumentMap) {
if (type === GraphQLString) {
if (type === GraphQLString || type === GraphQLID) {
return new ConstraintStringType(
fieldName,
uniqueTypeName,
Expand All @@ -30,7 +31,7 @@ function getConstraintTypeObject (fieldName, type, uniqueTypeName, directiveArgu
}

function getConstraintValidateFn (type) {
if (type === GraphQLString) {
if (type === GraphQLString || type === GraphQLID) {
return validateStringFn
} else if (type === GraphQLFloat || type === GraphQLInt) {
return validateNumberFn
Expand Down
64 changes: 64 additions & 0 deletions test/id.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { deepStrictEqual, strictEqual } = require('assert')
const { valueByImplType, isStatusCodeError } = require('./testutils')

module.exports.test = function (setup, implType) {
describe('@constraint ID in INPUT_FIELD_DEFINITION treated like String', function () {
const query = `mutation createBook($input: BookInput) {
createBook(input: $input) {
title
}
}`

describe('#minLength', function () {
before(async function () {
this.typeDefs = `
type Query {
books: [Book]
}
type Book {
title: ID
}
type Mutation {
createBook(input: BookInput): Book
}
input BookInput {
title: ID! @constraint(minLength: 3)
}`

this.request = await setup({ typeDefs: this.typeDefs })
})

it('should pass', async function () {
const { body, statusCode } = await this.request
.post('/graphql')
.set('Accept', 'application/json')
.send({ query, variables: { input: { title: 'he💩' } } })

strictEqual(statusCode, 200)
deepStrictEqual(body, { data: { createBook: null } })
})

it('should fail', async function () {
const { body, statusCode } = await this.request
.post('/graphql')
.set('Accept', 'application/json')
.send({ query, variables: { input: { title: 'a💩' } } })

isStatusCodeError(statusCode, implType)
strictEqual(body.errors[0].message,
'Variable "$input" got invalid value "a💩" at "input.title"' + valueByImplType(implType, '; Expected type "title_String_NotNull_minLength_3"') + '. Must be at least 3 characters in length')
})

it('should fail with empty id', async function () {
const { body, statusCode } = await this.request
.post('/graphql')
.set('Accept', 'application/json')
.send({ query, variables: { input: { title: '' } } })

isStatusCodeError(statusCode, implType)
strictEqual(body.errors[0].message,
'Variable "$input" got invalid value "" at "input.title"' + valueByImplType(implType, '; Expected type "title_String_NotNull_minLength_3"') + '. Must be at least 3 characters in length')
})
})
})
}