Skip to content

Commit fed5b3f

Browse files
committed
Define InlineMarkdown scalar
1 parent a81116d commit fed5b3f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/common/markdown.scalar.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { CustomScalar, Scalar } from '@nestjs/graphql';
2+
import { GraphQLError, Kind, ValueNode } from 'graphql';
3+
4+
@Scalar('InlineMarkdown')
5+
export class InlineMarkdownScalar
6+
implements CustomScalar<string, string | null>
7+
{
8+
description = 'A string that holds inline Markdown formatted text';
9+
10+
parseLiteral(ast: ValueNode): string | null {
11+
if (ast.kind !== Kind.STRING) {
12+
throw new GraphQLError(
13+
`Can only validate strings as InlineMarkdown but got a: ${ast.kind}`,
14+
);
15+
}
16+
return ast.value;
17+
}
18+
parseValue(value: any) {
19+
return value;
20+
}
21+
serialize(value: any) {
22+
return value;
23+
}
24+
}

src/common/scalars.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CustomScalar } from '@nestjs/graphql';
33
import { GraphQLScalarType } from 'graphql';
44
import UploadScalar from 'graphql-upload/GraphQLUpload.mjs';
55
import { DateScalar, DateTimeScalar } from './luxon.graphql';
6+
import { InlineMarkdownScalar } from './markdown.scalar';
67
import { RichTextScalar } from './rich-text.scalar';
78
import { UrlScalar } from './url.field';
89

@@ -15,4 +16,5 @@ export const getRegisteredScalars = (): Scalar[] => [
1516
RichTextScalar,
1617
UploadScalar,
1718
UrlScalar,
19+
InlineMarkdownScalar,
1820
];

0 commit comments

Comments
 (0)