Skip to content
On this page

Telephone (graphql_telephone)

Adds support for the telephone module.

Make sure to have at least one telephone field enabled in the schema.

The extension adds a parsed field on the FieldItemTypeTelephone type that returns a the parsed number as a ParsedPhoneNumber type. On it are some fields like countryCode or type. In addition you can get the formatted number.

All exceptions during parsing and formatting are caught, so the field will return NULL if the entered number is not valid.

Schema

Base

graphql
enum PhoneNumberFormat {
  """
  Formatted: +41446681800
  """
  E164

  """
  Formatted: +41 44 668 1800
  """
  INTERNATIONAL

  """
  Formatted: 044 668 1800
  """
  NATIONAL

  """
  tel:+41-44-668-1800
  """
  RFC3966
}

enum PhoneNumberType {
  """
  Fixed line.
  """
  FIXED_LINE

  """
  Mobile.
  """
  MOBILE

  """
  In some regions (e.g. the USA), it is impossible to distinguish
  between fixed-line and mobile numbers by looking at the phone
  number itself.
  """
  FIXED_LINE_OR_MOBILE

  """
  Freephone lines.
  """
  TOLL_FREE

  """
  Premium rate.
  """
  PREMIUM_RATE

  """
  The cost of this call is shared between the caller and the
  recipient, and is hence typically less than PREMIUM_RATE calls.
  """
  SHARED_COST

  """
  Voice over IP numbers. This includes TSoIP (Telephony Service
  over IP).
  """
  VOIP

  """
  A personal number is associated with a particular person, and
  may be routed to either a MOBILE or FIXED_LINE number.
  """
  PERSONAL_NUMBER

  """
  Pager.
  """
  PAGER

  """
  Used for "Universal Access Numbers" or "Company Numbers". They
  may be further routed to specific offices, but allow one number
  to be used for a company.
  """
  UAN

  """
  A phone number is of type UNKNOWN when it does not fit any of
  the known patterns for a specific region.
  """
  UNKNOWN

  """
  Emergency.
  """
  EMERGENCY

  """
  Voicemail.
  """
  VOICEMAIL

  """
  Short code.
  """
  SHORT_CODE

  """
  Standard rate.
  """
  STANDARD_RATE
}

type ParsedPhoneNumber {
  """
  Format the phone number.
  """
  format(
    """
    The desired format.
    """
    format: PhoneNumberFormat
  ): String

  """
  The country calling code for this number, as defined by the International
  Telecommunication Union (ITU). For example, this would be 1 for NANPA
  countries, and 33 for France.
  """
  countryCode: Int

  """
  Two character region codes for this country code (e.g. "CH").
  """
  regionCodes: [String]

  """
  The phone number type.
  """
  type: PhoneNumberType!
}

Extension

graphql
extend type FieldItemTypeTelephone {
  """
  Parse the phone number. If the number is not valid it will return NULL.
  """
  parsed(
    """
    The default region to use when parsing the phone number.
    """
    region: String
  ): ParsedPhoneNumber
}

Examples

graphql
query {
  entityById(entityType: NODE, id: "123") {
    ... on NodeContact {
      phone: fieldPhoneRawField {
        first {
          parsed {
            ...phoneNumber
          }
        }
      }
    }
  }
}

fragment phoneNumber on ParsedPhoneNumber {
  e164: format(format: E164)
  international: format(format: INTERNATIONAL)
  national: format(format: NATIONAL)
  rfc3966: format(format: RFC3966)
  default: format
  type
  countryCode
  regionCodes
}
json
{
  "data": {
    "entityById": {
      "phone": {
        "first": {
          "parsed": {
            "e164": "+41791234567",
            "international": "+41 79 123 45 67",
            "national": "079 123 45 67",
            "rfc3966": "tel:+41-79-123-45-67",
            "default": "+41 79 123 45 67",
            "type": "MOBILE",
            "countryCode": 41,
            "regionCodes": ["CH"]
          }
        }
      }
    }
  }
}

Data Producers

phone_parser

Parse a phone number given in the value argument. Optionally provide a region argument if the value does not contain a country code.

Returns a PhoneNumber instance.

php
$registry->addFieldResolver('Query', 'contactNumber',
  $builder->produce('phone_parser')
    ->map('value', $builder->fromValue('+41791234567')),
);

phone_formatter

Format a phone number. argument phoneNumber is a PhoneNumber instance.

The format argument is the name of the format, e.g. INTERNATIONAL or RFC3966.

php
$registry->addFieldResolver('Person', 'phoneFormatted',
  $builder->compose(
    $builder->produce('phone_parser')
      ->map('value', $builder->fromValue('+41791234567')),
    $builder->produce('phone_formatter')
      ->map('phoneNumber', $builder->fromParent())
      ->map('format', $builder->fromValue('INTERNATIONAL'))
  )
);