|
| 1 | +package se.uprise.graphql.execution |
| 2 | + |
| 3 | +import se.uprise.graphql.error.{GraphQLError, GraphQLFormattedError} |
| 4 | +import se.uprise.graphql.types.GraphQLSchema |
| 5 | +import se.uprise.parser.GraphQlParser |
| 6 | +import se.uprise.parser.GraphQlParser._ |
| 7 | +import scala.collection.JavaConversions._ |
| 8 | + |
| 9 | +/** |
| 10 | + * Terminology |
| 11 | + * |
| 12 | + * "Definitions" are the generic name for top-level statements in the document. |
| 13 | + * Examples of this include: |
| 14 | + * 1) Operations (such as a query) |
| 15 | + * 2) Fragments |
| 16 | + * |
| 17 | + * "Operations" are a generic name for requests in the document. |
| 18 | + * Examples of this include: |
| 19 | + * 1) query, |
| 20 | + * 2) mutation |
| 21 | + * |
| 22 | + * "Selections" are the statements that can appear legally and at |
| 23 | + * single level of the query. These include: |
| 24 | + * 1) field references e.g "a" |
| 25 | + * 2) fragment "spreads" e.g. "...c" |
| 26 | + * 3) inline fragment "spreads" e.g. "...on Type { a }" |
| 27 | + */ |
| 28 | + |
| 29 | + |
| 30 | +/** |
| 31 | + * The result of execution. `data` is the result of executing the |
| 32 | + * query, `errors` is null if no errors occurred, and is a |
| 33 | + * non-empty array if an error occurred. |
| 34 | + */ |
| 35 | +// FIXME: root should not be type Any |
| 36 | +case class ExecutionResult(data: Any, errors: List[GraphQLFormattedError]) |
| 37 | + |
| 38 | + |
| 39 | +object Executor { |
| 40 | + def apply(schema: GraphQLSchema, |
| 41 | + root: Any, |
| 42 | + ast: DocumentContext, |
| 43 | + operationName: String, |
| 44 | + variableValues: Map[String, String] = Map.empty): ExecutionResult = { |
| 45 | + |
| 46 | + // FIXME: We cannot pass a immutable List of errors in the execution context |
| 47 | + val exeContext = buildExecutionContext(schema, root, ast, operationName, variableValues, List.empty) |
| 48 | + |
| 49 | + // FIXME: Result is currently Any |
| 50 | + val result = executeOperation(exeContext, root, exeContext.operation) |
| 51 | + |
| 52 | + new ExecutionResult("FIXME", List.empty) |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Implements the "Evaluating operations" section of the spec. |
| 57 | + */ |
| 58 | + def executeOperation(exeContext: ExecutionContext, |
| 59 | + root: Any, |
| 60 | + operation: OperationDefinitionContext): Any = { |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + // FIXME: root should perhaps not be type Any |
| 66 | + def buildExecutionContext(schema: GraphQLSchema, |
| 67 | + root: Any, |
| 68 | + ast: DocumentContext, |
| 69 | + operationName: String, |
| 70 | + variableValues: Map[String, String] = Map.empty, |
| 71 | + errors: List[Exception]): ExecutionContext = { |
| 72 | + |
| 73 | + // Convert from Java to Scala |
| 74 | + val definitionSet: List[DefinitionContext] = ast.definition().toList |
| 75 | + |
| 76 | + // FIXME: Might be improved... |
| 77 | + val fragments: Map[String, FragmentDefinitionContext] = definitionSet flatMap { entry => |
| 78 | + entry.getRuleIndex match { |
| 79 | + case GraphQlParser.RULE_fragmentDefinition => |
| 80 | + val fragment = entry.asInstanceOf[FragmentDefinitionContext] |
| 81 | + Some(fragment.fragmentName().NAME().getText -> fragment) |
| 82 | + case _ => None |
| 83 | + } |
| 84 | + } toMap |
| 85 | + |
| 86 | + // FIXME: Might be improved... |
| 87 | + val operations: Map[String, OperationDefinitionContext] = definitionSet flatMap { entry => |
| 88 | + entry.getRuleIndex match { |
| 89 | + case GraphQlParser.RULE_operationDefinition => |
| 90 | + val operation = entry.asInstanceOf[OperationDefinitionContext] |
| 91 | + Some(operation.NAME().getText -> operation) |
| 92 | + case _ => None |
| 93 | + } |
| 94 | + } toMap |
| 95 | + |
| 96 | + if (operationName.length > 0 && operations.size != 1) { |
| 97 | + throw new GraphQLError("Must provide operation name if query contains multiple operations") |
| 98 | + } |
| 99 | + |
| 100 | + val opName = operationName match { |
| 101 | + case name if name.length > 0 => name |
| 102 | + case _ => operations.head._1 |
| 103 | + } |
| 104 | + |
| 105 | + val operation = operations.get(opName) match { |
| 106 | + case Some(entry) => entry |
| 107 | + case None => throw new GraphQLError("Unknown operation name: " + opName) |
| 108 | + } |
| 109 | + |
| 110 | + val variables = getVariableValues(schema, |
| 111 | + operation.variableDefinitions().variableDefinition().toList, |
| 112 | + variableValues |
| 113 | + ) |
| 114 | + |
| 115 | + ExecutionContext(schema, fragments, root, operation, variables, errors) |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + /** |
| 120 | + * Prepares an object map of variables of the correct type based on the provided |
| 121 | + * variable definitions and arbitrary input. If the input cannot be coerced |
| 122 | + * to match the variable definitions, a GraphQLError will be thrown. |
| 123 | + */ |
| 124 | + /* |
| 125 | + FIXME: Replace Any types with proper Types resolved from the variableValues |
| 126 | + Ex: query Example($size: Int) { |
| 127 | + variableValues = { size: 100 } |
| 128 | + */ |
| 129 | + |
| 130 | + def getVariableValues(schema: GraphQLSchema, |
| 131 | + definitionASTs: List[VariableDefinitionContext] = List.empty, |
| 132 | + variableValues: Map[String, String] = Map.empty): Map[String, Any] = { |
| 133 | + |
| 134 | + |
| 135 | + // FIXME: Implement |
| 136 | +// definitionASTs map { definition => |
| 137 | +// val varName = definition.variable().NAME().getText |
| 138 | +// } |
| 139 | + |
| 140 | + Map.empty |
| 141 | + } |
| 142 | + |
| 143 | + def getVariableValue(schema: GraphQLSchema, |
| 144 | + definitionAST: VariableDefinitionContext, |
| 145 | + input: String) = { |
| 146 | + |
| 147 | + } |
| 148 | +} |
0 commit comments