query_type_must_be_object.ex (1575B)
1 defmodule Absinthe.Phase.Schema.Validation.QueryTypeMustBeObject do 2 use Absinthe.Phase 3 alias Absinthe.Blueprint 4 5 def run(bp, _) do 6 bp = Blueprint.prewalk(bp, &validate_schemas/1) 7 {:ok, bp} 8 end 9 10 defp validate_schemas(%Blueprint.Schema.SchemaDefinition{} = schema) do 11 case Enum.find( 12 schema.type_definitions, 13 &match?(%Blueprint.Schema.ObjectTypeDefinition{identifier: :query}, &1) 14 ) do 15 nil -> 16 schema |> put_error(error(schema)) 17 18 _ -> 19 schema 20 end 21 end 22 23 defp validate_schemas(node), do: node 24 25 defp error(schema) do 26 %Absinthe.Phase.Error{ 27 message: explanation(nil), 28 locations: [schema.__reference__.location], 29 phase: __MODULE__ 30 } 31 end 32 33 @moduledoc false 34 35 @description """ 36 37 # Example 38 defmodule MyApp.Schema do 39 use Absinthe.Schema 40 41 query do 42 # Fields go here 43 end 44 end 45 46 -------------------------------------- 47 From the graqhql schema specification 48 49 A GraphQL schema includes types, indicating where query and mutation 50 operations start. This provides the initial entry points into the type system. 51 The query type must always be provided, and is an Object base type. The 52 mutation type is optional; if it is null, that means the system does not 53 support mutations. If it is provided, it must be an object base type. 54 55 Reference: https://facebook.github.io/graphql/#sec-Initial-types 56 """ 57 58 def explanation(_value) do 59 """ 60 The root query type must be implemented and be of type Object 61 62 #{@description} 63 """ 64 end 65 end