Skip to content

Commit 149b6d9

Browse files
committed
fix fragments
1 parent c0d5455 commit 149b6d9

File tree

6 files changed

+69
-16
lines changed

6 files changed

+69
-16
lines changed

build_tree.go

+43-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fields
22

33
import (
4+
"sort"
45
"strings"
56
)
67

@@ -53,11 +54,11 @@ func BuildTree(request string) map[string][]string {
5354

5455
path := strings.Join(currentTree, ".")
5556

56-
tree[path] = append(tree[path], field)
57+
tree[path] = appendIfMissing(tree[path], field)
5758
}
5859

5960
if levelUpRegex.MatchString(line) {
60-
currentTree = append(currentTree, lastField)
61+
currentTree = appendIfMissing(currentTree, lastField)
6162
currentLevel++
6263
} else if levelDownRegex.MatchString(line) {
6364
currentTree = currentTree[:len(currentTree)-1]
@@ -106,12 +107,40 @@ func extractAndGroupFragments(request string) map[string]string {
106107
return fragments
107108
}
108109

110+
const fragmentsLimit int = 1000
111+
109112
func applyFragments(request string, fragments map[string]string) string {
113+
var sortedFragments []string
114+
115+
for name, _ := range fragments {
116+
sortedFragments = append(sortedFragments, name)
117+
}
118+
119+
sort.Sort(byLength(sortedFragments))
120+
121+
continueApplying := true
122+
123+
replacedFragments := 0
124+
125+
for continueApplying {
126+
if partialFragmentRegex.MatchString(request) {
127+
for _, name := range sortedFragments {
128+
request = strings.Replace(request, name, fragments[name], -1)
129+
}
130+
131+
replacedFragments++
132+
133+
if replacedFragments > fragmentsLimit {
134+
continueApplying = false
135+
}
136+
} else {
137+
continueApplying = false
138+
}
139+
}
140+
110141
// TODO: while ... exists, with a max of loops.
111142
for i := 5; i > 0; i-- {
112-
for name, body := range fragments {
113-
request = strings.Replace(request, name, body, -1)
114-
}
143+
115144
}
116145

117146
return request
@@ -125,3 +154,12 @@ func removeQuery(request string) string {
125154
request = queryStartRegex.ReplaceAllString(request, "")
126155
return queryEndRegex.ReplaceAllString(request, "")
127156
}
157+
158+
func appendIfMissing(tree []string, fieldToAppend string) []string {
159+
for _, field := range tree {
160+
if field == fieldToAppend {
161+
return tree
162+
}
163+
}
164+
return append(tree, fieldToAppend)
165+
}

field.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func nameFromResolver(resolver interface{}) string {
4444
fieldType, _ := reflect.TypeOf(
4545
resolver).Elem().FieldByName("Field")
4646

47-
fieldName = fieldType.Tag.Get("name")
47+
fieldName = fieldType.Tag.Get("graphql")
4848
}
4949

5050
return fieldName

field_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ import (
66
)
77

88
type ResolverA struct {
9-
Field Field `name:"a"`
9+
Field Field `graphql:"a"`
1010
}
1111

1212
type ResolverB struct {
13-
Field Field `name:"b"`
13+
Field Field `graphql:"b"`
1414
}
1515

1616
type ResolverC struct {
17-
Field Field `name:"c"`
17+
Field Field `graphql:"c"`
1818
}
1919

2020
type ResolverD struct {
21-
Field Field `name:"d"`
21+
Field Field `graphql:"d"`
2222
}
2323

2424
func TestField(t *testing.T) {

regex.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const (
1212
aliasRegexString = `.*?:`
1313
spacesRegexString = `\s{1,}`
1414

15-
fragmentsRegexString = `(?m)fragment(.|\n)*?}`
15+
fragmentsRegexString = `(?m)fragment(.|\n)*?}`
16+
partialFragmentRegexString = `\.\.\.`
1617

1718
fragmentNameStartRegexString = `(?m)fragment(\s|\n){1,}`
1819
fragmentNameEndRegexString = `(?m)\s{1,}on(.|\n)*`
@@ -34,7 +35,8 @@ var (
3435
aliasRegex = regexp.MustCompile(aliasRegexString)
3536
spacesRegex = regexp.MustCompile(spacesRegexString)
3637

37-
fragmentsRegex = regexp.MustCompile(fragmentsRegexString)
38+
fragmentsRegex = regexp.MustCompile(fragmentsRegexString)
39+
partialFragmentRegex = regexp.MustCompile(partialFragmentRegexString)
3840

3941
fragmentNameStartRegex = regexp.MustCompile(fragmentNameStartRegexString)
4042
fragmentNameEndRegex = regexp.MustCompile(fragmentNameEndRegexString)

requested_fields_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ query {
2020
`
2121

2222
type QueryResolver struct {
23-
Field Field `name:"query"`
23+
Field Field `graphql:"query"`
2424
}
2525

2626
type SearchResolver struct {
27-
Field Field `name:"search"`
27+
Field Field `graphql:"search"`
2828
}
2929

3030
type ProductsResolver struct {
31-
Field Field `name:"products"`
31+
Field Field `graphql:"products"`
3232
}
3333

3434
func TestRequestedFieldsForProducts(t *testing.T) {
@@ -69,7 +69,7 @@ var graphql_query_user string = `
6969
`
7070

7171
type UserResolver struct {
72-
Field Field `name:"user"`
72+
Field Field `graphql:"user"`
7373
}
7474

7575
func TestRequestedFieldsForUser(t *testing.T) {

sort_by_length.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package fields
2+
3+
type byLength []string
4+
5+
func (s byLength) Len() int {
6+
return len(s)
7+
}
8+
func (s byLength) Swap(i, j int) {
9+
s[i], s[j] = s[j], s[i]
10+
}
11+
func (s byLength) Less(i, j int) bool {
12+
return len(s[i]) > len(s[j])
13+
}

0 commit comments

Comments
 (0)