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

Fix scalar throwing 500 #9465

Merged
merged 1 commit into from
Jan 8, 2025
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
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import { GraphQLScalarType, Kind } from 'graphql';

import { ValidationError } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';

export const CursorScalarType = new GraphQLScalarType({
name: 'Cursor',
description: 'A custom scalar that represents a cursor for pagination',
serialize(value) {
if (typeof value !== 'string') {
throw new Error('Cursor must be a string');
throw new ValidationError('Cursor must be a string');
}

return value;
},
parseValue(value) {
if (typeof value !== 'string') {
throw new Error('Cursor must be a string');
throw new ValidationError('Cursor must be a string');
}

return value;
},
parseLiteral(ast) {
if (ast.kind !== Kind.STRING) {
throw new Error('Cursor must be a string');
throw new ValidationError('Cursor must be a string');
}

return ast.value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { GraphQLScalarType, Kind } from 'graphql';

import { ValidationError } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';

type PositionType = 'first' | 'last' | number;

const isValidStringPosition = (value: string): boolean =>
Expand All @@ -13,7 +15,9 @@ const checkPosition = (value: any): PositionType => {
return value;
}

throw new Error('Invalid position found');
throw new ValidationError(
`Invalid position value: '${value}'. Position must be 'first', 'last', or a number`,
);
};

export const PositionScalarType = new GraphQLScalarType({
Expand All @@ -34,6 +38,6 @@ export const PositionScalarType = new GraphQLScalarType({
return parseFloat(ast.value);
}

throw new Error('Invalid position found');
throw new ValidationError('Invalid position value');
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Maybe } from 'graphql-yoga';
import { ObjMap } from 'graphql/jsutils/ObjMap';
import { ASTNode, Kind, ValueNode } from 'graphql/language';

import { ValidationError } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';

const parseLiteral = (
ast: ValueNode,
variables?: Maybe<ObjMap<unknown>>,
Expand All @@ -25,7 +27,7 @@ const parseLiteral = (
case Kind.VARIABLE:
return variables ? variables[ast.name.value] : undefined;
default:
throw new TypeError(
throw new ValidationError(
`JSONStringify cannot represent value: ${JSON.stringify(ast)}`,
);
}
Expand Down Expand Up @@ -54,7 +56,7 @@ const parseJSON = (value: string): object => {
try {
return JSON.parse(value);
} catch (e) {
throw new TypeError(`Value is not valid JSON: ${value}`);
throw new ValidationError(`Value is not valid JSON: ${value}`);
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { GraphQLScalarType } from 'graphql';
import { IntValueNode, Kind } from 'graphql/language';

import { ValidationError } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';

export const TimeScalarType = new GraphQLScalarType({
name: 'Time',
description: 'Time custom scalar type',
Expand All @@ -17,8 +19,8 @@ export const TimeScalarType = new GraphQLScalarType({
if (typeof intAst.value === 'number') {
return new Date(intAst.value);
}
throw new Error(`Invalid timestamp value: ${ast.value}`);
throw new ValidationError(`Invalid timestamp value: ${ast.value}`);
}
throw new Error(`Invalid AST kind: ${ast.kind}`);
throw new ValidationError(`Invalid AST kind: ${ast.kind}`);
},
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { GraphQLScalarType, Kind } from 'graphql';
import { validate as uuidValidate } from 'uuid';

import { ValidationError } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';

const checkUUID = (value: any): string => {
if (typeof value !== 'string') {
throw new Error('UUID must be a string');
throw new ValidationError('UUID must be a string');
}
if (!uuidValidate(value)) {
throw new Error('Invalid UUID');
throw new ValidationError('Invalid UUID');
}

return value;
Expand All @@ -19,7 +21,7 @@ export const UUIDScalarType = new GraphQLScalarType({
parseValue: checkUUID,
parseLiteral(ast): string {
if (ast.kind !== Kind.STRING) {
throw new Error('UUID must be a string');
throw new ValidationError('UUID must be a string');
}

return ast.value;
Expand Down
Loading