Module: GraphQL::Introspection

Defined in:
lib/graphql/introspection.rb,
lib/graphql/introspection/schema_field.rb,
lib/graphql/introspection/typename_field.rb,
lib/graphql/introspection/type_by_name_field.rb

Defined Under Namespace

Classes: SchemaField, TypeByNameField, TypenameField

Constant Summary collapse

TypeType =
GraphQL::ObjectType.define do
  name "__Type"
  description "The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in "\
              "GraphQL as represented by the `__TypeKind` enum.\n\n"\
              "Depending on the kind of a type, certain fields describe information about that type. "\
              "Scalar types provide no information beyond a name and description, while "\
              "Enum types provide their values. Object and Interface types provide the fields "\
              "they describe. Abstract types, Union and Interface, provide the Object types "\
              "possible at runtime. List and NonNull types compose other types."

  field :kind do
    type !GraphQL::Introspection::TypeKindEnum
    resolve ->(target, a, c) { target.kind.name }
  end
  field :name, types.String
  field :description, types.String
  field :fields,          field: GraphQL::Introspection::FieldsField
  field :interfaces,      field: GraphQL::Introspection::InterfacesField
  field :possibleTypes,   field: GraphQL::Introspection::PossibleTypesField
  field :enumValues,      field: GraphQL::Introspection::EnumValuesField
  field :inputFields,     field: GraphQL::Introspection::InputFieldsField
  field :ofType,          field: GraphQL::Introspection::OfTypeField
end
FieldType =
GraphQL::ObjectType.define do
  name "__Field"
  description "Object and Interface types are described by a list of Fields, each of which has "\
              "a name, potentially a list of arguments, and a return type."
  field :name, !types.String
  field :description, types.String
  field :args, GraphQL::Introspection::ArgumentsField
  field :type, !GraphQL::Introspection::TypeType
  field :isDeprecated, !types.Boolean do
    resolve ->(obj, a, c) { !!obj.deprecation_reason }
  end
  field :deprecationReason, types.String, property: :deprecation_reason
end
SchemaType =
GraphQL::ObjectType.define do
  name "__Schema"
  description "A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all "\
              "available types and directives on the server, as well as the entry points for "\
              "query, mutation, and subscription operations."

  field :types, !types[!GraphQL::Introspection::TypeType], "A list of all types supported by this server." do
    resolve ->(obj, arg, ctx) { obj.types.values }
  end

  field :queryType, !GraphQL::Introspection::TypeType, "The type that query operations will be rooted at." do
    resolve ->(obj, arg, ctx) { obj.query }
  end

  field :mutationType, GraphQL::Introspection::TypeType, "If this server supports mutation, the type that mutation operations will be rooted at." do
    resolve ->(obj, arg, ctx) { obj.mutation }
  end

  field :subscriptionType, GraphQL::Introspection::TypeType, "If this server support subscription, the type that subscription operations will be rooted at." do
    resolve ->(obj, arg, ctx) { obj.subscription }
  end

  field :directives, !types[!GraphQL::Introspection::DirectiveType], "A list of all directives supported by this server." do
    resolve ->(obj, arg, ctx) { obj.directives.values }
  end
end
FieldsField =
GraphQL::Field.define do
  type -> { types[!GraphQL::Introspection::FieldType] }
  argument :includeDeprecated, GraphQL::BOOLEAN_TYPE, default_value: false
  resolve ->(object, arguments, context) {
    return nil if !object.kind.fields?
    fields = object.all_fields
    if !arguments["includeDeprecated"]
      fields = fields.select {|f| !f.deprecation_reason }
    end
    fields.sort_by(&:name)
  }
end
OfTypeField =
GraphQL::Field.define do
  name "ofType"
  type -> { GraphQL::Introspection::TypeType }
  resolve ->(obj, args, ctx) { obj.kind.wraps? ? obj.of_type : nil }
end
DirectiveType =
GraphQL::ObjectType.define do
  name "__Directive"
  description "A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document."\
              "\n\n"\
              "In some cases, you need to provide options to alter GraphQL's execution behavior "\
              "in ways field arguments will not suffice, such as conditionally including or "\
              "skipping a field. Directives provide this by describing additional information "\
              "to the executor."
  field :name, !types.String
  field :description, types.String
  field :locations, !types[!GraphQL::Introspection::DirectiveLocationEnum]
  field :args, field: GraphQL::Introspection::ArgumentsField
  field :onOperation, !types.Boolean, deprecation_reason: "Use `locations`.", property: :on_operation?
  field :onFragment, !types.Boolean, deprecation_reason: "Use `locations`.", property: :on_fragment?
  field :onField, !types.Boolean, deprecation_reason: "Use `locations`.", property: :on_field?
