Skip to content

Commit 4189dfc

Browse files
committed
fix: hide isRequired from API and add more tests and notes
1 parent 9ca3919 commit 4189dfc

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ const countryQuery = query(q => [
6565
])
6666
```
6767

68-
This will generate `TypedDocumentNode<{ countries: Array<{...}>}, { continentCode: string }>`, a typed document node that includes the input variable `continentCode`.
68+
This will generate `TypedDocumentNode<{ countries: Array<{...}>}, { continentCode?: string | null | undefined }>`, a typed document node that includes the input variable `continentCode`.
69+
70+
> Note: By default, `$` will allow nulls if the input type allows it in the desired position. Use `$$` to force-require the variable to have a non-null value.
6971
7072
The GraphQL version of the above query is shown below:
7173

src/preamble.src.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Variable<T, Name extends string> {
1515
private [VariableName]: Name
1616
private _type?: T
1717

18-
constructor(name: Name, public readonly isRequired?: boolean) {
18+
constructor(name: Name, private readonly isRequired?: boolean) {
1919
this[VariableName] = name
2020
}
2121
}
@@ -271,7 +271,7 @@ function fieldToQuery(prefix: string, field: $Field<any, any, any>) {
271271
if (kind.isRequired) type += '!'
272272
if (kind.array) type += kind.array.isRequired ? ']!' : ']'
273273

274-
if (!type.endsWith('!') && variable.isRequired === true) {
274+
if (!type.endsWith('!') && (variable as any).isRequired === true) {
275275
type += '!'
276276
}
277277

test/examples/test-countries.good.ts

+59-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { query } from './countries.graphql.api'
1+
import { query, $, $$ } from './countries.graphql.api'
22
import { verify } from './verify'
33

44
// Allow both overloads
@@ -18,11 +18,69 @@ let twoCountriesString = `{
1818
}
1919
}`
2020

21+
let countryQuery = query(q => [
22+
q.countries({ filter: { continent: { eq: $('continentCode') } } }, c => [
23+
c.code,
24+
c.capital,
25+
c.name,
26+
c.languages(l => [l.name]),
27+
]),
28+
])
29+
30+
let countryQueryString = `
31+
query ($continentCode: String) {
32+
countries(filter: {continent: {eq: $continentCode}}) {
33+
code
34+
capital
35+
name
36+
languages {
37+
name
38+
}
39+
}
40+
}`
41+
42+
let countryQueryRequiredVar = query(q => [
43+
q.countries({ filter: { continent: { eq: $$('continentCode') } } }, c => [
44+
c.code,
45+
c.capital,
46+
c.name,
47+
c.languages(l => [l.name]),
48+
]),
49+
])
50+
51+
let countryQueryStringRequired = `
52+
query ($continentCode: String!) {
53+
countries(filter: {continent: {eq: $continentCode}}) {
54+
code
55+
capital
56+
name
57+
languages {
58+
name
59+
}
60+
}
61+
}`
62+
2163
export default [
2264
verify({
2365
query: twoCountries,
2466
variables: {},
2567
schemaPath: 'countries.graphql',
2668
string: twoCountriesString,
2769
}),
70+
verify({
71+
query: countryQuery,
72+
variables: {
73+
continentCode: null,
74+
},
75+
schemaPath: 'countries.graphql',
76+
string: countryQueryString,
77+
}),
78+
verify({
79+
query: countryQueryRequiredVar,
80+
variables: {
81+
continentCode: 'x',
82+
},
83+
schemaPath: 'countries.graphql',
84+
string: countryQueryStringRequired,
85+
}),
2886
]

0 commit comments

Comments
 (0)