-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoFragmentOperations.java
More file actions
114 lines (95 loc) · 4.07 KB
/
Copy pathDemoFragmentOperations.java
File metadata and controls
114 lines (95 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import com.intuit.gqlex.transformation.GraphQLTransformer;
import com.intuit.gqlex.transformation.TransformationResult;
public class DemoFragmentOperations {
public static void main(String[] args) {
System.out.println("=== FRAGMENT OPERATIONS DEMO ===\n");
// Example 1: Fragment Inlining
demoFragmentInlining();
System.out.println("\n" + "=".repeat(50) + "\n");
// Example 2: Fragment Extraction
demoFragmentExtraction();
}
public static void demoFragmentInlining() {
System.out.println("1. FRAGMENT INLINING - BEFORE:");
String queryWithFragments = """
query GetUser($userId: ID!) {
user(id: $userId) {
id
name
...UserProfile
...UserPosts
}
}
fragment UserProfile on User {
email
profile {
bio
avatar
}
}
fragment UserPosts on User {
posts {
id
title
}
}
""";
System.out.println(queryWithFragments);
System.out.println("\nRUNNING: transformer.inlineAllFragments().transform()");
try {
GraphQLTransformer transformer = new GraphQLTransformer(queryWithFragments);
TransformationResult result = transformer.inlineAllFragments().transform();
System.out.println("\nAFTER (fragments inlined):");
if (result.isSuccess()) {
System.out.println(result.getQueryString());
System.out.println("\nWHAT HAPPENED:");
System.out.println("✓ ...UserProfile → replaced with email + profile { bio avatar }");
System.out.println("✓ ...UserPosts → replaced with posts { id title }");
System.out.println("✓ All fragment definitions removed");
System.out.println("✓ Query is now self-contained");
} else {
System.out.println("ERROR: " + result.getErrors());
}
} catch (Exception e) {
System.out.println("ERROR: " + e.getMessage());
}
}
public static void demoFragmentExtraction() {
System.out.println("2. FRAGMENT EXTRACTION - BEFORE:");
String queryWithInlineFields = """
query GetHero($episode: Episode) {
hero(episode: $episode) {
id
name
appearsIn
friends {
id
name
appearsIn
}
}
}
""";
System.out.println(queryWithInlineFields);
System.out.println("\nRUNNING: transformer.extractFragment(\"//query/hero\", \"HeroFields\", \"Character\").transform()");
try {
GraphQLTransformer transformer = new GraphQLTransformer(queryWithInlineFields);
TransformationResult result = transformer
.extractFragment("//query/hero", "HeroFields", "Character")
.transform();
System.out.println("\nAFTER (fragment extracted):");
if (result.isSuccess()) {
System.out.println(result.getQueryString());
System.out.println("\nWHAT HAPPENED:");
System.out.println("✓ Selection set under 'hero' extracted to fragment 'HeroFields'");
System.out.println("✓ Fragment has type condition 'Character'");
System.out.println("✓ Original query now references ...HeroFields");
System.out.println("✓ Fields: id, name, appearsIn, friends { id, name, appearsIn }");
} else {
System.out.println("ERROR: " + result.getErrors());
}
} catch (Exception e) {
System.out.println("ERROR: " + e.getMessage());
}
}
}