/
query.go
142 lines (124 loc) · 3.39 KB
/
query.go
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
package parser
import (
"github.com/codebdy/entify-core/model/graph"
"github.com/codebdy/entify-core/model/meta"
"github.com/codebdy/entify-graphql-schema/consts"
"github.com/codebdy/entify-graphql-schema/resolve"
"github.com/graphql-go/graphql"
)
// func (p *ModelParser) ClassListType(cls *graph.Class) *graphql.Object {
// name := cls.Name()
// listName := cls.ListName()
// if p.listMap[listName] != nil {
// return p.listMap[listName]
// }
// returnValue := graphql.NewObject(
// graphql.ObjectConfig{
// Name: listName,
// Fields: graphql.Fields{
// consts.NODES: &graphql.Field{
// Type: &graphql.List{
// OfType: p.OutputType(name),
// },
// },
// consts.TOTAL: &graphql.Field{
// Type: graphql.Int,
// },
// },
// },
// )
// p.listMap[listName] = returnValue
// return returnValue
// }
func (p *ModelParser) EntityListType(entity *graph.Entity) *graphql.Object {
name := entity.Name()
listName := entity.ListName()
if p.listMap[listName] != nil {
return p.listMap[listName]
}
returnValue := graphql.NewObject(
graphql.ObjectConfig{
Name: listName,
Fields: graphql.Fields{
consts.NODES: &graphql.Field{
Type: &graphql.List{
OfType: p.OutputType(name),
},
},
consts.TOTAL: &graphql.Field{
Type: graphql.Int,
},
consts.AGGREGATE: &graphql.Field{
Type: p.aggregateType(entity),
},
},
},
)
p.listMap[listName] = returnValue
return returnValue
}
func (p *ModelParser) makeEntityOutputObjects(entities []*graph.Entity) {
for i := range entities {
p.makeEntityObject(entities[i])
}
}
func (p *ModelParser) makeEntityObject(entity *graph.Entity) {
objType := p.ObjectType(entity)
p.objectTypeMap[entity.Name()] = objType
p.objectMapById[entity.InnerId()] = objType
}
// func (p *ModelParser) makeThirdPartyOutputObjects(thirds []*graph.ThirdParty) {
// for i := range thirds {
// p.makeThirdPartyObject(thirds[i])
// }
// }
// func (p *ModelParser) makeThirdPartyObject(third *graph.ThirdParty) {
// objType := graphql.NewObject(
// graphql.ObjectConfig{
// Name: third.Name(),
// Fields: p.OutputFields(third.Attributes()),
// Description: third.Description(),
// },
// )
// p.objectTypeMap[third.Name()] = objType
// p.objectMapById[third.InnerId()] = objType
// }
func (p *ModelParser) ObjectType(entity *graph.Entity) *graphql.Object {
name := entity.Name()
interfaces := p.mapInterfaces(entity.Interfaces)
if len(interfaces) > 0 {
return graphql.NewObject(
graphql.ObjectConfig{
Name: name,
Fields: p.OutputFields(entity.AllAttributes()),
Description: entity.Description(),
Interfaces: interfaces,
},
)
} else {
return graphql.NewObject(
graphql.ObjectConfig{
Name: name,
Fields: p.OutputFields(entity.AllAttributes()),
Description: entity.Description(),
},
)
}
}
func (p *ModelParser) OutputFields(attrs []*graph.Attribute) graphql.Fields {
fields := graphql.Fields{}
for _, attr := range attrs {
if attr.Type != meta.PASSWORD {
fields[attr.Name] = &graphql.Field{
Type: PropertyType(attr.GetType()),
Description: attr.Description,
Resolve: resolve.AttributeResolveFn(attr, p.model),
// Resolve: func(p graphql.ResolveParams) (interface{}, error) {
// fmt.Println(p.Context.Value("data"))
// return "world", nil
// },
}
}
}
return fields
}