Skip to content

Commit 6b4d4fa

Browse files
committed
added escapeGraphQLCharacters
1 parent dd89905 commit 6b4d4fa

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/graphql.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,20 @@ export function isGeneratedByIntrospection(schema: GraphQLSchema): boolean {
185185
.filter(([name, type]) => !name.startsWith('__') && !isSpecifiedScalarType(type))
186186
.every(([, type]) => type.astNode === undefined)
187187
}
188+
189+
// https://spec.graphql.org/October2021/#EscapedCharacter
190+
const escapeMap: { [key: string]: string } = {
191+
'\"': '\\\"',
192+
'\\': '\\\\',
193+
'\/': '\\/',
194+
'\b': '\\b',
195+
'\f': '\\f',
196+
'\n': '\\n',
197+
'\r': '\\r',
198+
'\t': '\\t',
199+
};
200+
201+
export function escapeGraphQLCharacters(input: string): string {
202+
// eslint-disable-next-line regexp/no-escape-backspace
203+
return input.replace(/["\\/\f\n\r\t\b]/g, match => escapeMap[match]);
204+
}

tests/graphql.spec.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from 'graphql';
1313
import dedent from 'ts-dedent';
1414

15-
import { ObjectTypeDefinitionBuilder, isGeneratedByIntrospection, topologicalSortAST, topsort } from '../src/graphql';
15+
import { ObjectTypeDefinitionBuilder, escapeGraphQLCharacters, isGeneratedByIntrospection, topologicalSortAST, topsort } from '../src/graphql';
1616

1717
describe('graphql', () => {
1818
describe('objectTypeDefinitionBuilder', () => {
@@ -297,3 +297,65 @@ describe('isGeneratedByIntrospection function', () => {
297297
expect(isGeneratedByIntrospection(clientSchema)).toBe(true);
298298
});
299299
});
300+
301+
describe('escapeGraphQLCharacters', () => {
302+
it('should escape double quotes', () => {
303+
const input = 'This is a "test" string.';
304+
const expected = 'This is a \\\"test\\\" string.';
305+
expect(escapeGraphQLCharacters(input)).toBe(expected);
306+
});
307+
308+
it('should escape backslashes', () => {
309+
const input = 'This is a backslash: \\';
310+
const expected = 'This is a backslash: \\\\';
311+
expect(escapeGraphQLCharacters(input)).toBe(expected);
312+
});
313+
314+
it('should escape forward slashes', () => {
315+
const input = 'This is a forward slash: /';
316+
const expected = 'This is a forward slash: \\/';
317+
expect(escapeGraphQLCharacters(input)).toBe(expected);
318+
});
319+
320+
it('should escape backspaces', () => {
321+
const input = 'This is a backspace: \b';
322+
const expected = 'This is a backspace: \\b';
323+
expect(escapeGraphQLCharacters(input)).toBe(expected);
324+
});
325+
326+
it('should escape form feeds', () => {
327+
const input = 'This is a form feed: \f';
328+
const expected = 'This is a form feed: \\f';
329+
expect(escapeGraphQLCharacters(input)).toBe(expected);
330+
});
331+
332+
it('should escape new lines', () => {
333+
const input = 'This is a new line: \n';
334+
const expected = 'This is a new line: \\n';
335+
expect(escapeGraphQLCharacters(input)).toBe(expected);
336+
});
337+
338+
it('should escape carriage returns', () => {
339+
const input = 'This is a carriage return: \r';
340+
const expected = 'This is a carriage return: \\r';
341+
expect(escapeGraphQLCharacters(input)).toBe(expected);
342+
});
343+
344+
it('should escape horizontal tabs', () => {
345+
const input = 'This is a tab: \t';
346+
const expected = 'This is a tab: \\t';
347+
expect(escapeGraphQLCharacters(input)).toBe(expected);
348+
});
349+
350+
it('should escape multiple special characters', () => {
351+
const input = 'This is a "test" string with \n new line and \t tab.';
352+
const expected = 'This is a \\\"test\\\" string with \\n new line and \\t tab.';
353+
expect(escapeGraphQLCharacters(input)).toBe(expected);
354+
});
355+
356+
it('should not escape non-special characters', () => {
357+
const input = 'Normal string with no special characters.';
358+
const expected = 'Normal string with no special characters.';
359+
expect(escapeGraphQLCharacters(input)).toBe(expected);
360+
});
361+
});

0 commit comments

Comments
 (0)