Skip to content

Write details to transaction metadata instead of useragent #1143

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

Merged
Merged
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
14 changes: 0 additions & 14 deletions packages/graphql/src/utils/execute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,6 @@ describe("execute", () => {
});

expect(executeResult.records).toEqual([{ title }]);
// @ts-ignore
expect(driver._userAgent).toBe(`${environment.NPM_PACKAGE_NAME}/${environment.NPM_PACKAGE_VERSION}`);
// @ts-ignore
expect(driver._config.userAgent).toBe(
`${environment.NPM_PACKAGE_NAME}/${environment.NPM_PACKAGE_VERSION}`
);
})
);
});
Expand Down Expand Up @@ -178,10 +172,6 @@ describe("execute", () => {
});

expect(executeResult.records).toEqual([{ title }]);
// @ts-ignore
expect(driver._userAgent).toBe(`${environment.NPM_PACKAGE_NAME}/${environment.NPM_PACKAGE_VERSION}`);
// @ts-ignore
expect(driver._config.userAgent).toBe(`${environment.NPM_PACKAGE_NAME}/${environment.NPM_PACKAGE_VERSION}`);
});

test("one of each query option", async () => {
Expand Down Expand Up @@ -264,10 +254,6 @@ describe("execute", () => {
});

expect(executeResult.records).toEqual([{ title }]);
// @ts-ignore
expect(driver._userAgent).toBe(`${environment.NPM_PACKAGE_NAME}/${environment.NPM_PACKAGE_VERSION}`);
// @ts-ignore
expect(driver._config.userAgent).toBe(`${environment.NPM_PACKAGE_NAME}/${environment.NPM_PACKAGE_VERSION}`);
});
});
});
Expand Down
39 changes: 24 additions & 15 deletions packages/graphql/src/utils/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,6 @@ async function execute(input: {
}
}

const userAgent = `${environment.NPM_PACKAGE_NAME}/${environment.NPM_PACKAGE_VERSION}`;

// @ts-ignore: below
// eslint-disable-next-line no-underscore-dangle
if (input.context.driver?._config) {
// @ts-ignore: (driver >= 4.3)
input.context.driver._config.userAgent = userAgent; // eslint-disable-line no-underscore-dangle
}

// @ts-ignore: (driver <= 4.2)
input.context.driver._userAgent = userAgent; // eslint-disable-line no-underscore-dangle

const session = input.context.driver.session(sessionParams);

// Its really difficult to know when users are using the `auth` param. For Simplicity it better to do the check here
Expand All @@ -100,9 +88,30 @@ async function execute(input: {
try {
debug("%s", `About to execute Cypher:\nCypher:\n${cypher}\nParams:\n${JSON.stringify(input.params, null, 2)}`);

const result: QueryResult = await session[
`${input.defaultAccessMode.toLowerCase()}Transaction`
]((tx: Transaction) => tx.run(cypher, input.params));
const app = `${environment.NPM_PACKAGE_NAME}@${environment.NPM_PACKAGE_VERSION}`;

let result: QueryResult | undefined;
const transactionWork = (tx: Transaction) => tx.run(cypher, input.params);
const transactionConfig = {
metadata: {
app,
type: "user-transpiled",
},
};

switch (input.defaultAccessMode) {
case "READ":
result = await session.readTransaction(transactionWork, transactionConfig);
break;
case "WRITE":
result = await session.writeTransaction(transactionWork, transactionConfig);
break;
// no default
}

if (!result) {
throw new Error("Unable to execute query against Neo4j database");
}

const records = result.records.map((r) => r.toObject());

Expand Down