Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.

Commit 6e3dee6

Browse files
authored
Merge pull request #140 from creatyvtype/writeable-query-variables
Make relay variables writeable in ts
2 parents 9c97404 + d33a0cf commit 6e3dee6

File tree

2 files changed

+59
-54
lines changed

2 files changed

+59
-54
lines changed

src/TypeScriptGenerator.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function makeProp(
9292
if (schemaName === "__typename" && concreteType) {
9393
value = ts.createLiteralTypeNode(ts.createLiteral(concreteType));
9494
}
95-
return readOnlyObjectTypeProperty(key, value, conditional);
95+
return objectTypeProperty(key, value, { optional: conditional });
9696
}
9797

9898
const isTypenameSelection = (selection: Selection) =>
@@ -156,7 +156,7 @@ function selectionsToAST(
156156
// this doesn't exist in Flow at the time.
157157
types.push(
158158
Array.from(typenameAliases).map(typenameAlias => {
159-
const otherProp = readOnlyObjectTypeProperty(
159+
const otherProp = objectTypeProperty(
160160
typenameAlias,
161161
ts.createLiteralTypeNode(ts.createLiteral("%other"))
162162
);
@@ -203,7 +203,7 @@ function selectionsToAST(
203203
const typeElements = types.map(props => {
204204
if (fragmentTypeName) {
205205
props.push(
206-
readOnlyObjectTypeProperty(
206+
objectTypeProperty(
207207
REF_TYPE,
208208
ts.createTypeReferenceNode(
209209
ts.createIdentifier(fragmentTypeName),
@@ -231,13 +231,18 @@ function exactObjectTypeAnnotation(
231231

232232
const idRegex = /^[$a-zA-Z_][$a-z0-9A-Z_]*$/;
233233

234-
function readOnlyObjectTypeProperty(
234+
function objectTypeProperty(
235235
propertyName: string,
236236
type: ts.TypeNode,
237-
optional?: boolean
237+
options: { readonly?: boolean; optional?: boolean } = {}
238238
): ts.PropertySignature {
239+
const { optional, readonly = true } = options;
240+
const modifiers = readonly
241+
? [ts.createToken(ts.SyntaxKind.ReadonlyKeyword)]
242+
: undefined;
243+
239244
return ts.createPropertySignature(
240-
[ts.createToken(ts.SyntaxKind.ReadonlyKeyword)],
245+
modifiers,
241246
idRegex.test(propertyName)
242247
? ts.createIdentifier(propertyName)
243248
: ts.createLiteral(propertyName),
@@ -360,11 +365,11 @@ function createVisitor(options: TypeGeneratorOptions): IRVisitor.NodeVisitor {
360365
)
361366
);
362367
const operationTypes = [
363-
readOnlyObjectTypeProperty(
368+
objectTypeProperty(
364369
"response",
365370
ts.createTypeReferenceNode(responseType.name, undefined)
366371
),
367-
readOnlyObjectTypeProperty(
372+
objectTypeProperty(
368373
"variables",
369374
ts.createTypeReferenceNode(inputVariablesType.name, undefined)
370375
)
@@ -407,7 +412,7 @@ function createVisitor(options: TypeGeneratorOptions): IRVisitor.NodeVisitor {
407412
);
408413
}
409414
operationTypes.push(
410-
readOnlyObjectTypeProperty(
415+
objectTypeProperty(
411416
"rawResponse",
412417
ts.createTypeReferenceNode(`${node.name}RawResponse`, undefined)
413418
)
@@ -612,7 +617,7 @@ function makeRawResponseProp(
612617
if (schemaName === "__typename" && concreteType) {
613618
value = ts.createLiteralTypeNode(ts.createLiteral(concreteType));
614619
}
615-
const typeProperty = readOnlyObjectTypeProperty(key, value);
620+
const typeProperty = objectTypeProperty(key, value);
616621
if (conditional) {
617622
typeProperty.questionToken = ts.createToken(ts.SyntaxKind.QuestionToken);
618623
}
@@ -821,10 +826,10 @@ function generateInputVariablesType(node: Root, state: State) {
821826
`${node.name}Variables`,
822827
exactObjectTypeAnnotation(
823828
node.argumentDefinitions.map(arg => {
824-
return readOnlyObjectTypeProperty(
829+
return objectTypeProperty(
825830
arg.name,
826831
transformInputType(arg.type, state),
827-
!(arg.type instanceof GraphQLNonNull)
832+
{ readonly: false, optional: !(arg.type instanceof GraphQLNonNull) }
828833
);
829834
})
830835
)

test/__snapshots__/TypeScriptGenerator-test.ts.snap

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fragment FeedbackComments_feedback on Feedback {
5858
// QueryWithConnectionField.graphql
5959
type FeedbackComments_feedback$ref = any;
6060
export type QueryWithConnectionFieldVariables = {
61-
readonly id: string;
61+
id: string;
6262
};
6363
export type QueryWithConnectionFieldResponse = {
6464
readonly feedback: {
@@ -628,7 +628,7 @@ export type UpdateAllSeenStateInput = {
628628
readonly storyIds?: ReadonlyArray<string | null> | null;
629629
};
630630
export type TestVariables = {
631-
readonly input?: UpdateAllSeenStateInput | null;
631+
input?: UpdateAllSeenStateInput | null;
632632
};
633633
export type TestResponse = {
634634
readonly viewerNotificationsUpdateAllSeenState: {
@@ -702,7 +702,7 @@ export type FeedbackcommentComment = {
702702
readonly feedback?: CommentfeedbackFeedback | null;
703703
};
704704
export type TestMutationVariables = {
705-
readonly input: CommentCreateInput;
705+
input: CommentCreateInput;
706706
};
707707
export type TestMutationResponse = {
708708
readonly commentCreate: {
@@ -796,9 +796,9 @@ export type FeedbackcommentComment = {
796796
readonly feedback?: CommentfeedbackFeedback | null;
797797
};
798798
export type CommentCreateMutationVariables = {
799-
readonly input: CommentCreateInput;
800-
readonly first?: number | null;
801-
readonly orderBy?: ReadonlyArray<string> | null;
799+
input: CommentCreateInput;
800+
first?: number | null;
801+
orderBy?: ReadonlyArray<string> | null;
802802
};
803803
export type CommentCreateMutationResponse = {
804804
readonly commentCreate: {
@@ -835,7 +835,7 @@ export type UpdateAllSeenStateInput = {
835835
readonly storyIds?: ReadonlyArray<string | null> | null;
836836
};
837837
export type InputHasArrayVariables = {
838-
readonly input?: UpdateAllSeenStateInput | null;
838+
input?: UpdateAllSeenStateInput | null;
839839
};
840840
export type InputHasArrayResponse = {
841841
readonly viewerNotificationsUpdateAllSeenState: {
@@ -906,9 +906,9 @@ export type FeedbackcommentComment = {
906906
readonly feedback?: CommentfeedbackFeedback | null;
907907
};
908908
export type CommentCreateMutationVariables = {
909-
readonly input: CommentCreateInput;
910-
readonly first?: number | null;
911-
readonly orderBy?: ReadonlyArray<string> | null;
909+
input: CommentCreateInput;
910+
first?: number | null;
911+
orderBy?: ReadonlyArray<string> | null;
912912
};
913913
export type CommentCreateMutationResponse = {
914914
readonly commentCreate: {
@@ -1016,9 +1016,9 @@ export type FeedbackcommentComment = {
10161016
readonly feedback?: CommentfeedbackFeedback | null;
10171017
};
10181018
export type CommentCreateMutationVariables = {
1019-
readonly input: CommentCreateInput;
1020-
readonly first?: number | null;
1021-
readonly orderBy?: ReadonlyArray<string> | null;
1019+
input: CommentCreateInput;
1020+
first?: number | null;
1021+
orderBy?: ReadonlyArray<string> | null;
10221022
};
10231023
export type CommentCreateMutationResponse = {
10241024
readonly commentCreate: {
@@ -1188,7 +1188,7 @@ query ScalarHandleField($id: ID!) @raw_response_type {
11881188
~~~~~~~~~~ OUTPUT ~~~~~~~~~~
11891189
// LinkedHandleField.graphql
11901190
export type LinkedHandleFieldVariables = {
1191-
readonly id: string;
1191+
id: string;
11921192
};
11931193
export type LinkedHandleFieldResponse = {
11941194
readonly node: {
@@ -1218,7 +1218,7 @@ export type LinkedHandleField = {
12181218
12191219
// ScalarHandleField.graphql
12201220
export type ScalarHandleFieldVariables = {
1221-
readonly id: string;
1221+
id: string;
12221222
};
12231223
export type ScalarHandleFieldResponse = {
12241224
readonly node: {
@@ -1266,8 +1266,8 @@ fragment FriendFragment on User {
12661266
// ExampleQuery.graphql
12671267
type FriendFragment$ref = any;
12681268
export type ExampleQueryVariables = {
1269-
readonly id: string;
1270-
readonly condition: boolean;
1269+
id: string;
1270+
condition: boolean;
12711271
};
12721272
export type ExampleQueryResponse = {
12731273
readonly node: {
@@ -1427,7 +1427,7 @@ export type RefetchableFragment = {
14271427
declare const _RefetchableFragment$ref: unique symbol;
14281428
export type RefetchableFragment$ref = typeof _RefetchableFragment$ref;
14291429
export type RefetchableFragmentQueryVariables = {
1430-
readonly id: string;
1430+
id: string;
14311431
};
14321432
export type RefetchableFragmentQueryResponse = {
14331433
readonly node: {
@@ -1472,7 +1472,7 @@ subscription TestSubscription($input: FeedbackLikeInput) {
14721472
~~~~~~~~~~ OUTPUT ~~~~~~~~~~
14731473
// ExampleQuery.graphql
14741474
export type ExampleQueryVariables = {
1475-
readonly id: string;
1475+
id: string;
14761476
};
14771477
export type ExampleQueryResponse = {
14781478
readonly node: {
@@ -1507,7 +1507,7 @@ export type FeedbackcommentComment = {
15071507
readonly feedback?: CommentfeedbackFeedback | null;
15081508
};
15091509
export type TestMutationVariables = {
1510-
readonly input: CommentCreateInput;
1510+
input: CommentCreateInput;
15111511
};
15121512
export type TestMutationResponse = {
15131513
readonly commentCreate: {
@@ -1528,7 +1528,7 @@ export type FeedbackLikeInput = {
15281528
readonly feedbackId?: string | null;
15291529
};
15301530
export type TestSubscriptionVariables = {
1531-
readonly input?: FeedbackLikeInput | null;
1531+
input?: FeedbackLikeInput | null;
15321532
};
15331533
export type TestSubscriptionResponse = {
15341534
readonly feedbackLikeSubscribe: {
@@ -1972,7 +1972,7 @@ fragment FeedbackComments_feedback on Feedback {
19721972
// QueryWithConnectionField.graphql
19731973
type FeedbackComments_feedback$ref = any;
19741974
export type QueryWithConnectionFieldVariables = {
1975-
readonly id: string;
1975+
id: string;
19761976
};
19771977
export type QueryWithConnectionFieldResponse = {
19781978
readonly feedback: {
@@ -2523,7 +2523,7 @@ export type UpdateAllSeenStateInput = {
25232523
readonly storyIds?: ReadonlyArray<string | null> | null;
25242524
};
25252525
export type TestVariables = {
2526-
readonly input?: UpdateAllSeenStateInput | null;
2526+
input?: UpdateAllSeenStateInput | null;
25272527
};
25282528
export type TestResponse = {
25292529
readonly viewerNotificationsUpdateAllSeenState: {
@@ -2597,7 +2597,7 @@ export type FeedbackcommentComment = {
25972597
readonly feedback?: CommentfeedbackFeedback | null;
25982598
};
25992599
export type TestMutationVariables = {
2600-
readonly input: CommentCreateInput;
2600+
input: CommentCreateInput;
26012601
};
26022602
export type TestMutationResponse = {
26032603
readonly commentCreate: {
@@ -2690,9 +2690,9 @@ export type FeedbackcommentComment = {
26902690
readonly feedback?: CommentfeedbackFeedback | null;
26912691
};
26922692
export type CommentCreateMutationVariables = {
2693-
readonly input: CommentCreateInput;
2694-
readonly first?: number | null;
2695-
readonly orderBy?: ReadonlyArray<string> | null;
2693+
input: CommentCreateInput;
2694+
first?: number | null;
2695+
orderBy?: ReadonlyArray<string> | null;
26962696
};
26972697
export type CommentCreateMutationResponse = {
26982698
readonly commentCreate: {
@@ -2729,7 +2729,7 @@ export type UpdateAllSeenStateInput = {
27292729
readonly storyIds?: ReadonlyArray<string | null> | null;
27302730
};
27312731
export type InputHasArrayVariables = {
2732-
readonly input?: UpdateAllSeenStateInput | null;
2732+
input?: UpdateAllSeenStateInput | null;
27332733
};
27342734
export type InputHasArrayResponse = {
27352735
readonly viewerNotificationsUpdateAllSeenState: {
@@ -2800,9 +2800,9 @@ export type FeedbackcommentComment = {
28002800
readonly feedback?: CommentfeedbackFeedback | null;
28012801
};
28022802
export type CommentCreateMutationVariables = {
2803-
readonly input: CommentCreateInput;
2804-
readonly first?: number | null;
2805-
readonly orderBy?: ReadonlyArray<string> | null;
2803+
input: CommentCreateInput;
2804+
first?: number | null;
2805+
orderBy?: ReadonlyArray<string> | null;
28062806
};
28072807
export type CommentCreateMutationResponse = {
28082808
readonly commentCreate: {
@@ -2909,9 +2909,9 @@ export type FeedbackcommentComment = {
29092909
readonly feedback?: CommentfeedbackFeedback | null;
29102910
};
29112911
export type CommentCreateMutationVariables = {
2912-
readonly input: CommentCreateInput;
2913-
readonly first?: number | null;
2914-
readonly orderBy?: ReadonlyArray<string> | null;
2912+
input: CommentCreateInput;
2913+
first?: number | null;
2914+
orderBy?: ReadonlyArray<string> | null;
29152915
};
29162916
export type CommentCreateMutationResponse = {
29172917
readonly commentCreate: {
@@ -3078,7 +3078,7 @@ query ScalarHandleField($id: ID!) @raw_response_type {
30783078
~~~~~~~~~~ OUTPUT ~~~~~~~~~~
30793079
// LinkedHandleField.graphql
30803080
export type LinkedHandleFieldVariables = {
3081-
readonly id: string;
3081+
id: string;
30823082
};
30833083
export type LinkedHandleFieldResponse = {
30843084
readonly node: {
@@ -3108,7 +3108,7 @@ export type LinkedHandleField = {
31083108
31093109
// ScalarHandleField.graphql
31103110
export type ScalarHandleFieldVariables = {
3111-
readonly id: string;
3111+
id: string;
31123112
};
31133113
export type ScalarHandleFieldResponse = {
31143114
readonly node: {
@@ -3156,8 +3156,8 @@ fragment FriendFragment on User {
31563156
// ExampleQuery.graphql
31573157
type FriendFragment$ref = any;
31583158
export type ExampleQueryVariables = {
3159-
readonly id: string;
3160-
readonly condition: boolean;
3159+
id: string;
3160+
condition: boolean;
31613161
};
31623162
export type ExampleQueryResponse = {
31633163
readonly node: {
@@ -3313,7 +3313,7 @@ export type RefetchableFragment = {
33133313
// RefetchableFragmentQuery.graphql
33143314
type RefetchableFragment$ref = any;
33153315
export type RefetchableFragmentQueryVariables = {
3316-
readonly id: string;
3316+
id: string;
33173317
};
33183318
export type RefetchableFragmentQueryResponse = {
33193319
readonly node: {
@@ -3358,7 +3358,7 @@ subscription TestSubscription($input: FeedbackLikeInput) {
33583358
~~~~~~~~~~ OUTPUT ~~~~~~~~~~
33593359
// ExampleQuery.graphql
33603360
export type ExampleQueryVariables = {
3361-
readonly id: string;
3361+
id: string;
33623362
};
33633363
export type ExampleQueryResponse = {
33643364
readonly node: {
@@ -3392,7 +3392,7 @@ export type FeedbackcommentComment = {
33923392
readonly feedback?: CommentfeedbackFeedback | null;
33933393
};
33943394
export type TestMutationVariables = {
3395-
readonly input: CommentCreateInput;
3395+
input: CommentCreateInput;
33963396
};
33973397
export type TestMutationResponse = {
33983398
readonly commentCreate: {
@@ -3413,7 +3413,7 @@ export type FeedbackLikeInput = {
34133413
readonly feedbackId?: string | null;
34143414
};
34153415
export type TestSubscriptionVariables = {
3416-
readonly input?: FeedbackLikeInput | null;
3416+
input?: FeedbackLikeInput | null;
34173417
};
34183418
export type TestSubscriptionResponse = {
34193419
readonly feedbackLikeSubscribe: {

0 commit comments

Comments
 (0)