Skip to content

Commit

Permalink
[FEATURE][SEL-485] Added graphql support for postcode-nl/api-magento2…
Browse files Browse the repository at this point in the history
…-module and removed support for experius/module-postcode
  • Loading branch information
Hexmage committed Sep 28, 2021
1 parent 2e70173 commit 9f1c261
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 12 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## 2.0.0 (2021-09-28)

[View Release]([email protected]:experius/Mage2-Module-Experius-PostcodeGraphQl.git/commits/tag/2.0.0)

* [FEATURE][SEL-485] Added graphql support for postcode-nl/api-magento2-module and removed support for experius/module-postcode *(René Schep)*


## 1.0.1 (2020-10-08)

[View Release]([email protected]:experius/Mage2-Module-Experius-PostcodeGraphQl.git/commits/tag/1.0.1)

* [BUGFIX] Fixed old reference *(René Schep)*


## 1.0.0 (2020-10-02)

[View Release]([email protected]:experius/Mage2-Module-Experius-PostcodeGraphQl.git/commits/tag/1.0.0)

* [FEATURE] Intial commit, split from Postcode module *(René Schep)*


57 changes: 57 additions & 0 deletions Model/Resolver/AddressDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* A Magento 2 module named Experius/Postcode
* Copyright (C) 2016 Experius
*
* This file included in Experius/Postcode is licensed under OSL 3.0
*
* http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* Please see LICENSE.txt for the full text of the OSL 3.0 license
*/

namespace Experius\PostcodeGraphQl\Model\Resolver;

use Experius\Core\Helper\Settings;
use Flekto\Postcode\Helper\PostcodeApiClient;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

class AddressDetails implements ResolverInterface
{

protected $postcodeHelper;

/**
* PostcodeManagement constructor.
* @param Settings $helper
*/
public function __construct(
Settings $helper
) {
$this->postcodeHelper = new PostcodeApiClient(
$helper->getConfigValue('postcodenl_api/general/api_key'),
$helper->getConfigValue('postcodenl_api/general/api_secret')
);
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {

if (!isset($args['context']) || !$args['context']) {
throw new GraphQlInputException(__('"context" should be specified'));
}
$result = $this->postcodeHelper->internationalGetDetails($args['context']);
return $result;
}
}
80 changes: 80 additions & 0 deletions Model/Resolver/Autocomplete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* A Magento 2 module named Experius/Postcode
* Copyright (C) 2016 Experius
*
* This file included in Experius/Postcode is licensed under OSL 3.0
*
* http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* Please see LICENSE.txt for the full text of the OSL 3.0 license
*/

namespace Experius\PostcodeGraphQl\Model\Resolver;

use Experius\Core\Helper\Settings;
use Flekto\Postcode\Helper\CountryCodeConvertorHelper;
use Flekto\Postcode\Helper\PostcodeApiClient;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

class Autocomplete implements ResolverInterface
{

/**
* @var PostcodeApiClient
*/
protected $postcodeHelper;

/**
* PostcodeManagement constructor.
* @param Settings $helper
*/
public function __construct(
Settings $helper
) {
$this->postcodeHelper = new PostcodeApiClient(
$helper->getConfigValue('postcodenl_api/general/api_key'),
$helper->getConfigValue('postcodenl_api/general/api_secret')
);
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (!isset($args['countryId']) || !$args['countryId']) {
throw new GraphQlInputException(__('"countryId" should be specified'));
}
if (!isset($args['searchTerm']) || !$args['searchTerm']) {
throw new GraphQlInputException(__('"searchTerm" should be specified'));
}
$countryId = CountryCodeConvertorHelper::alpha2ToAlpha3($args['countryId']);
if(!isset($args['xAutocompleteSession'])) {
$session = bin2hex(random_bytes(8));
} else {
$session = $args['xAutocompleteSession'];
}

$result = $this->postcodeHelper->internationalAutocomplete($countryId, $args['searchTerm'], null, 'en-US');
if (isset($result['matches'])) {
foreach($result['matches'] as &$match) {
if(isset($match['highlights'])) {
foreach($match['highlights'] as &$highlight) {
$highlight['start'] = $highlight[0];
$highlight['end'] = $highlight[1];
}
}
}
}
return $result;
}
}
17 changes: 10 additions & 7 deletions Model/Resolver/Postcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

namespace Experius\PostcodeGraphQl\Model\Resolver;

use Experius\Postcode\Helper\Data as HelperData;
use Magento\Framework\Exception\LocalizedException;
use Experius\Core\Helper\Settings;
use Flekto\Postcode\Helper\PostcodeApiClient;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
Expand All @@ -23,18 +23,21 @@ class Postcode implements ResolverInterface
{

/**
* @var \Experius\Postcode\Helper\Data
* @var PostcodeApiClient
*/
protected $postcodeHelper;

/**
* PostcodeManagement constructor.
* @param \Experius\Postcode\Helper\Data $postcodeHelper Postcode helper.
* @param PostcodeApiClient $postcodeHelper
*/
public function __construct(
HelperData $postcodeHelper
Settings $helper
) {
$this->postcodeHelper = $postcodeHelper;
$this->postcodeHelper = new PostcodeApiClient(
$helper->getConfigValue('postcodenl_api/general/api_key'),
$helper->getConfigValue('postcodenl_api/general/api_secret')
);
}

/**
Expand All @@ -57,7 +60,7 @@ public function resolve(
$args['houseNumberAddition'] = '';
}

$result = $this->postcodeHelper->lookupAddress($args['postcode'], $args['houseNumber'], $args['houseNumberAddition']);
$result = $this->postcodeHelper->dutchAddressByPostcode($args['postcode'], $args['houseNumber'], $args['houseNumberAddition']);
if (isset($result['message'])) {
throw new GraphQlInputException($result['message']);
}
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Postcode.nl GraphQl Magento 2 Module by Experius
=============

Adds GraphQl support to the Experius Postcode.nl module.
Adds GraphQl support to the postcode-nl/api-magento2-module

IMPORTANT: Version 2 only supports the postcode-nl/api-magento2-module. For graphql support the experius/module-postcode module use version 1.x.
65 changes: 61 additions & 4 deletions etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
type Query {
postcode (
postcode: String @doc(description: "Postcode")
houseNumber: String @doc(description: "Housenumber")
postcode: String! @doc(description: "Postcode")
houseNumber: String! @doc(description: "Housenumber")
houseNumberAddition: String @doc(description: "Housenumber Addition")
): postCodeAddress @resolver(class: "Experius\\PostcodeGraphQl\\Model\\Resolver\\Postcode") @doc(description: "Returns Json Encoded AddressInfo") @cache(cacheIdentity: "Experius\\PostcodeGraphQl\\Model\\Resolver\\Postcode\\Identity")
): PostCodeAddress @resolver(class: "Experius\\PostcodeGraphQl\\Model\\Resolver\\Postcode") @doc(description: "Returns Json Encoded AddressInfo") @cache(cacheIdentity: "Experius\\PostcodeGraphQl\\Model\\Resolver\\Postcode\\Identity")
autocomplete (
countryId: String! @doc(description: "country code ISO-2")
searchTerm: String! @doc(description: "Search term")
xAutocompleteSession: String @doc(description: "Autocomplete Session")
): AutocompleteMatches @resolver(class: "Experius\\PostcodeGraphQl\\Model\\Resolver\\Autocomplete") @doc(description: "Returns Json Encoded Autocomplete suggestions")
addressDetails (
context: String! @doc(description: "context string from autocomplete")
xAutocompleteSession: String @doc(description: "Autocomplete Session")
): AddressDetails @resolver(class: "Experius\\PostcodeGraphQl\\Model\\Resolver\\AddressDetails") @doc(description: "Returns Json Encoded Autocomplete suggestions")
}

type postCodeAddress {
type PostCodeAddress {
street: String @doc(description: "Street name in accordance with \"BAG (Basisregistraties adressen en gebouwen)\". In capital and lowercase letters, including punctuation marks and accents. This field is at most 80 characters in length. Filled with \"Postbus\" in case it is a range of PO boxes.")
houseNumber: Int @doc(description: "House number of a perceel. In case of a Postbus match the house number will always be 0. Range: 0-99999")
houseNumberAddition: String @doc(description: "Addition of the house number to uniquely define a location. These additions are officially recognized by the municipality. This field is at most 6 characters in length and null if addition not found (see houseNumberAdditions result field).")
Expand All @@ -25,3 +34,51 @@ type postCodeAddress {
surfaceArea: Int @doc(description: "Surface in square meters. Null for PO Boxes.")
houseNumberAdditions: [String] @doc(description: "List of all house number additions having the postcode and houseNumber which was input.")
}

type AutocompleteMatches {
matches: [AutocompleteMatch]
xAutocompleteSession: String
}

type AutocompleteMatch {
value: String
label: String
description: String
precision: String
context: String
highlights: [Highlight]
}

type Highlight {
start: Int
end: Int
}

type AddressDetails {
language: String
address: AddressDetailsAddress
mailLines: [String]
location: AddressDetailsLocation
country: AddressDetailsCountry
xAutocompleteSession: String
}

type AddressDetailsAddress {
country: String
locality: String
street: String
postcode: String
building: String
buildingNumber: Int
buildingNumberAddition: String
}

type AddressDetailsLocation {
longitude: Float
latitude: Float
}

type AddressDetailsCountry {
name: String
iso3Code: String
}

0 comments on commit 9f1c261

Please sign in to comment.