Skip to content

Commit c1f943e

Browse files
committed
CTR Modernize JS README and examples for TP4, and update JS Upgrade Docs
1 parent 5ad09cb commit c1f943e

6 files changed

Lines changed: 138 additions & 9 deletions

File tree

docs/src/upgrade/release-4.x.x.asciidoc

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ might apply.
123123
124124
* JavaScript GLV
125125
** `mimeType` setting has been removed. Use `reader` and `writer` options directly to control serialization format.
126+
** `authenticator` option has been removed. Use `interceptors` with `auth.basic()` or `auth.sigv4()` instead.
127+
** `session` option has been removed.
128+
** `interceptors` option has been added to `Client` and `DriverRemoteConnection` for HTTP request modification.
129+
** `writer` can be set to `null` to skip request serialization, allowing interceptors to handle it directly.
130+
** `bulkResults` has been added as a request option. `DriverRemoteConnection` defaults it to `true`.
131+
** `Bytecode` has been replaced with `GremlinLang`.
132+
** Serialization updated to GraphBinary 4.
126133
* .NET GLV
127134
** `ConnectionPoolSettings` has been replaced with `ConnectionSettings` for HTTP connection configuration.
128135
** `webSocketConfiguration` parameter has been removed.
@@ -223,6 +230,112 @@ The WebSocket-specific settings (`TransporterType`, `ReadBufferSize`, `WriteBuff
223230
`NewConnectionThreshold`, `Session`) have been removed. New HTTP connection pool settings have been added:
224231
`MaximumConcurrentConnections`, `MaxIdleConnections`, `IdleConnectionTimeout`, and `KeepAliveInterval`.
225232
233+
[[javascript-typescript-esm]]
234+
==== Gremlin-JavaScript TypeScript and ESM Migration
235+
236+
The `gremlin-javascript` driver has been rewritten in TypeScript and now ships with built-in type declarations. The
237+
package publishes dual ESM and CommonJS builds, so both `import` and `require` work out of the box.
238+
239+
===== Module format changes
240+
241+
The package now sets `"type": "module"` in `package.json` and uses conditional `exports`:
242+
243+
- ESM: `import gremlin from 'gremlin'` resolves to `build/esm/index.js`
244+
- CJS: `const gremlin = require('gremlin')` resolves to `build/cjs/index.cjs`
245+
246+
Existing CommonJS applications using `require('gremlin')` will continue to work without changes. Applications using
247+
ESM can now use `import` directly:
248+
249+
[source,javascript]
250+
----
251+
// CommonJS (still works)
252+
const gremlin = require('gremlin');
253+
254+
// ESM — default import
255+
import gremlin from 'gremlin';
256+
257+
// ESM — named imports
258+
import { driver, process, structure } from 'gremlin';
259+
----
260+
261+
===== TypeScript support
262+
263+
Type declarations (`.d.ts`) are included in the published package. TypeScript projects can import the driver directly
264+
without installing separate `@types` packages:
265+
266+
[source,typescript]
267+
----
268+
import gremlin from 'gremlin';
269+
import type { ConnectionOptions } from 'gremlin/build/esm/driver/connection.js';
270+
271+
const connection = new gremlin.driver.DriverRemoteConnection('http://localhost:8182/gremlin');
272+
const g = gremlin.process.traversal().with_(connection);
273+
----
274+
275+
===== Breaking changes to exports
276+
277+
Several exports have been removed or renamed compared to 3.x:
278+
279+
- `process.Bytecode` has been removed. Use `process.GremlinLang` instead.
280+
- `process.Translator` has been removed. To produce a canonical gremlin script from a `Traversal`, use
281+
`process.GremlinLang` instead (e.g. `traversal.getGremlinLang().getGremlin()`).
282+
- `driver.auth.Authenticator` and `driver.auth.PlainTextSaslAuthenticator` have been removed. Use `driver.auth.basic()`
283+
and `driver.auth.sigv4()` interceptor factories instead.
284+
- `structure.io` (the serializer namespace) has been removed. Serialization is now internal to the driver.
285+
- `Graph.traversal()` has been removed. Use the `process.traversal()` function instead.
286+
287+
For example, authentication that previously used `PlainTextSaslAuthenticator`:
288+
289+
[source,javascript]
290+
----
291+
// 3.x
292+
const authenticator = new gremlin.driver.auth.PlainTextSaslAuthenticator('username', 'password');
293+
const client = new gremlin.driver.Client('ws://localhost:8182/gremlin', { authenticator });
294+
295+
// 4.x
296+
const client = new gremlin.driver.Client('http://localhost:8182/gremlin', {
297+
interceptors: gremlin.driver.auth.basic('username', 'password'),
298+
});
299+
----
300+
301+
[[javascript-http-interceptor]]
302+
==== Gremlin-JavaScript HTTP and Interceptor Changes
303+
304+
The `gremlin-javascript` driver has been updated to use HTTP instead of WebSockets. The `Connection` class now uses
305+
Node.js `fetch` internally and sends requests via HTTP POST to Gremlin Server.
306+
307+
`Bytecode` has been replaced with `GremlinLang`, which builds a gremlin-lang compatible string representation of a
308+
traversal along with a map of named parameters. Traversals now produce gremlin-lang strings that are sent to the server
309+
rather than serialized bytecode.
310+
311+
HTTP interceptors have been added to `gremlin-javascript`. Interceptors can be passed to the `Client` or
312+
`DriverRemoteConnection` constructor using the `interceptors` option. A `RequestInterceptor` is a function that receives
313+
an `HttpRequest` object containing the HTTP method, URL, headers, and body, and returns the (possibly modified) request
314+
or a `Promise` resolving to one. Interceptors are executed in order before the request is sent.
315+
316+
Authentication is now handled through interceptors rather than the previous `authenticator` option. The `auth` module
317+
provides factory functions for built-in interceptors:
318+
319+
[source,javascript]
320+
----
321+
import gremlin from 'gremlin';
322+
323+
const { DriverRemoteConnection, auth } = gremlin.driver;
324+
325+
// Basic authentication
326+
const connection = new DriverRemoteConnection('https://localhost:8182/gremlin', {
327+
interceptors: auth.basic('username', 'password'),
328+
});
329+
330+
// SigV4 authentication
331+
const connection = new DriverRemoteConnection('https://localhost:8182/gremlin', {
332+
interceptors: auth.sigv4('service-region', 'service-name'),
333+
});
334+
----
335+
336+
Setting the `writer` option to `null` prevents the `RequestMessage` from being serialized before interceptors run,
337+
allowing an interceptor to handle serialization directly where desired.
338+
226339
==== Transactions Re-enabled
227340
228341
IMPORTANT: The remote transaction API is still under active discussion and the specification has not been finalized.

gremlin-js/gremlin-javascript/README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ Gremlin-Javascript is designed to connect to a "server" that is hosting a Tinker
3636
could be [Gremlin Server][gs] or a [remote Gremlin provider][rgp] that exposes protocols by which Gremlin-Javascript
3737
can connect.
3838

39-
A typical connection to a server running on "localhost" that supports the Gremlin Server protocol using websockets
40-
looks like this:
39+
A typical connection to a server running on "localhost" that supports the Gremlin Server protocol looks like this:
4140

4241
```javascript
4342
const gremlin = require('gremlin');
@@ -47,6 +46,21 @@ const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
4746
const g = traversal().with_(new DriverRemoteConnection('http://localhost:8182/gremlin'));
4847
```
4948

49+
Some graph providers may require additional request customization to connect. The driver supports request interceptors
50+
for these cases. For example, to connect with basic authentication:
51+
52+
```javascript
53+
const gremlin = require('gremlin');
54+
const traversal = gremlin.process.AnonymousTraversalSource.traversal;
55+
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
56+
57+
const g = traversal().with_(new DriverRemoteConnection('http://localhost:8182/gremlin', {
58+
interceptors: gremlin.driver.auth.basic('username', 'password')
59+
}));
60+
```
61+
62+
Refer to your graph provider's documentation for specific connection requirements.
63+
5064
Once "g" has been created using a connection, it is then possible to start writing Gremlin traversals to query the
5165
remote graph:
5266

gremlin-js/gremlin-javascript/examples/node/basic-gremlin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const vertexLabel = process.env.VERTEX_LABEL || 'person';
2727

2828
async function main() {
2929
const dc = new DriverRemoteConnection(serverUrl);
30-
const g = traversal().withRemote(dc);
30+
const g = traversal().with_(dc);
3131

3232
// Basic Gremlin: adding and retrieving data
3333
const v1 = await g.addV(vertexLabel).property('name','marko').next();

gremlin-js/gremlin-javascript/examples/node/connections.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async function main() {
3232
async function withRemote() {
3333
// Connecting to the server
3434
const dc = new DriverRemoteConnection(serverUrl);
35-
const g = traversal().withRemote(dc);
35+
const g = traversal().with_(dc);
3636

3737
// Simple query to verify connection
3838
const v = await g.addV(vertexLabel).iterate();
@@ -46,10 +46,10 @@ async function withRemote() {
4646
async function withConfigs() {
4747
// Connecting and customizing configurations
4848
const dc = new DriverRemoteConnection(serverUrl, {
49-
rejectUnauthorized: false,
5049
traversalSource: 'g',
50+
headers: { 'X-Api-Key': 'your-api-key' },
5151
});
52-
const g = traversal().withRemote(dc);
52+
const g = traversal().with_(dc);
5353

5454
const v = await g.addV(vertexLabel).iterate();
5555
const count = await g.V().hasLabel(vertexLabel).count().next();

gremlin-js/gremlin-javascript/examples/node/modern-traversals.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ async function main() {
3636
// Use gmodern in CI environment, default connection locally
3737
const options = process.env.DOCKER_ENVIRONMENT ? { traversalSource: 'gmodern' } : {};
3838
const dc = new DriverRemoteConnection(serverUrl, options);
39-
const g = traversal().withRemote(dc);
39+
const g = traversal().with_(dc);
4040

4141
const e1 = await g.V(1).bothE().toList(); // (1)
4242
const e2 = await g.V(1).bothE().where(__.otherV().hasId(2)).toList(); // (2)
4343
const v1 = await g.V(1).next();
4444
const v2 = await g.V(2).next();
45-
const e3 = await g.V(v1.value).bothE().where(__.otherV().is(v2.value)).toList(); // (3)
46-
const e4 = await g.V(v1.value).outE().where(__.inV().is(v2.value)).toList(); // (4)
45+
const e3 = await g.V(v1.value).bothE().where(__.otherV().id().is(v2.value)).toList(); // (3)
46+
const e4 = await g.V(v1.value).outE().where(__.inV().id().is(v2.value)).toList(); // (4)
4747
const e5 = await g.V(1).outE().where(__.inV().has(t.id, p.within(2,3))).toList(); // (5)
4848
const e6 = await g.V(1).out().where(__.in_().hasId(6)).toList(); // (6)
4949

gremlin-js/gremlin-javascript/lib/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,5 @@ export const structure = {
8888
VertexProperty: graph.VertexProperty,
8989
toLong: utils.toLong,
9090
};
91+
92+
export default { driver, process, structure };

0 commit comments

Comments
 (0)