Skip to content

Commit 569cf73

Browse files
committed
Export useful sanitizing functions.
1 parent df046b1 commit 569cf73

2 files changed

Lines changed: 80 additions & 3 deletions

File tree

packages/apollo-graphql/src/__tests__/transforms.test.ts

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { default as gql, disableFragmentWarnings } from "graphql-tag";
22

3-
import { printWithReducedWhitespace, hideLiterals } from "../transforms";
3+
import {
4+
printWithReducedWhitespace,
5+
hideLiterals,
6+
hideStringAndNumericLiterals
7+
} from "../transforms";
48

59
// The gql duplicate fragment warning feature really is just warnings; nothing
610
// breaks if you turn it off in tests.
@@ -44,14 +48,27 @@ describe("hideLiterals", () => {
4448
name: "full test",
4549
input: gql`
4650
query Foo($b: Int, $a: Boolean) {
47-
user(name: "hello", age: 5) {
51+
user(
52+
name: "hello"
53+
age: 5
54+
pct: 0.4
55+
lst: ["a", "b", "c"]
56+
obj: { a: "a", b: 1 }
57+
) {
4858
...Bar
4959
... on User {
5060
hello
5161
bee
5262
}
5363
tz
5464
aliased: name
65+
withInputs(
66+
str: "hi"
67+
int: 2
68+
flt: 0.3
69+
lst: ["x", "y", "z"]
70+
obj: { q: "r", s: 1 }
71+
)
5572
}
5673
}
5774
@@ -65,7 +82,8 @@ describe("hideLiterals", () => {
6582
}
6683
`,
6784
output:
68-
'query Foo($b:Int,$a:Boolean){user(name:"",age:0){...Bar...on User{hello bee}tz aliased:name}}' +
85+
'query Foo($b:Int,$a:Boolean){user(name:"",age:0,pct:0,lst:[],obj:{}){...Bar...on User{hello bee}tz aliased:name ' +
86+
'withInputs(str:"",int:0,flt:0,lst:[],obj:{})}}' +
6987
"fragment Bar on User{age@skip(if:$a)...Nested}fragment Nested on User{blah}"
7088
}
7189
];
@@ -75,3 +93,57 @@ describe("hideLiterals", () => {
7593
});
7694
});
7795
});
96+
97+
describe("hideStringAndNumericLiterals", () => {
98+
const cases = [
99+
{
100+
name: "full test",
101+
input: gql`
102+
query Foo($b: Int, $a: Boolean) {
103+
user(
104+
name: "hello"
105+
age: 5
106+
pct: 0.4
107+
lst: ["a", "b", "c"]
108+
obj: { a: "a", b: 1 }
109+
) {
110+
...Bar
111+
... on User {
112+
hello
113+
bee
114+
}
115+
tz
116+
aliased: name
117+
withInputs(
118+
str: "hi"
119+
int: 2
120+
flt: 0.3
121+
lst: ["", "", ""]
122+
obj: { q: "", s: 0 }
123+
)
124+
}
125+
}
126+
127+
fragment Bar on User {
128+
age @skip(if: $a)
129+
...Nested
130+
}
131+
132+
fragment Nested on User {
133+
blah
134+
}
135+
`,
136+
output:
137+
'query Foo($b:Int,$a:Boolean){user(name:"",age:0,pct:0,lst:["","",""],obj:{a:"",b:0}){...Bar...on User{hello bee}tz aliased:name ' +
138+
'withInputs(str:"",int:0,flt:0,lst:["","",""],obj:{q:"",s:0})}}' +
139+
"fragment Bar on User{age@skip(if:$a)...Nested}fragment Nested on User{blah}"
140+
}
141+
];
142+
cases.forEach(({ name, input, output }) => {
143+
test(name, () => {
144+
expect(
145+
printWithReducedWhitespace(hideStringAndNumericLiterals(input))
146+
).toEqual(output);
147+
});
148+
});
149+
});

packages/apollo-graphql/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ export {
55
operationHash
66
} from "./operationId";
77
export * from "./schema";
8+
export {
9+
printWithReducedWhitespace,
10+
hideStringAndNumericLiterals,
11+
hideLiterals
12+
} from "./transforms";

0 commit comments

Comments
 (0)