Skip to content

Commit

Permalink
Fix backward compatibility bug introduced in 4.5.3 that causes query …
Browse files Browse the repository at this point in the history
…to return a differnt type (#633)

* Fix backward compatibility bug introduced in 4.5.3 that causes query to return a differnt type

* Update CHANGELOG.md

* remove metrics from queryoptions type

* Fix client test with invalid metrics flag

* Fix test

* Fix test

* Deprecate 4.5.3
  • Loading branch information
cleve-fauna authored Mar 30, 2022
1 parent 20e66a4 commit 71085f0
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 31 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 4.5.4
- Disable ability to configure a client and the query method from returning metrics when calling query - fixing bug introduced in 4.5.3 that breaks backward compatibility. Continue supporting queryWithMetrics. [#633](https://github.com/fauna/faunadb-js/pull/633).

## 4.5.3
- Enable the client to return metrics on queries [#625](https://github.com/fauna/faunadb-js/pull/625) [#628](https://github.com/fauna/faunadb-js/pull/628)

Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@ let client = new faunadb.Client({
})
```

The `response` object is shaped differently for clients when the `metrics` option
is/not set. The default client setting returns the `response` body at root level.
When the client is configured to return `metrics`, the `response` object is
structured as follows:
#### Querying and Returning the metrics of your queries

The `response` object is shaped differently for clients when calling `queryWithMetrics`;
it includes the value of the response along with a metrics field giving data on ops,
time, and transaction retires consumed by your query:

```javascript
{
Expand Down
1 change: 1 addition & 0 deletions concourse/scripts/publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ then
echo "Publishing a new version..."
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc
npm publish
npm deprecate faunadb-js@4.5.3 "4.5.3 is is deprecated as it contains a bug that changed the type returned by query for typescript users"
rm .npmrc

echo "faunadb-js@$PACKAGE_VERSION published to npm" > ../slack-message/publish
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "faunadb",
"version": "4.5.3",
"version": "4.5.4",
"apiVersion": "4",
"description": "FaunaDB Javascript driver for Node.JS and Browsers",
"homepage": "https://fauna.com",
Expand Down
11 changes: 3 additions & 8 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ function Client(options) {
queryTimeout: null,
http2SessionIdleTime: http2SessionIdleTime.value,
checkNewVersion: false,
metrics: false,
})

if (http2SessionIdleTime.shouldOverride) {
Expand All @@ -194,7 +193,6 @@ function Client(options) {
this._observer = options.observer
this._http = new http.HttpClient(options)
this.stream = stream.StreamAPI(this)
this._globalQueryOptions = { metrics: options.metrics }
}

/**
Expand Down Expand Up @@ -298,13 +296,10 @@ Client.prototype.close = function(opts) {
* @return {external:Promise<Object>} {value, metrics} An object containing the FaunaDB response object and the list of query metrics incurred by the request.
*/
Client.prototype.queryWithMetrics = function(expression, options) {
options = Object.assign({}, this._globalQueryOptions, options, {
metrics: true,
})
return this._execute('POST', '', query.wrap(expression), null, options)
return this._execute('POST', '', query.wrap(expression), null, options, true)
}

Client.prototype._execute = function(method, path, data, query, options) {
Client.prototype._execute = function(method, path, data, query, options, returnMetrics = false) {
query = util.defaults(query, null)

if (
Expand Down Expand Up @@ -358,7 +353,7 @@ Client.prototype._execute = function(method, path, data, query, options) {
'x-txn-retries',
]

if (options && options.metrics) {
if (returnMetrics) {
return {
value: responseObject['resource'],
metrics: Object.fromEntries(Array.from(Object.entries(response.headers)).
Expand Down
4 changes: 2 additions & 2 deletions src/types/Client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface QueryOptions
extends Partial<
Pick<
ClientConfig,
'secret' | 'queryTimeout' | 'fetch' | 'observer' | 'metrics'
'secret' | 'queryTimeout' | 'fetch' | 'observer'
>
> {
signal?: AbortSignal
Expand Down Expand Up @@ -62,7 +62,7 @@ interface MetricsResponse<T = object> {

export default class Client {
constructor(opts?: ClientConfig)
query<T = object>(expr: ExprArg, options?: QueryOptions): Promise<T> | Promise<MetricsResponse<T>>
query<T = object>(expr: ExprArg, options?: QueryOptions): Promise<T>
queryWithMetrics<T = object>(
expr: ExprArg,
options?: QueryOptions
Expand Down
24 changes: 10 additions & 14 deletions test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,31 @@ describe('Client', () => {
expect(client._http._baseUrl.endsWith(':0')).toBeFalsy()
})

test('returns query metrics if the metrics flag is set', async () => {
var metricsClient = util.getClient({ metrics: true })
const response = await metricsClient.query(query.Add(1, 1))
assertMetric(response.metrics, 'x-compute-ops')
assertMetric(response.metrics, 'x-byte-read-ops')
assertMetric(response.metrics, 'x-byte-write-ops')
assertMetric(response.metrics, 'x-query-time')
assertMetric(response.metrics, 'x-txn-retries')
test('the client does not support a metrics flag', async () => {
expect(() => util.getClient({ metrics: true })).toThrow(new Error('No such option metrics'))
})

test('returns query metrics if using the queryWithMetrics function', async () => {
test('query does not support a metrics flag', async () => {
const response = await client.query(query.Add(1, 1))
expect(response).toEqual(2)
})

test('queryWithMetrics returns the metrics and the response value', async () => {
const response = await client.queryWithMetrics(query.Add(1, 1))
expect(response.value).toEqual(2)
assertMetric(response.metrics, 'x-compute-ops')
assertMetric(response.metrics, 'x-byte-read-ops')
assertMetric(response.metrics, 'x-byte-write-ops')
assertMetric(response.metrics, 'x-query-time')
assertMetric(response.metrics, 'x-txn-retries')
})

test('query metrics response has correct structure', async () => {
test('queryWithMetrics returns the metrics', async () => {
const response = await client.queryWithMetrics(query.Add(1, 1))
expect(Object.keys(response).sort()).
toEqual(['metrics', 'value'])
})

test('client with metrics returns expected value', async () => {
const response = await client.queryWithMetrics(query.Add(1, 1))
expect(response.value).toEqual(2)
})

test('paginates', () => {
return createDocument().then(function(document) {
Expand Down

0 comments on commit 71085f0

Please sign in to comment.