Skip to content

Commit 3476149

Browse files
authored
Export useful sanitizing functions: printWithReducedWhitespace, hideStringAndNumericLiterals and hideLiterals (#2057)
1 parent 145e653 commit 3476149

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
> **Note:** Apollo's GraphQL VSCode extension is no longer housed in this repository. It is now maintained separately in [this repo](https://github.com/apollographql/vscode-graphql).
44
5+
## vNEXT
6+
7+
- `apollo-graphql`
8+
- Export useful operation sanitization transforms [PR #2057](https://github.com/apollographql/apollo-tooling/pull/2057)
9+
510
## `apollo@2.33.7`
611

712
- `apollo-graphql@0.9.4`

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

+75-3
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

+5
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ export {
77
defaultUsageReportingSignature as defaultEngineReportingSignature
88
} from "./operationId";
99
export * from "./schema";
10+
export {
11+
printWithReducedWhitespace,
12+
hideStringAndNumericLiterals,
13+
hideLiterals
14+
} from "./transforms";

0 commit comments

Comments
 (0)