Skip to content

Commit

Permalink
Deploy dotenv-flow changes to Prod (#654)
Browse files Browse the repository at this point in the history
* fix: correct domain replacement for graphql fields (#650)

* docs: correct `dotenv` dep to `dotenv-flow` (#646)

* docs: correct `dotenv` dep to `dotenv-flow`

* docs: replace rest of `dotenv` -> `dotenv-flow`

Co-authored-by: Andrew Matthews <[email protected]>
  • Loading branch information
blakewilson and apmatthews authored Nov 17, 2021
1 parent e3f68d1 commit 7e46646
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 19 deletions.
4 changes: 2 additions & 2 deletions docs/next/guides/fetching-data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ Faust.js uses [GQty](https://gqty.dev/docs/react/fetching-data) as the primary w

GQty works primarily based on TypeScript typings generated using introspection on your GraphQL API. Using WPGraphQL as your Headless WordPress API, you can enable introspection and then generate typings for GQty. You will need to do the following:

1. Run `npm install -D @gqty/cli dotenv`
1. Run `npm install -D @gqty/cli dotenv-flow`
1. Create a `generate` script in your `package.json` that runs `gqty generate`.
1. Create a `gqty.config.js` file at the root of your project. Use the following example config:

```js
require('dotenv').config();
require('dotenv-flow').config();

/**
* @type {import("@gqty/cli").GQtyConfig}
Expand Down
2 changes: 1 addition & 1 deletion docs/release-notes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ module.exports = withFaust({
In the root of your project, alongside `package.json`, replace the `gqty.config.js` file with the following:

```js
require('dotenv').config();
require('dotenv-flow').config();

/**
* @type {import("@gqty/cli").GQtyConfig}
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorial/setup-faustjs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ npm install --save-prod @faustjs/core @faustjs/next
#### Dev Dependencies

```bash
npm install --save-dev @gqty/cli dotenv
npm install --save-dev @gqty/cli dotenv-flow
```

### Create `faust.config.js`
Expand Down Expand Up @@ -105,7 +105,7 @@ Replace the `NEXT_PUBLIC_WORDPRESS_URL` value with the URL of your WordPress sit
We use [GQty](https://gqty.dev) as our GraphQL client. To configure GQty, we need to create a `gqty.config.js` file in the root of our project.

```js title=gqty.config.js
require('dotenv').config();
require('dotenv-flow').config();

/**
* @type {import("@gqty/cli").GQtyConfig}
Expand Down Expand Up @@ -186,7 +186,7 @@ Next, add the following `generate` script to your `package.json`:
"devDependencies": {
"@gqty/cli": "^1.0.4",
"@types/react": "^17.0.15",
"dotenv": "^10.0.0",
"dotenv-flow": "^3.2.0",
"typescript": "^4.3.5"
}
}
Expand Down
33 changes: 20 additions & 13 deletions plugins/faustwp/includes/replacement/graphql-callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
/**
* Callback for WP GraphQL 'graphql_request_results' filter.
*
* Replaces the WordPress Site URL with the replacement domain in 'url' nodes.
* Replaces the WordPress Site URL with the replacement domain in 'url' and
* 'href' fields. Response data for RootQuery.generalSettings is intentionally
* left unaltered.
*
* @param object $response The default GraphQL query response.
*
Expand All @@ -31,20 +33,25 @@ function url_replacement( $response ) {
if (
is_object( $response ) &&
property_exists( $response, 'data' ) &&
is_array( $response->data ) &&
domain_replacement_enabled() &&
! array_key_exists( 'generalSettings', $response->data )
is_array( $response->data )
) {
array_walk_recursive(
$response->data,
function( &$value, $key ) {
if ( 'url' === $key || 'href' === $key ) {
$replacement = faustwp_get_setting( 'frontend_uri', '/' );
$value = str_replace( site_url(), $replacement, $value );
}
}
);
url_replace_recursive( $response->data );
}

return $response;
}

function url_replace_recursive( &$data ) {
foreach ( $data as $key => &$value ) {
if ( $key === 'generalSettings' ) {
continue;
}

if ( ( 'url' === $key || 'href' === $key ) && is_string( $value ) ) {
$replacement = faustwp_get_setting( 'frontend_uri', '/' );
$value = str_replace( site_url(), $replacement, $value );
} else if ( is_array( $value ) ) {
url_replace_recursive( $value );
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
/**
* Class GraphQLCallbacksTestCases
*
* @package FaustWP
*/

namespace WPE\FaustWP\Tests\Replacement;

use function WPE\FaustWP\Replacement\{ url_replacement };
use function WPE\FaustWP\Settings\faustwp_update_setting;

class GraphQLCallbacksTestCases extends \WP_UnitTestCase {

private $graphqlResponse;

private $responseData = [
'generalSettings' => [
'url' => 'http://example.org'
],
'menuItems' => [
'nodes' => [
[
'label' => 'Hello World',
'url' => 'http://example.org/hello-world/'
],
[
'label' => 'External Site',
'url' => 'http://external.site/no-replacement'
]
]
],
'menus' => [
'nodes' => [
[
'menuItems' => [
'nodes' => [
[
'label' => 'Hello World',
'url' => 'http://example.org/hello-world/'
]
]
]
]
]
],
'aThingWithHref' => [
'nodes' => [
[
'label' => "This uses href instead of url fields",
'href' => 'http://example.org/href'
]
]
]
];

private $expectedData = [
'generalSettings' => [
'url' => 'http://example.org'
],
'menuItems' => [
'nodes' => [
[
'label' => 'Hello World',
'url' => 'http://frontend/hello-world/'
],
[
'label' => 'External Site',
'url' => 'http://external.site/no-replacement'
]
]
],
'menus' => [
'nodes' => [
[
'menuItems' => [
'nodes' => [
[
'label' => 'Hello World',
'url' => 'http://frontend/hello-world/'
]
]
]
]
]
],
'aThingWithHref' => [
'nodes' => [
[
'label' => "This uses href instead of url fields",
'href' => 'http://frontend/href'
]
]
]
];

public function setUp() {
parent::setUp();

faustwp_update_setting( 'frontend_uri', 'http://frontend' );

$this->graphqlResponse = new \stdClass();
$this->graphqlResponse->data = $this->responseData;
}

public function test_graphql_request_results_filter() {
$this->assertSame( 10, has_action( 'graphql_request_results', 'WPE\FaustWP\Replacement\url_replacement' ) );
}

/**
* Tests url_replacement() returns original data when rewrites are not enabled.
*/
public function test_url_replacement_does_not_alter_response_when_rewrites_are_not_enabled() {
faustwp_update_setting( 'enable_rewrites', '0' );
$filteredRespone = url_replacement( $this->graphqlResponse );
$this->assertSame( $this->responseData, $filteredRespone->data );
}

/**
* Tests url_replacement() does not modify generalSettings even when rewrites are enabled.
*/
public function test_url_replacement_does_not_alter_generalSettings_when_rewrites_are_enabled() {
faustwp_update_setting( 'enable_rewrites', '1' );
$filteredRespone = url_replacement( $this->graphqlResponse );
$this->assertSame( $this->responseData['generalSettings'], $filteredRespone->data['generalSettings'] );
}

/**
* Tests url_replacement() modifies url and href fields when rewrites are enabled.
*/
public function test_url_replacement_replaces_url_fields_when_rewrites_are_enabled() {
faustwp_update_setting( 'enable_rewrites', '1' );
$filteredRespone = url_replacement( $this->graphqlResponse );
$this->assertSame( $this->expectedData, $filteredRespone->data );
}
}

1 comment on commit 7e46646

@headless-platform-by-wp-engine

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Branch main was deployed successfully
Your environment Production of app faustjs-site was successfully updated
View build logs: https://my.wpengine.com/atlas#/faustjs-site/cwa4v7z4d1u9vi7qw7hythg2/ktcyquuf386lfxnhme9c0wr3
View your environment URL: https://faustjs.org

Please sign in to comment.