Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hnsr committed Jun 2, 2023
0 parents commit f108036
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 0 deletions.
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "reach-digital/magento2-apollo-sandbox",
"description": "Embeds Apollo Sandbox at /graphql-sandbox",
"authors": [
{
"name": "Hans Nieser",
"email": "hans@reachdigital.nl"
}
],
"require": {
"php": "~7.4.0||~8.1.0",
"magento/product-community-edition": "^2.3"
},
"type": "magento2-module",
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"ReachDigital\\ApolloSandbox\\": "src/"
}
}
}

9 changes: 9 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'ReachDigital_ApolloSandbox',
__DIR__ . '/src'
);
58 changes: 58 additions & 0 deletions src/Controller/Index/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace ReachDigital\ApolloSandbox\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Escaper;

class Index extends Action implements HttpGetActionInterface
{
private \Magento\Framework\Controller\Result\RawFactory $rawFactory;
private \Magento\Framework\UrlInterface $urlBuilder;
private Escaper $escaper;

public function __construct(
Context $context,
\Magento\Framework\Controller\Result\RawFactory $rawFactory,
\Magento\Framework\UrlInterface $urlBuilder,
Escaper $escaper
) {
parent::__construct($context);
$this->rawFactory = $rawFactory;
$this->urlBuilder = $urlBuilder;
$this->escaper = $escaper;
}

/**
* @inheritDoc
*/
public function execute()
{
$rawResponse = $this->rawFactory->create();
$rawResponse->setContents($this->getEmbedScript());
return $rawResponse;
}

private function getEmbedScript(): string
{
return '<body style="margin:0;padding:0;">
<div style="width: 100%; height: 100%;" id="embedded-sandbox"></div>
<script src="https://embeddable-sandbox.cdn.apollographql.com/_latest/embeddable-sandbox.umd.production.min.js"></script>
<script>
new window.EmbeddedSandbox({
target: "#embedded-sandbox",
initialEndpoint: "' . $this->escaper->escapeUrl($this->getGraphqlUrl()) . '"
});
</script>
</body>';
}

private function getGraphqlUrl(): string
{
return rtrim($this->urlBuilder->getBaseUrl(), '/') . '/graphql';
}
}
16 changes: 16 additions & 0 deletions src/etc/csp_whitelist.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<csp_whitelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Csp:etc/csp_whitelist.xsd">
<policies>
<policy id="script-src">
<values>
<value id="apollo_sandbox_cdn" type="host">embeddable-sandbox.cdn.apollographql.com</value>
</values>
</policy>
<policy id="frame-src">
<values>
<value id="apollo_sandbox_embed" type="host">sandbox.embed.apollographql.com/</value>
</values>
</policy>
</policies>
</csp_whitelist>
8 changes: 8 additions & 0 deletions src/etc/frontend/routes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="apollosandbox" frontName="graphql-sandbox">
<module name="ReachDigital_ApolloSandbox" />
</route>
</router>
</config>
8 changes: 8 additions & 0 deletions src/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="ReachDigital_ApolloSandbox">
<sequence>
<module name="Magento_GraphQl" />
</sequence>
</module>
</config>

0 comments on commit f108036

Please sign in to comment.