-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added UUID (Universally unique identifier) (#518)
* Added UUID (Universally unique identifier). Now GUID is an alias of UUID. * Readme update * Updated test to match UUID * GUID in UUID test * Bug fix * Bug fix, renamed scalar GUID from UUID to GUID Co-authored-by: Carlo Corradini <c.corradini@ad.enginsoft.com>
- Loading branch information
1 parent
d0c7961
commit 3b02bc7
Showing
9 changed files
with
199 additions
and
110 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,10 @@ | ||
import { Kind, GraphQLError, GraphQLScalarType } from 'graphql'; | ||
import { GraphQLScalarType } from 'graphql'; | ||
import { GraphQLUUIDConfig } from './UUID'; | ||
|
||
const validate = (value: any) => { | ||
const GUID_REGEX = /^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$/gi; | ||
|
||
if (typeof value !== 'string') { | ||
throw new TypeError(`Value is not string: ${value}`); | ||
} | ||
|
||
if (value.startsWith('{')) { | ||
value = value.substring(1, value.length - 1); | ||
} | ||
|
||
if (!GUID_REGEX.test(value)) { | ||
throw new TypeError(`Value is not a valid GUID: ${value}`); | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
export const GraphQLGUID = /*#__PURE__*/ new GraphQLScalarType({ | ||
name: `GUID`, | ||
|
||
description: `A field whose value is a generic Globally Unique Identifier: https://en.wikipedia.org/wiki/Universally_unique_identifier.`, | ||
|
||
serialize(value) { | ||
return validate(value); | ||
}, | ||
|
||
parseValue(value) { | ||
return validate(value); | ||
}, | ||
|
||
parseLiteral(ast) { | ||
if (ast.kind !== Kind.STRING) { | ||
throw new GraphQLError( | ||
`Can only validate strings as GUIDs but got a: ${ast.kind}`, | ||
); | ||
} | ||
|
||
return validate(ast.value); | ||
}, | ||
const GraphQLGUIDConfig = /*#__PURE__*/ Object.assign({}, GraphQLUUIDConfig, { | ||
name: 'GUID', | ||
}); | ||
|
||
export const GraphQLGUID = /*#__PURE__*/ new GraphQLScalarType( | ||
GraphQLGUIDConfig, | ||
); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Kind, GraphQLError, GraphQLScalarType } from 'graphql'; | ||
|
||
const validate = (value: any) => { | ||
const UUID_REGEX = /^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$/gi; | ||
|
||
if (typeof value !== 'string') { | ||
throw new TypeError(`Value is not string: ${value}`); | ||
} | ||
|
||
if (value.startsWith('{')) { | ||
value = value.substring(1, value.length - 1); | ||
} | ||
|
||
if (!UUID_REGEX.test(value)) { | ||
throw new TypeError(`Value is not a valid UUID: ${value}`); | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
export const GraphQLUUIDConfig = /*#__PURE__*/ new GraphQLScalarType({ | ||
name: `UUID`, | ||
|
||
description: `A field whose value is a generic Universally Unique Identifier: https://en.wikipedia.org/wiki/Universally_unique_identifier.`, | ||
|
||
serialize(value) { | ||
return validate(value); | ||
}, | ||
|
||
parseValue(value) { | ||
return validate(value); | ||
}, | ||
|
||
parseLiteral(ast) { | ||
if (ast.kind !== Kind.STRING) { | ||
throw new GraphQLError( | ||
`Can only validate strings as UUIDs but got a: ${ast.kind}`, | ||
); | ||
} | ||
|
||
return validate(ast.value); | ||
}, | ||
}); | ||
|
||
export const GraphQLUUID = /*#__PURE__*/ new GraphQLScalarType( | ||
GraphQLUUIDConfig, | ||
); |
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
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.