Created
August 13, 2024 18:08
-
-
Save danshev/fcf77fd1524fb5857fffe8df198e657f to your computer and use it in GitHub Desktop.
TypeScript for connecting to AWS Neptune w/ v4 Signature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { driver, structure, process as gremlinProcess } from 'gremlin'; | |
import { fromNodeProviderChain } from '@aws-sdk/credential-providers'; | |
import { getUrlAndHeaders } from 'gremlin-aws-sigv4/lib/utils'; | |
async function getCredentials() { | |
try { | |
const provider = fromNodeProviderChain({ | |
clientConfig: { region: process.env.REGION }, | |
}); | |
const credentials = await provider(); | |
return { ...credentials, region: process.env.REGION }; | |
} catch (e) { | |
console.error('No credentials found', e); | |
throw e; | |
} | |
} | |
export const getGremlinClient = async ( | |
neptuneUrl: string, | |
): Promise<gremlinProcess.GraphTraversalSource> => { | |
const credentials = await getCredentials(); | |
const { url, headers } = getUrlAndHeaders( | |
neptuneUrl, | |
'8182', | |
credentials, | |
'/gremlin', | |
'wss', | |
); | |
const dc = new driver.DriverRemoteConnection(url, { headers }); | |
const graph = new structure.Graph(); | |
return graph.traversal().withRemote(dc); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment