-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfield-filtering.go
More file actions
143 lines (133 loc) · 6.91 KB
/
field-filtering.go
File metadata and controls
143 lines (133 loc) · 6.91 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package examples
// Field Filtering Examples
// This file demonstrates the new field filtering capabilities using gql struct tags
// Example 1: Read-Only Fields (ro)
// Fields marked with 'ro' appear only in types, not in inputs
// @gqlType(name:"User")
// @gqlInput(name:"UserInput")
type UserWithReadOnly struct {
ID string `gql:"id,type:ID,ro"` // Only in User type, not in UserInput
CreatedAt string `gql:"createdAt,ro"` // Only in User type, not in UserInput
Name string `gql:"name"` // In both User type and UserInput
Email string `gql:"email"` // In both User type and UserInput
}
// Example 2: Write-Only Fields (wo)
// Fields marked with 'wo' appear only in inputs, not in types
// @gqlType(name:"User")
// @gqlInput(name:"CreateUserInput")
type UserWithPassword struct {
ID string `gql:"id,type:ID,ro"` // Only in User type
Name string `gql:"name"` // In both
Password string `gql:"password,wo"` // Only in CreateUserInput, not in User type
}
// Example 3: Include Field in Specific Types
// Use 'include:' to specify which types should have this field
// Supports three syntax styles: "Type,Type", 'Type,Type', or [Type,Type]
// @gqlType(name:"PublicUser")
// @gqlType(name:"AdminUser")
// @gqlType(name:"SuperAdminUser")
type UserWithConditionalFields struct {
ID string `gql:"id,type:ID"`
Email string `gql:"email,include:\"AdminUser,SuperAdminUser\""` // Double quotes
Phone string `gql:"phone,include:'AdminUser,SuperAdminUser'"` // Single quotes (alternative)
Address string `gql:"address,include:[AdminUser,SuperAdminUser]"` // Square brackets (alternative)
SecretKey string `gql:"secretKey,include:SuperAdminUser"` // Only in SuperAdminUser (single value)
}
// Example 4: Omit/Ignore Field from Specific Types
// Use 'omit:' or 'ignore:' to exclude field from specific types (they're aliases)
// @gqlType(name:"FullUser")
// @gqlType(name:"PartialUser")
type UserWithOmissions struct {
ID string `gql:"id,type:ID"`
Name string `gql:"name"`
Email string `gql:"email,omit:PartialUser"` // Excluded from PartialUser
Phone string `gql:"phone,ignore:PartialUser"` // Also excluded from PartialUser (omit and ignore are aliases)
}
// Example 5: Read-Only for Specific Types
// Combine 'ro' with type list to make field read-only for specific types only
// @gqlType(name:"AdminView")
// @gqlType(name:"UserView")
// @gqlInput(name:"AdminInput")
// @gqlInput(name:"UserInput")
type AccountData struct {
ID string `gql:"id,type:ID,ro"` // Read-only for all types
Name string `gql:"name"` // In all types and inputs
SecretData string `gql:"secretData,ro:AdminView"` // Only in AdminView type (single value)
Balance float64 `gql:"balance,ro:\"AdminView,UserView\""` // Read-only in both AdminView and UserView
}
// Example 6: Wildcard Usage
// Use '*' to apply to all types/inputs
// @gqlIgnoreAll // Ignore all fields by default
// @gqlType(name:"User")
// @gqlInput(name:"UserInput")
type SelectiveUser struct {
ID string `gql:"id,type:ID,include:*"` // Included in all (overrides @gqlIgnoreAll)
Name string `gql:"name,include"` // Included in all (shorthand for include:*)
Email string `gql:"email,rw:*"` // Read-write in all (overrides @gqlIgnoreAll)
Internal string // Ignored (due to @gqlIgnoreAll)
}
// Example 7: Multiple Types with Different Visibility
// @gqlType(name:"UserV1")
// @gqlType(name:"UserV2")
// @gqlType(name:"UserV3")
type EvolvingUser struct {
ID string `gql:"id,type:ID"` // In all versions
Name string `gql:"name"` // In all versions
Email string `gql:"email,include:\"UserV2,UserV3\""` // Added in V2, kept in V3
Phone string `gql:"phone,include:UserV3"` // Added in V3 only (single value)
OldField string `gql:"oldField,omit:\"UserV2,UserV3\""` // Only in V1, removed in V2+
}
// Example 8: Complex Scenario - Different Fields for Different Contexts
// @gqlType(name:"PublicProfile")
// @gqlType(name:"PrivateProfile")
// @gqlInput(name:"CreateProfileInput")
// @gqlInput(name:"UpdateProfileInput")
type ProfileData struct {
ID string `gql:"id,type:ID,ro"` // Read-only in types
Username string `gql:"username,ro:\"PublicProfile,PrivateProfile\""` // Read-only in types, editable in inputs
DisplayName string `gql:"displayName"` // In all
Email string `gql:"email,omit:PublicProfile"` // Hidden from public view (single value)
Bio string `gql:"bio"` // In all
PrivateNotes string `gql:"privateNotes,include:PrivateProfile"` // Only in private view (single value)
Password string `gql:"password,wo"` // Write-only (inputs only)
}
// Summary of available tags:
//
// Basic flags (no type list):
// - ro : Read-only everywhere (types only, excluded from all inputs)
// - wo : Write-only everywhere (inputs only, excluded from all types)
// - rw : Read-write everywhere (shorthand for include:*)
// - include : Include everywhere (overrides @gqlIgnoreAll)
// - omit : Omit/ignore everywhere
// - ignore : Omit/ignore everywhere (alias for omit)
//
// With single type:
// - ro:TypeName : Read-only for TypeName only (no quotes needed)
// - wo:InputName : Write-only for InputName only (no quotes needed)
// - include:TypeName: Include only in TypeName (no quotes needed)
// - omit:TypeName : Exclude from TypeName (no quotes needed)
//
// With multiple types (REQUIRES QUOTES):
// - ro:"TypeA,TypeB" : Read-only for TypeA and TypeB only
// - wo:"InputA,InputB" : Write-only for InputA and InputB only
// - rw:"TypeA,TypeB" : Include in TypeA and TypeB (both types and inputs)
// - include:"TypeA,TypeB" : Include only in TypeA and TypeB
// - omit:"TypeA,TypeB" : Exclude from TypeA and TypeB
// - ignore:"TypeA,TypeB" : Exclude from TypeA and TypeB (alias for omit)
//
// Multiple types also support single quotes and square brackets:
// - include:'TypeA,TypeB' : Single quotes (alternative syntax)
// - include:[TypeA,TypeB] : Square brackets (alternative syntax)
// - You can mix styles: include:"A,B",omit:'C,D',ro:[E,F]
//
// Wildcard:
// - include:* or include : Include in all types/inputs
// - omit:* or omit : Exclude from all types/inputs
// - rw:* : Include in all types and inputs
//
// Notes:
// - omit and ignore are aliases (same behavior)
// - Multiple types can use: "TypeA,TypeB" or 'TypeA,TypeB' or [TypeA,TypeB]
// - Single types don't need quotes: include:TypeName
// - Separate type names with commas (no spaces): "TypeA,TypeB,TypeC"
// - Type names in lists match the name specified in @gqlType/@gqlInput directives