Skip to content

Commit 0110f66

Browse files
author
Håkan Rosenhorn
committed
New attempt with the Type system and Schema definition
1 parent 49dd0d8 commit 0110f66

File tree

15 files changed

+185
-315
lines changed

15 files changed

+185
-315
lines changed

src/main/scala/se/uprise/graphql/execution/Executor.scala

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package se.uprise.graphql.execution
22

33
import org.antlr.v4.runtime.ParserRuleContext
44
import se.uprise.graphql.error.{GraphQLError, GraphQLFormattedError}
5-
import se.uprise.graphql.types.{GraphQLObjectType, GraphQLSchema}
5+
import se.uprise.graphql.types._
66
import se.uprise.parser.GraphQlParser
77
import se.uprise.parser.GraphQlParser._
88
import scala.collection.JavaConversions._
@@ -75,20 +75,21 @@ object Executor {
7575
* for "read" mode.
7676
*/
7777
// FIXME: Better type than Any
78+
// FIXME: Support for futures
7879
def executeFields(exeContext: ExecutionContext,
79-
parentType: GraphQLObjectType,
80+
parentType: Class[_ <: GraphQLObjectType],
8081
source: Any,
8182
fields: Map[String, List[SelectionContext]]): Any = {
8283

84+
val finalResults = fields.keys.reduceLeft((results, responseName) => {
85+
val fieldASTs = fields(responseName)
86+
val result = resolveField(exeContext, parentType, source, fieldASTs)
87+
""
88+
})
8389

84-
// fields.keys.reduceLeft((results, responseName) => {
85-
// val fieldASTs = fields(responseName)
86-
// val result = resolveField(exeContext, parentType, source, fieldASTs)
87-
// })
88-
90+
finalResults
8991
//selectionSet.selection().foldLeft(fields)((result, selection) =>
9092
true
91-
9293
}
9394

9495
/**
@@ -99,20 +100,46 @@ object Executor {
99100
*/
100101
// FIXME: Better return type
101102
def resolveField(exeContext: ExecutionContext,
102-
parentType: GraphQLObjectType,
103+
parentType: Class[_ <: GraphQLObjectType],
103104
source: Any,
104105
fieldASTs: List[SelectionContext]): Any = {
106+
val fieldAST = fieldASTs.head
107+
val fieldDef = getFieldDef(exeContext.schema, parentType, fieldAST)
108+
109+
105110
//parentType.
106111

107112
}
108113

114+
/**
115+
* This method looks up the field on the given type defintion.
116+
* It has special casing for the two introspection fields, __schema
117+
* and __typename. __typename is special because it can always be
118+
* queried as a field, even in situations where no other fields
119+
* are allowed, like on a Union. __schema could get automatically
120+
* added to the query type, but that would require mutating type
121+
* definitions, which would cause issues.
122+
*/
123+
124+
def getFieldDef(schema: GraphQLSchema,
125+
parentType: Class[_ <: GraphQLObjectType],
126+
fieldAST: SelectionContext): GraphQLOutputType = {
127+
val name = fieldAST.field().fieldName().NAME().getText
128+
129+
//FIXME: Implement support for the introspection
130+
131+
//parentType.getFields(name)
132+
// FIXME
133+
null
134+
}
135+
109136

110137
/**
111138
* Given a selectionSet, adds all of the fields in that selection to
112139
* the passed in map of fields, and returns it at the end.
113140
*/
114141
def collectFields(exeContext: ExecutionContext,
115-
typ: GraphQLObjectType,
142+
typ: Class[_ <: GraphQLObjectType],
116143
selectionSet: SelectionSetContext,
117144
fields: Map[String, List[SelectionContext]] = Map.empty,
118145
visitedFragmentNames: Map[String, Boolean] = Map.empty): Map[String, List[SelectionContext]] = {
@@ -173,7 +200,7 @@ object Executor {
173200
// FIXME: Implement properly
174201
def doesFragmentConditionMatch(exeContext: ExecutionContext,
175202
fragment: ParserRuleContext,
176-
typ: GraphQLObjectType): Boolean = {
203+
typ: Class[_ <: GraphQLObjectType]): Boolean = {
177204
return true
178205
}
179206

@@ -203,7 +230,7 @@ object Executor {
203230
* Extracts the root type of the operation from the schema.
204231
*/
205232
def getOperationRootType(schema: GraphQLSchema,
206-
operation: OperationDefinitionContext): GraphQLObjectType = {
233+
operation: OperationDefinitionContext): Class[_ <: GraphQLObjectType] = {
207234
operation.operationType().getText match {
208235
case "query" => schema.query
209236
case "mutation" => schema.mutation match {
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
package se.uprise.graphql.types
22

3-
trait GraphQLEnumType extends GraphQLOutputType with GraphQLMetadata {
4-
protected def enum(name: String, value: Any, description: String = null, deprecationReason: String = null) = {}
5-
}
3+
trait GraphQLEnumType extends GraphQLOutputType

src/main/scala/se/uprise/graphql/types/GraphQLFieldDefinition.scala

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
package se.uprise.graphql.types
22

3-
trait GraphQLInputType
3+
/**
4+
* These types may be used as input types for arguments and directives.
5+
*/
6+
trait GraphQLInputType extends GraphQLType
7+
8+
/*
9+
GraphQLScalarType |
10+
GraphQLEnumType |
11+
GraphQLInputObjectType |
12+
GraphQLList |
13+
GraphQLNonNull;
14+
*/
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
package se.uprise.graphql.types
22

3-
trait GraphQLInterfaceType extends GraphQLObjectType with GraphQLMetadata with GraphQLFields {
4-
def resolveType(value: Any): Class[_ <: GraphQLObjectType] = {
5-
// FIXME: Try default resolve with getTypeOf
6-
null
7-
}
8-
}
3+
trait GraphQLInterfaceType extends GraphQLOutputType {
4+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
package se.uprise.graphql.types
22

3-
class GraphQLList[T <: GraphQLOutputType] extends GraphQLOutputType
3+
class GraphQLList[T <: GraphQLType](input: List[T]) extends GraphQLOutputType with GraphQLInputType {
4+
5+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package se.uprise.graphql.types
22

3-
class GraphQLNonNull[T <: GraphQLOutputType] extends GraphQLOutputType
3+
trait GraphQLNonNull extends GraphQLOutputType
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package se.uprise.graphql.types
22

3-
trait GraphQLObjectType extends GraphQLOutputType with GraphQLMetadata with GraphQLFields {
4-
def interfaces: List[Class[_ <: GraphQLInterfaceType]] = List()
3+
trait GraphQLObjectType extends GraphQLOutputType {
4+
55
}
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
package se.uprise.graphql.types
22

33
/**
4-
* Created by Håkan Rosenhorn on 2015-07-09.
4+
* These types may be used as output types as the result of fields.
55
*/
6-
trait GraphQLOutputType
6+
trait GraphQLOutputType extends GraphQLType
7+
/*
8+
GraphQLScalarType |
9+
GraphQLObjectType |
10+
GraphQLInterfaceType |
11+
GraphQLUnionType |
12+
GraphQLEnumType |
13+
GraphQLList |
14+
GraphQLNonNull;
15+
*/
Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,10 @@
11
package se.uprise.graphql.types
22

3-
trait GraphQLScalarType extends GraphQLOutputType {
3+
trait GraphQLScalarType extends GraphQLOutputType with GraphQLInputType {
44
val name: String = ""
55
val description: String = ""
6-
def coerce(value: String): Boolean = true
7-
def coerceLiteral(value: String): String = "Todo"
86
}
97

10-
class GraphQLString extends GraphQLScalarType {
8+
case class GraphQLString(value: String) extends GraphQLScalarType {
119
override val name: String = "String"
12-
}
13-
14-
class GraphQLBoolean extends GraphQLScalarType {
15-
override val name: String = "Boolean"
16-
}
17-
18-
class GraphQLID extends GraphQLScalarType {
19-
override val name: String = "ID"
20-
}
21-
22-
class GraphQLFloat extends GraphQLScalarType {
23-
override val name: String = "Float"
24-
}
25-
26-
// Integers are only safe when between -(2^53 - 1) and 2^53 - 1 due to being
27-
// encoded in JavaScript and represented in JSON as double-precision floating
28-
// point numbers, as specified by IEEE 754.
29-
30-
class GraphQLInt extends GraphQLScalarType {
31-
override val name: String = "Int"
32-
33-
val MAX_INT = 9007199254740991L
34-
val MIN_INT = -9007199254740991L
35-
}
10+
}

0 commit comments

Comments
 (0)