Receiving error "fields must be an object with field names as keys"

Created on 4 October 2018, about 6 years ago
Updated 9 August 2024, 4 months ago

Hello,

I am attempting to create a module which defines it's own custom Type and associated Field plugins.

When installed, GraphQLi reports the following error in the console:

Uncaught Error: CustomTypeInterface fields must be an object with field names as keys or a function which returns such an object.

Drupal 8.61. I have tried on both GraphQL 3.0-RC2 and 3.x-Dev. Any help would be much appreciated. Thanks.

My code is as follows:

/graphql_custom.info.yml

name: GraphQL Custom Type Example
type: module
description: ''
package: GraphQL
core: 8.x
dependencies:
  - graphql_core

/src/CustomObject.php

namespace Drupal\graphql_custom;

class CustomObject {
	protected $data;
	
	function __construct(String $data) {
		$this->data = $data;
	}
	
	function getData() {
		return $this->data;
	}
}

/src/Plugin/GraphQL/Fields/CustomField.php

<?php

namespace Drupal\graphql_custom\Plugin\GraphQL\Fields;

use Drupal\graphql_custom\CustomObject;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
use GraphQL\Type\Definition\ResolveInfo;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * Created Custom Object with argument as data.
 *
 * @GraphQLField(
 *   id = "custom_field",
 *   secure = true,
 *   name = "customfield",
 *   type = "CustomType",
 *   nullable = true,
 *   arguments = {
 *     "argument" = "String!"
 *   }
 * )
 */
class CustomField extends FieldPluginBase implements ContainerFactoryPluginInterface {
	/**
	 * {@inheritdoc}
	 */
	public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
		return new static(
			$configuration,
			$plugin_id,
			$plugin_definition
		);
	}
	
	/**
	 * {@inheritdoc}
	 */
	protected function isLanguageAwareField() {
		return FALSE;
	}
	
	/**
	 * {@inheritdoc}
	 */
	public function resolve($value, array $args, ResolveContext $context, ResolveInfo $info) {
		return parent::resolve($value, $args, $context, $info);
	}
  
	/**
	 * {@inheritdoc}
	 */
	public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
		$arg = $args['argument'];
		$object = new CustomObject($arg);
		
		yield $object;
	}
}

/src/Plugin/GraphQL/Fields/CustomFieldData.php

<?php

namespace Drupal\graphql_custom\Plugin\GraphQL\Fields;

use Drupal\graphql_custom\CustomObject;
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use GraphQL\Type\Definition\ResolveInfo;

/**
 * Custom Type Data Field
 *
 * @GraphQLField(
 *   id = "custom_field_data",
 *   secure = true,
 *   name = "data",
 *   type = "String",
 *   parents = {"CustomType"}
 * )
 */
 
class CustomFieldData extends FieldPluginBase {
	/**
	 * {@inheritdoc}
	 */
	protected function resolveValues($value, array $args, $context, $info) {
		if ($value instanceOf CustomObject) {
			yield (string) $value->getData();
		} else {
			yield (string) "Empty";
		}
	}
}

/src/Plugin/GraphQL/Interfaces/CustomTypeInterface.php

<?php

namespace Drupal\graphql_custom\Plugin\GraphQL\Interfaces;

use Drupal\graphql_custom\CustomObject;
use Drupal\graphql\Annotation\GraphQLInterface;
use Drupal\graphql\Plugin\GraphQL\Interfaces\InterfacePluginBase;

/**
 * Interface for Custom Type.
 *
 * For simplicity reasons, this example does not utilize dependency injection.
 *
 * @GraphQLInterface(
 *   id = "custom_type_interface",
 *   name = "CustomTypeInterface"
 * )
 */
 
class CustomTypeInterface extends InterfacePluginBase {
	/**
	 * {@inheritdoc}
	 */
	public function resolveType($object) {
		if ($object instanceof CustomObject) {
			$schemaManager = \Drupal::service('graphql_core.schema_manager');
			
			return $schemaManager->findByName('CustomType', [
					GRAPHQL_CORE_TYPE_PLUGIN,
				]);
		}
	}
}

/src/Plugin/GraphQL/Types/CustomType.php

<?php
namespace Drupal\graphql_custom\Plugin\GraphQL\Types;

use Drupal\graphql_custom\CustomObject;
use Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use GraphQL\Type\Definition\ResolveInfo;

/**
 * GraphQL Custom Type.
 *
 * @GraphQLType(
 *   id = "custom_type",
 *   name = "CustomType",
 *   interfaces = {"CustomTypeInterface"}
 * )
 */
 
class CustomType extends TypePluginBase {
	/**
	 * {@inheritdoc}
	 */
	public function applies($object, ResolveContext $context, ResolveInfo $info) {
		return $object instanceof CustomObject;
	}
}
πŸ’¬ Support request
Status

Closed: outdated

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States bamberjp

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024