You are here

public static function StringHelper::parseType in GraphQL 8.3

Parses a type definition from a string and properly decorates it.

Converts type strings (e.g. [Foo!]) to their object representations.

Parameters

string $type: The type string to parse.

Return value

array The extracted type with the type decorators.

4 calls to StringHelper::parseType()
ArgumentAwarePluginTrait::buildArgumentType in src/Plugin/GraphQL/Traits/ArgumentAwarePluginTrait.php
Builds an argument's type.
InputTypePluginBase::buildFieldType in src/Plugin/GraphQL/InputTypes/InputTypePluginBase.php
Builds a field's type.
IntrospectionTestTrait::assertGraphQLFields in tests/src/Traits/IntrospectionTestTrait.php
Assert certain fields in the GraphQL schema.
TypedPluginTrait::buildType in src/Plugin/GraphQL/Traits/TypedPluginTrait.php

File

src/Utility/StringHelper.php, line 117

Class

StringHelper

Namespace

Drupal\graphql\Utility

Code

public static function parseType($type) {
  $decorators = [];
  $unwrapped = $type;
  $matches = NULL;
  while (preg_match('/[\\[\\]!]/', $unwrapped) && preg_match_all('/^(\\[?)(.*?)(\\]?)(!*?)$/', $unwrapped, $matches)) {
    if ($unwrapped === $matches[2][0] || empty($matches[1][0]) !== empty($matches[3][0])) {
      throw new \InvalidArgumentException(sprintf("Invalid type declaration '%s'.", $type));
    }
    if (!empty($matches[4][0])) {
      array_unshift($decorators, [
        'GraphQL\\Type\\Definition\\Type',
        'nonNull',
      ]);
    }
    if (!empty($matches[1][0]) && !empty($matches[3][0])) {
      array_unshift($decorators, [
        'GraphQL\\Type\\Definition\\Type',
        'listOf',
      ]);
    }
    $unwrapped = $matches[2][0];
  }
  return [
    $unwrapped,
    $decorators,
  ];
}