Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
22 changes: 10 additions & 12 deletions test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,33 @@ describe('Client', () => {
expect(client._http._baseUrl.endsWith(':0')).toBeFalsy()
})

test('returns query metrics if the metrics flag is set', async () => {
test('the client does not support a metrics flag', 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')
expect(response).toEqual(2)
})

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