-
Notifications
You must be signed in to change notification settings - Fork 91
/
Kotlin2Core.kt
189 lines (158 loc) · 5.6 KB
/
Kotlin2Core.kt
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
*
* Copyright 2020 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.netflix.graphql.dgs.codegen
import com.netflix.graphql.dgs.client.codegen.InputValue
import com.netflix.graphql.dgs.client.codegen.InputValueSerializer
import com.netflix.graphql.dgs.client.codegen.InputValueSerializerInterface
import graphql.language.Argument
import graphql.language.AstPrinter
import graphql.language.Document
import graphql.language.Field
import graphql.language.InlineFragment
import graphql.language.OperationDefinition
import graphql.language.SelectionSet
import graphql.language.TypeName
@DslMarker
annotation class QueryProjectionMarker
object DefaultTracker {
// set of what defaults have been used
val defaults: ThreadLocal<MutableMap<String, MutableSet<String>>> = ThreadLocal.withInitial { mutableMapOf() }
// add a default value
fun add(type: String, arg: String) {
defaults.get()
.computeIfAbsent(type) { mutableSetOf() }
.add(arg)
}
// consume the set defaults & reset them
fun getAndClear(type: String): Set<String> {
return defaults.get().remove(type) ?: emptySet()
}
}
@QueryProjectionMarker
abstract class GraphQLProjection(
protected val inputValueSerializer: InputValueSerializerInterface? = null,
defaultFields: Set<String> = setOf("__typename")
) {
companion object {
val defaultInputValueSerializer = InputValueSerializer()
@JvmStatic
protected fun <T> default0(arg: String): T? {
DefaultTracker.add(this::class.qualifiedName!!, arg)
return null
}
@JvmStatic
protected inline fun <reified TClass, reified TValue> default(arg: String): TValue? {
DefaultTracker.add(TClass::class.qualifiedName!!, arg)
return null
}
fun <T : GraphQLProjection> asQuery(
operation: OperationDefinition.Operation,
projection: T,
projectionFields: T.() -> T
): String {
projectionFields.invoke(projection)
val document = Document.newDocument()
.definition(
OperationDefinition.newOperationDefinition()
.operation(operation)
.selectionSet(projection.builder.build())
.build()
)
.build()
return AstPrinter.printAst(document)
}
}
private val builder = SelectionSet.newSelectionSet()
init {
// add default fields
defaultFields.forEach { field(it) }
}
private fun arguments(
args: List<Pair<String, Any?>>,
defaults: Set<String>
): List<Argument> {
return args
.filter { (k, _) -> !defaults.contains(k) }
.map { (arg, value) ->
Argument.newArgument()
.name(arg)
.value((inputValueSerializer ?: defaultInputValueSerializer).toValue(value))
.build()
}
}
protected fun field(
name: String,
vararg args: Pair<String, Any?>
) {
val defaults = DefaultTracker.getAndClear(this::class.qualifiedName!!)
builder.selection(
Field.newField()
.name(name)
.arguments(arguments(args.toList(), defaults))
.build()
)
}
protected fun <T : GraphQLProjection> field(
alias: String? = null,
name: String,
projection: T,
projectionFields: T.() -> T,
vararg args: Pair<String, Any?>
) {
val defaults = DefaultTracker.getAndClear(this::class.qualifiedName!!)
projectionFields.invoke(projection)
val fieldBuilder = Field.newField()
.name(name)
.arguments(arguments(args.toList(), defaults))
.selectionSet(projection.builder.build())
alias?.also { fieldBuilder.alias(it) }
builder.selection(fieldBuilder.build())
}
protected fun <T : GraphQLProjection> fragment(
name: String,
projection: T,
projectionFields: T.() -> T
) {
projectionFields.invoke(projection)
builder.selection(
InlineFragment.newInlineFragment()
.typeCondition(TypeName(name))
.selectionSet(projection.builder.build())
.build()
)
}
}
abstract class GraphQLInput : InputValue {
companion object {
@JvmStatic
protected fun <T> default0(arg: String): T? {
DefaultTracker.add(this::class.qualifiedName!!, arg)
return null
}
@JvmStatic
protected inline fun <reified TClass, reified TValue> default(arg: String): TValue? {
DefaultTracker.add(TClass::class.qualifiedName!!, arg)
return null
}
}
private val defaults = DefaultTracker.getAndClear(this::class.qualifiedName!!)
abstract fun fields(): List<Pair<String, Any?>>
override fun inputValues(): List<Pair<String, Any?>> {
return fields().filter { (k, _) -> !defaults.contains(k) }
}
}