|
| 1 | +defmodule GraphQL.Execution.Executor do |
| 2 | + @moduledoc ~S""" |
| 3 | + Execute a GraphQL query against a given schema / datastore. |
| 4 | +
|
| 5 | + # iex> GraphQL.execute schema, "{ hello }" |
| 6 | + # {:ok, %{hello: "world"}} |
| 7 | + """ |
| 8 | + |
| 9 | + @doc """ |
| 10 | + Execute a query against a schema. |
| 11 | +
|
| 12 | + # iex> GraphQL.execute(schema, "{ hello }") |
| 13 | + # {:ok, %{hello: world}} |
| 14 | + """ |
| 15 | + def execute(schema, document, root_value \\ %{}, variable_values \\ %{}, operation_name \\ nil) do |
| 16 | + context = build_execution_context(schema, document, root_value, variable_values, operation_name) |
| 17 | + {:ok, {data, _errors}} = execute_operation(context, context.operation, root_value) |
| 18 | + {:ok, data} |
| 19 | + end |
| 20 | + |
| 21 | + defp build_execution_context(schema, document, root_value, variable_values, operation_name) do |
| 22 | + %{ |
| 23 | + schema: schema, |
| 24 | + fragments: %{}, |
| 25 | + root_value: root_value, |
| 26 | + operation: find_operation(document, operation_name), |
| 27 | + variable_values: variable_values, |
| 28 | + errors: [] |
| 29 | + } |
| 30 | + end |
| 31 | + |
| 32 | + defp execute_operation(context, operation, root_value) do |
| 33 | + type = operation_root_type(context.schema, operation) |
| 34 | + fields = collect_fields(context, type, operation.selectionSet) |
| 35 | + result = case operation.operation do |
| 36 | + :mutation -> execute_fields_serially(context, type, root_value, fields) |
| 37 | + _ -> execute_fields(context, type, root_value, fields) |
| 38 | + end |
| 39 | + {:ok, {result, nil}} |
| 40 | + end |
| 41 | + |
| 42 | + defp find_operation(document, operation_name) do |
| 43 | + if operation_name do |
| 44 | + Enum.find(document.definitions, fn(definition) -> definition.name == operation_name end) |
| 45 | + else |
| 46 | + hd(document.definitions) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + defp operation_root_type(schema, operation) do |
| 51 | + Map.get(schema, operation.operation) |
| 52 | + end |
| 53 | + |
| 54 | + defp collect_fields(_context, _runtime_type, selection_set, fields \\ %{}, _visited_fragment_names \\ %{}) do |
| 55 | + Enum.reduce selection_set[:selections], fields, fn(selection, fields) -> |
| 56 | + case selection do |
| 57 | + %{kind: :Field} -> Map.put(fields, field_entry_key(selection), [selection]) |
| 58 | + _ -> fields |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + # source_value -> root_value? |
| 64 | + defp execute_fields(context, parent_type, source_value, fields) do |
| 65 | + Enum.reduce fields, %{}, fn({field_name, field_asts}, results) -> |
| 66 | + Map.put results, field_name, resolve_field(context, parent_type, source_value, field_asts) |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + defp execute_fields_serially(context, parent_type, source_value, fields) do |
| 71 | + # call execute_fields because no async operations yet |
| 72 | + execute_fields(context, parent_type, source_value, fields) |
| 73 | + end |
| 74 | + |
| 75 | + defp resolve_field(context, parent_type, source, field_asts) do |
| 76 | + field_ast = hd(field_asts) |
| 77 | + field_name = field_ast.name |
| 78 | + field_def = field_definition(context.schema, parent_type, field_name) |
| 79 | + return_type = field_def.type |
| 80 | + |
| 81 | + resolve_fn = Map.get(field_def, :resolve, &default_resolve_fn/3) |
| 82 | + args = argument_values(Map.get(field_def, :args, %{}), Map.get(field_ast, :arguments, %{}), context.variable_values) |
| 83 | + info = %{ |
| 84 | + field_name: field_name, |
| 85 | + field_asts: field_asts, |
| 86 | + return_type: return_type, |
| 87 | + parent_type: parent_type, |
| 88 | + schema: context.schema, |
| 89 | + fragments: context.fragments, |
| 90 | + root_value: context.root_value, |
| 91 | + operation: context.operation, |
| 92 | + variable_values: context.variable_values |
| 93 | + } |
| 94 | + result = resolve_fn.(source, args, info) |
| 95 | + complete_value_catching_error(context, return_type, field_asts, info, result) |
| 96 | + end |
| 97 | + |
| 98 | + defp default_resolve_fn(source, _args, %{field_name: field_name}) do |
| 99 | + source[field_name] |
| 100 | + end |
| 101 | + |
| 102 | + defp complete_value_catching_error(context, return_type, field_asts, info, result) do |
| 103 | + # TODO lots of error checking |
| 104 | + complete_value(context, return_type, field_asts, info, result) |
| 105 | + end |
| 106 | + |
| 107 | + defp complete_value(context, %GraphQL.ObjectType{} = return_type, field_asts, _info, result) do |
| 108 | + sub_field_asts = Enum.reduce field_asts, %{}, fn(field_ast, sub_field_asts) -> |
| 109 | + if selection_set = Map.get(field_ast, :selectionSet) do |
| 110 | + collect_fields(context, return_type, selection_set, sub_field_asts) |
| 111 | + else |
| 112 | + sub_field_asts |
| 113 | + end |
| 114 | + end |
| 115 | + execute_fields(context, return_type, result, sub_field_asts) |
| 116 | + end |
| 117 | + |
| 118 | + defp complete_value(_context, _return_type, _field_asts, _info, result) do |
| 119 | + result |
| 120 | + end |
| 121 | + |
| 122 | + defp field_definition(_schema, parent_type, field_name) do |
| 123 | + # TODO deal with introspection |
| 124 | + parent_type.fields[String.to_atom field_name] |
| 125 | + end |
| 126 | + |
| 127 | + defp argument_values(arg_defs, arg_asts, variable_values) do |
| 128 | + arg_ast_map = Enum.reduce arg_asts, %{}, fn(arg_ast, result) -> |
| 129 | + Map.put(result, String.to_atom(arg_ast.name), arg_ast) |
| 130 | + end |
| 131 | + Enum.reduce arg_defs, %{}, fn(arg_def, result) -> |
| 132 | + {arg_def_name, arg_def_type} = arg_def |
| 133 | + if value_ast = arg_ast_map[arg_def_name] do |
| 134 | + Map.put result, arg_def_name, value_from_ast(value_ast, arg_def_type, variable_values) |
| 135 | + else |
| 136 | + result |
| 137 | + end |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + defp value_from_ast(value_ast, _type, _variable_values) do |
| 142 | + value_ast.value.value |
| 143 | + end |
| 144 | + |
| 145 | + defp field_entry_key(field) do |
| 146 | + Map.get(field, :alias, field.name) |
| 147 | + end |
| 148 | +end |
0 commit comments