Skip to content

Commit

Permalink
Feature/7 persistent query variables (#16)
Browse files Browse the repository at this point in the history
* #7 Add variables param for persistent queries

* #7 encode variables in the url

* #7 add default env vars for e2e tests

* #7 update Readme with variables example

* #7 add persistent query variables example to Readme
  • Loading branch information
easingthemes committed Jun 2, 2021
1 parent ea5d4e4 commit bdad2d3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ aemHeadlessClient.persistQuery(queryString, 'wknd/persist-query-name')
.then(data => console.log(data))
.catch(e => console.error(e.toJSON()))

aemHeadlessClient.runPersistedQuery('wknd/persist-query-name')
aemHeadlessClient.runPersistedQuery('wknd/persist-query-name', { variable1: 'variable1Value' })
.then(data => console.log(data))
.catch(e => console.error(e.toJSON()))

aemHeadlessClient.getQuery('wknd/persist-query-name-with-variables', { name: 'John Doe'})
.then(data => console.log(data))
.catch(e => console.error(e.toJSON()))
```
#### Within async/await:
```javascript
Expand Down Expand Up @@ -112,7 +116,7 @@ If `auth` is not defined, Authorization header will not be set
SDK contains helper function to get Auth token from credentials config file

```javascript
const {getToken} = require('@adobe/aem-headless-client-js')
const { getToken } = require('@adobe/aem-headless-client-js')
(async () => {
const {accessToken, type, expires} = await getToken('path/to/service-config.json')
const sdkNode = new AEMHeadless('content/graphql/endpoint.gql', AEM_HOST_URI, accessToken)
Expand Down
5 changes: 3 additions & 2 deletions api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ with GraphQL endpoint, GraphQL host and auth if needed
* [.runQuery(query, [options])](#AEMHeadless+runQuery) ⇒ <code>Promise.&lt;any&gt;</code>
* [.persistQuery(query, path, [options])](#AEMHeadless+persistQuery) ⇒ <code>Promise.&lt;any&gt;</code>
* [.listPersistedQueries([options])](#AEMHeadless+listPersistedQueries) ⇒ <code>Promise.&lt;any&gt;</code>
* [.runPersistedQuery(path, [options])](#AEMHeadless+runPersistedQuery) ⇒ <code>Promise.&lt;any&gt;</code>
* [.runPersistedQuery(path, [variables], [options])](#AEMHeadless+runPersistedQuery) ⇒ <code>Promise.&lt;any&gt;</code>

<a name="new_AEMHeadless_new"></a>

Expand Down Expand Up @@ -83,7 +83,7 @@ Returns a Promise that resolves with a GET request JSON data.

<a name="AEMHeadless+runPersistedQuery"></a>

### aemHeadless.runPersistedQuery(path, [options]) ⇒ <code>Promise.&lt;any&gt;</code>
### aemHeadless.runPersistedQuery(path, [variables], [options]) ⇒ <code>Promise.&lt;any&gt;</code>
Returns a Promise that resolves with a GET request JSON data.

**Kind**: instance method of [<code>AEMHeadless</code>](#AEMHeadless)
Expand All @@ -92,5 +92,6 @@ Returns a Promise that resolves with a GET request JSON data.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| path | <code>string</code> | | AEM path for persisted query, format: configuration_name/endpoint_name |
| [variables] | <code>object</code> | <code>{}</code> | query variables |
| [options] | <code>object</code> | <code>{}</code> | additional GET request options |

6 changes: 3 additions & 3 deletions e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
## Requirements

To run the e2e test you'll need these env variables set:
1. `AEM_HOST_URI`
2. `AEM_GRAPHQL_ENDPOINT` (if different from default `content/graphql/endpoint.gql`)
3. `AEM_TOKEN` (or `AEM_USER` and `AEM_PASS`)
1. `AEM_HOST_URI` - default 'http://localhost:4502'
2. `AEM_GRAPHQL_ENDPOINT` - default `content/graphql/global/endpoint.json`
3. `AEM_TOKEN` (or `AEM_USER` and `AEM_PASS`) - default `AEM_USER=admin` and `AEM_PASS=admin`

## Run

Expand Down
2 changes: 1 addition & 1 deletion e2e/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require('dotenv').config({ path: path.join(__dirname, '.env') })

const { AEMHeadless } = require('../src')

const { AEM_TOKEN, AEM_USER, AEM_PASS, AEM_HOST_URI, AEM_GRAPHQL_ENDPOINT } = process.env
const { AEM_TOKEN, AEM_USER = 'admin', AEM_PASS = 'admin', AEM_HOST_URI = 'http://localhost:4502', AEM_GRAPHQL_ENDPOINT = 'content/graphql/global/endpoint.json' } = process.env

const queryString = `
{
Expand Down
17 changes: 14 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,23 @@ class AEMHeadless {
* Returns a Promise that resolves with a GET request JSON data.
*
* @param {string} path - AEM path for persisted query, format: configuration_name/endpoint_name
* @param {object} [variables={}] - query variables
* @param {object} [options={}] - additional GET request options
* @returns {Promise<any>} - the response body wrapped inside a Promise
*/
runPersistedQuery (path, options = {}) {
const url = `${AEM_GRAPHQL_ACTIONS.execute}/${path}`
return this.__handleRequest(url, '', { method: 'GET', ...options })

runPersistedQuery (path, variables = {}, options = {}) {
const method = (options.method || 'GET').toUpperCase()
let body = ''
let variablesString = Object.keys(variables).map(key => `;${key}=${encodeURIComponent(variables[key])}`).join()

if (method === 'POST') {
body = JSON.stringify({ variables })
variablesString = ''
}

const url = `${AEM_GRAPHQL_ACTIONS.execute}/${path}${variablesString}`
return this.__handleRequest(url, body, { method, ...options })
}

/**
Expand Down

0 comments on commit bdad2d3

Please sign in to comment.