typeReference static method

Map<String, Object?> typeReference(
  1. String type
)

An SDL type string as an introspection type reference.

[User!]! becomes NON_NULL(LIST(NON_NULL(OBJECT User))) — the nesting clients rely on to know what can be null.

Implementation

static Map<String, Object?> typeReference(String type) {
  final trimmed = type.trim();
  if (trimmed.endsWith('!')) {
    return <String, Object?>{
      'kind': 'NON_NULL',
      'name': null,
      'ofType': typeReference(trimmed.substring(0, trimmed.length - 1)),
    };
  }
  if (trimmed.startsWith('[') && trimmed.endsWith(']')) {
    return <String, Object?>{
      'kind': 'LIST',
      'name': null,
      'ofType': typeReference(trimmed.substring(1, trimmed.length - 1)),
    };
  }
  return <String, Object?>{
    'kind': _types.containsKey(trimmed) ? 'OBJECT' : 'SCALAR',
    'name': trimmed,
    'ofType': null,
  };
}