end
TypeKindEnum =
GraphQL::EnumType.define do
  name "__TypeKind"
  description "An enum describing what kind of type a given `__Type` is."
  GraphQL::TypeKinds::TYPE_KINDS.each do |type_kind|
    value(type_kind.name, type_kind.description)
  end
end
ArgumentsField =
GraphQL::Field.define do
  type !GraphQL::ListType.new(of_type: !GraphQL::Introspection::InputValueType)
  resolve ->(target, a, c) { target.arguments.values }
end
EnumValueType =
GraphQL::ObjectType.define do
  name "__EnumValue"
  description "One possible value for a given Enum. Enum values are unique values, not a "\
              "placeholder for a string or numeric value. However an Enum value is returned in "\
              "a JSON response as a string."
  field :name, !types.String
  field :description, types.String
  field :isDeprecated, !types.Boolean do
    resolve ->(obj, a, c) { !!obj.deprecation_reason }
  end
  field :deprecationReason, types.String, property: :deprecation_reason
end
InputValueType =
GraphQL::ObjectType.define do
  name "__InputValue"
  description "Arguments provided to Fields or Directives and the input fields of an "\
              "InputObject are represented as Input Values which describe their type and "\
              "optionally a default value."
  field :name, !types.String
  field :description, types.String
  field :type, !GraphQL::Introspection::TypeType
  field :defaultValue, types.String, "A GraphQL-formatted string representing the default value for this input value." do
    resolve ->(obj, args, ctx) {
      value = obj.default_value
      value.nil? ? nil : JSON.generate(obj.type.coerce_result(value), quirks_mode: true)
    }
  end
end
InterfacesField =
GraphQL::Field.define do
  type -> { types[!GraphQL::Introspection::TypeType] }
  resolve ->(target, a, c) { target.kind.object? ? target.interfaces : nil }
end
EnumValuesField =
GraphQL::Field.define do
  type types[!GraphQL::Introspection::EnumValueType]
  argument :includeDeprecated, types.Boolean, default_value: false
  resolve ->(object, arguments, context) do
    return nil if !object.kind.enum?
    fields = object.values.values
    if !arguments["includeDeprecated"]
      fields = fields.select {|f| !f.deprecation_reason }
    end
    fields
  end
end
InputFieldsField =
GraphQL::Field.define do
  name "inputFields"
  type types[!GraphQL::Introspection::InputValueType]
  resolve ->(target, a, c) {
    if target.kind.input_object?
      target.input_fields.values
    else
      nil
    end
  }
end
INTROSPECTION_QUERY =

The introspection query to end all introspection queries, copied from github.com/graphql/graphql-js/blob/master/src/utilities/introspectionQuery.js

"
query IntrospectionQuery {
  __schema {
    queryType { name }
    mutationType { name }
    subscriptionType { name }
    types {
      ...FullType
    }
    directives {
      name
      description
      locations
      args {
        ...InputValue
      }
    }
  }
}
fragment FullType on __Type {
  kind
  name
  description
  fields(includeDeprecated: true) {
    name
    description
    args {
      ...InputValue
    }
    type {
      ...TypeRef
    }
    isDeprecated
    deprecationReason
  }
  inputFields {
    ...InputValue
  }
  interfaces {
    ...TypeRef
  }
  enumValues(includeDeprecated: true) {
    name
    description
    isDeprecated
    deprecationReason
  }
  possibleTypes {
    ...TypeRef
  }
}
fragment InputValue on __InputValue {
  name
  description
  type { ...TypeRef }
  defaultValue
}
fragment TypeRef on __Type {
  kind
  name
  ofType {
    kind
    name
    ofType {
      kind
      name
      ofType {
        kind
        name
        ofType {
          kind
          name
          ofType {
            kind
            name
            ofType {
              kind
              name
              ofType {
                kind
                name
              }
            }
          }
        }
      }
    }
  }
}
"
PossibleTypesField =
GraphQL::Field.define do
  type -> { types[!GraphQL::Introspection::TypeType] }
  resolve ->(target, args, ctx) {
    if target.kind.resolves?
      ctx.schema.possible_types(target)
    else
      nil
    end
  }
end
DirectiveLocationEnum =
GraphQL::EnumType.define do
  name "__DirectiveLocation"
  description "A Directive can be adjacent to many parts of the GraphQL language, a "\
              "__DirectiveLocation describes one such possible adjacencies."

  GraphQL::Directive::LOCATIONS.each do |location|
    value(location.to_s, GraphQL::Directive::LOCATION_DESCRIPTIONS[location], value: location)
  end
end