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

Display wire-protocol when using --raw #520

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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: 2 additions & 1 deletion src/commands/query.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ async function queryCommand(argv) {
// Using --json or --raw takes precedence over --format
const outputFormat = resolveFormat(argv);

const results = await container.resolve("runQueryFromString")(expression, {
const runQueryFromString = container.resolve("runQueryFromString");
const results = await runQueryFromString(expression, {
apiVersion,
secret,
url,
Expand Down
1 change: 1 addition & 0 deletions src/commands/shell.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
prompt: `${argv.database || ""}> `,
ignoreUndefined: true,
preview: argv.apiVersion !== "10",
// TODO: integrate with fql-analyzer for completions

Check warning on line 41 in src/commands/shell.mjs

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO: integrate with fql-analyzer for...'
completer: argv.apiVersion === "10" ? () => [] : undefined,
output: container.resolve("stdoutStream"),
input: container.resolve("stdinStream"),
Expand Down Expand Up @@ -188,6 +188,7 @@
timeout,
typecheck,
performanceHints,
raw,
format: outputFormat,
});

Expand Down
7 changes: 4 additions & 3 deletions src/lib/fauna-client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export const runQueryFromString = (expression, argv) => {
}),
);
} else {
const { secret, url, timeout, format, performanceHints, ...rest } = argv;
const { secret, url, timeout, format, performanceHints, raw, ...rest } =
argv;
let apiFormat = "decorated";
if (format === Format.JSON) {
apiFormat = "simple";
Expand All @@ -72,9 +73,9 @@ export const runQueryFromString = (expression, argv) => {
return retryInvalidCredsOnce(secret, (secret) =>
faunaV10.runQueryFromString({
expression,
secret,
url,
client: undefined,
secret,
raw,
options: {
/* eslint-disable camelcase */
query_timeout_ms: timeout,
Expand Down
69 changes: 52 additions & 17 deletions src/lib/fauna.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import chalk from "chalk";
import {
ClientClosedError,
ClientError,
getDefaultHTTPClient,
NetworkError,
ProtocolError,
ServiceError,
Expand Down Expand Up @@ -43,9 +44,10 @@ export const defaultV10QueryOptions = {
* @param {object} opts
* @param {string} opts.url
* @param {string} opts.secret
* @param {import("fauna").HTTPClient | undefined} [opts.httpClient]
* @returns {import("fauna").Client}
*/
export const getClient = ({ url, secret }) => {
export const getClient = ({ url, secret }, httpClient) => {
const Client = container.resolve("fauna").Client;

// Check for required arguments.
Expand All @@ -55,7 +57,10 @@ export const getClient = ({ url, secret }) => {
);
}
// Create the client.
return new Client({ secret, endpoint: url ? new URL(url) : undefined });
return new Client(
{ secret, endpoint: url ? new URL(url) : undefined },
httpClient,
);
};

/**
Expand All @@ -64,37 +69,67 @@ export const getClient = ({ url, secret }) => {
*
* @param {object} opts
* @param {import("fauna").Query<any>} opts.query
* @param {string} [opts.url]
* @param {string} [opts.secret]
* @param {import("fauna").Client} [opts.client]
* @param {string} opts.url
* @param {string} opts.secret
* @param {boolean} [opts.raw]
* @param {import("fauna").QueryOptions} [opts.options]
* @returns {Promise<import("fauna").QuerySuccess<any>>}
*/
export const runQuery = async ({
query,
url,
secret,
client,
raw = false,
options = {},
}) => {
// Check for required arguments.
if (!query) {
throw new ValidationError("A query is required.");
}

/** @type {import("fauna").HTTPClient | undefined} */
let httpClient;
if (raw) {
httpClient = {
/**
* The Fauna Client instance must be using "simple" or "decorated" format
* for this to work.
*
* Either setting will assume that the response from Fauna should be used
* as-is, without any tagged decoding. So we can force tagged within the
* the request and return the response back to the client.
*/
request: async (req) => {
const innerClient = getDefaultHTTPClient({
url: url,
// These options are required for any HTTPClient. We are not setting
// them elsewhere in the CLI. Specify the same defaults as Client.
http2_session_idle_ms: 5000, // eslint-disable-line camelcase
http2_max_streams: 100, // eslint-disable-line camelcase
fetch_keepalive: false, // eslint-disable-line camelcase
});

req.headers["x-format"] = "tagged";
return await innerClient.request(req);
},
close: () => {}, //eslint-disable-line no-empty-function
};
}

// Create the client if one wasn't provided.
let _client =
client ??
getClient({
let client = getClient(
{
url: /** @type {string} */ (url), // We know this is a string because we check for !url above.
secret: /** @type {string} */ (secret), // We know this is a string because we check for !secret above.
});
},
httpClient,
);
// Run the query.
return _client
return client
.query(query, { ...defaultV10QueryOptions, ...options })
.finally(() => {
// Clean up the client if one was created internally.
if (!client && _client) _client.close();
client.close();
});
};

Expand All @@ -103,21 +138,21 @@ export const runQuery = async ({
*
* @param {object} opts
* @param {string} opts.expression - The FQL expression to interpret
* @param {string} [opts.url]
* @param {string} [opts.secret]
* @param {import("fauna").Client} [opts.client]
* @param {string} opts.url
* @param {string} opts.secret
* @param {boolean} opts.raw
* @param {import("fauna").QueryOptions} [opts.options]
* @returns {Promise<import("fauna").QuerySuccess<any>>}
*/
export const runQueryFromString = async ({
expression,
url,
secret,
client,
raw = false,
options = {},
}) => {
const query = await stringExpressionToQuery(expression);
return runQuery({ query, url, secret, client, options });
return runQuery({ query, url, secret, raw, options });
};

/**
Expand Down
Loading