Skip to content

Commit

Permalink
Added UUID (Universally unique identifier) (#518)
Browse files Browse the repository at this point in the history
* 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
carlocorradini and Carlo Corradini authored Oct 6, 2020
1 parent d0c7961 commit 3b02bc7
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 110 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ scalar Long

scalar SafeInt

scalar UUID

scalar GUID

scalar HexColorCode
Expand Down Expand Up @@ -136,6 +138,7 @@ import {
BigIntResolver,
LongResolver,
SafeIntResolver,
UUIDResolver,
GUIDResolver,
HexColorCodeResolver,
HSLResolver,
Expand Down Expand Up @@ -188,6 +191,7 @@ const myResolverMap = {
PostalCode: PostalCodeResolver,
NonEmptyString: NonEmptyStringResolver,

UUID: UUIDResolver,
GUID: GUIDResolver,

HexColorCode: HexColorCodeResolver,
Expand Down Expand Up @@ -221,6 +225,7 @@ const myResolverMap = {

NOTE: `NonNegativeFloat` and `NonNegativeInt` are also available under the aliases `UnsignedFloat`
and `UnsignedInt`, respectively.
NOTE: `UUID` are also available under the alias `GUID`.

Alternatively, use the default import and ES6's spread operator syntax:

Expand Down Expand Up @@ -644,9 +649,9 @@ In order to support `BigInt` in `JSON.parse` and `JSON.stringify`, it is recomme

This scalar behaves just like the native GraphQLInt scalar, but it allows integers that require more than 32-bits. Any integer that is considered "safe" in JavaScript (i.e. ± 9,007,199,254,740,991) is considered a valid value.

### GUID
### UUID

A field whose value is a generic [Globally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier).
A field whose value is a generic [Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier).

### Hexadecimal

Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
GraphQLByte,
GraphQLLong,
GraphQLSafeInt,
GraphQLUUID,
GraphQLGUID,
GraphQLHexadecimal,
GraphQLHexColorCode,
Expand Down Expand Up @@ -72,6 +73,7 @@ export {
Byte as ByteTypeDefinition,
Long as LongTypeDefinition,
SafeInt as SafeIntDefinition,
UUID as UUIDDefinition,
GUID as GUIDDefinition,
Hexadecimal as HexadecimalTypeDefinition,
HexColorCode as HexColorCodeDefinition,
Expand Down Expand Up @@ -121,6 +123,7 @@ export {
GraphQLByte as ByteResolver,
GraphQLLong as LongResolver,
GraphQLSafeInt as SafeIntResolver,
GraphQLUUID as UUIDResolver,
GraphQLGUID as GUIDResolver,
GraphQLHexadecimal as HexadecimalResolver,
GraphQLHexColorCode as HexColorCodeResolver,
Expand Down Expand Up @@ -168,6 +171,7 @@ export const resolvers = {
Byte: GraphQLByte,
Long: GraphQLLong,
SafeInt: GraphQLSafeInt,
UUID: GraphQLUUID,
GUID: GraphQLGUID,
Hexadecimal: GraphQLHexadecimal,
HexColorCode: GraphQLHexColorCode,
Expand Down Expand Up @@ -215,6 +219,7 @@ export {
Byte as ByteMock,
Long as LongMock,
SafeInt as SafeIntMock,
UUID as UUIDMock,
GUID as GUIDMock,
Hexadecimal as HexadecimalMock,
HexColorCode as HexColorCodeMock,
Expand Down Expand Up @@ -266,6 +271,7 @@ export {
GraphQLByte,
GraphQLLong,
GraphQLSafeInt,
GraphQLUUID,
GraphQLGUID,
GraphQLHexadecimal,
GraphQLHexColorCode,
Expand Down
5 changes: 3 additions & 2 deletions src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const Time = () => '10:15:30Z';
export const DateTime = () => '2007-12-03T10:15:30Z';
export const Timestamp = () => 1592577642;
export const UtcOffset = () => '+03:00';
export const ISO8601Duration = () => 'P3Y6M4DT12H30M5S'
export const ISO8601Duration = () => 'P3Y6M4DT12H30M5S';
export const EmailAddress = () => 'test@test.com';
export const NegativeFloat = () => -123.45;
export const NegativeInt = () => -123;
Expand All @@ -21,7 +21,7 @@ export const PositiveInt = () => 123;
export const PostalCode = () => '60031';
const URLMock = () => new URL('http://www.test.com/') as any;
// https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
export const GUID = () => {
export const UUID = () => {
// Public Domain/MIT
let d = new Date().getTime();
if (
Expand Down Expand Up @@ -93,6 +93,7 @@ export {
URLMock as URL,
NonNegativeInt as UnsignedInt,
NonNegativeFloat as UnsignedFloat,
UUID as GUID,
BigIntMock as Long,
BigIntMock as BigInt,
ByteMock as Byte,
Expand Down
49 changes: 8 additions & 41 deletions src/scalars/GUID.ts
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,
);
47 changes: 47 additions & 0 deletions src/scalars/UUID.ts
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,
);
3 changes: 2 additions & 1 deletion src/scalars/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export { GraphQLTime } from './iso-date/Time';
export { GraphQLDateTime } from './iso-date/DateTime';
export { GraphQLTimestamp } from './Timestamp';
export { GraphQLUtcOffset } from './UtcOffset';
export { GraphQLISO8601Duration } from './iso-date/Duration'
export { GraphQLISO8601Duration } from './iso-date/Duration';
export { GraphQLEmailAddress } from './EmailAddress';
export { GraphQLNegativeFloat } from './NegativeFloat';
export { GraphQLNegativeInt } from './NegativeInt';
Expand All @@ -23,6 +23,7 @@ export { GraphQLBigInt } from './BigInt';
export { GraphQLByte } from './Byte';
export { GraphQLLong } from './Long';
export { GraphQLSafeInt } from './SafeInt';
export { GraphQLUUID } from './UUID';
export { GraphQLGUID } from './GUID';
export { GraphQLHexadecimal } from './Hexadecimal';
export { GraphQLHexColorCode } from './HexColorCode';
Expand Down
6 changes: 4 additions & 2 deletions src/typeDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export const Time = 'scalar Time';
export const Timestamp = 'scalar Timestamp';
export const DateTime = 'scalar DateTime';
export const UtcOffset = 'scalar UtcOffset';
export const ISO8601Duration = 'scalar ISO8601Duration'
export const ISO8601Duration = 'scalar ISO8601Duration';
export const EmailAddress = 'scalar EmailAddress';
export const GUID = `scalar GUID`;
export const UUID = `scalar UUID`;
export const Hexadecimal = `scalar Hexadecimal`;
export const HexColorCode = `scalar HexColorCode`;
export const HSL = `scalar HSL`;
Expand Down Expand Up @@ -40,6 +40,7 @@ export const Currency = `scalar Currency`;

export const UnsignedFloat = 'scalar UnsignedFloat';
export const UnsignedInt = 'scalar UnsignedInt';
export const GUID = `scalar GUID`;
export const Long = 'scalar Long';
export const ObjectID = 'scalar ObjectID';

Expand Down Expand Up @@ -70,6 +71,7 @@ export const typeDefs = [
BigInt,
Long,
Byte,
UUID,
GUID,
Hexadecimal,
HexColorCode,
Expand Down
62 changes: 0 additions & 62 deletions tests/GUID.test.ts

This file was deleted.

Loading

0 comments on commit 3b02bc7

Please sign in to comment.