Skip to content

Commit

Permalink
fix: use package json for useragent name and version (#271)
Browse files Browse the repository at this point in the history
* fix: use package json for useragent name and version

* fix: add userAgent support for <=4.2 and >=4.3 drivers
  • Loading branch information
danstarns authored Jun 22, 2021
1 parent af33c5e commit 6d1853d
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,5 @@ packages/package-tests/**/package-lock.json
!.yarn/sdks
!.yarn/versions
.pnp.*

tsconfig.tsbuildinfo
6 changes: 4 additions & 2 deletions packages/graphql/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
* limitations under the License.
*/

import * as pack from "../package.json";

const environment = {
NPM_PACKAGE_VERSION: process.env.NPM_PACKAGE_VERSION as string,
NPM_PACKAGE_NAME: process.env.NPM_PACKAGE_NAME as string,
NPM_PACKAGE_VERSION: pack.version,
NPM_PACKAGE_NAME: pack.name,
};

export default environment;
5 changes: 4 additions & 1 deletion packages/graphql/src/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"compilerOptions": {
"outDir": "../dist"
},
"exclude": ["**/*.test.ts"]
"exclude": ["**/*.test.ts"],
"references": [
{ "path": "../" } // for package.json import
]
}
5 changes: 4 additions & 1 deletion packages/graphql/src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"outDir": "../dist"
}
},
"references": [
{ "path": "../" } // for package.json import
]
}
9 changes: 9 additions & 0 deletions packages/graphql/src/utils/execute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Driver } from "neo4j-driver";
import { Neo4jGraphQL } from "../classes";
import { Context } from "../types";
import execute from "./execute";
import environment from "../environment";

describe("execute", () => {
test("should execute return records.toObject", async () => {
Expand Down Expand Up @@ -66,6 +67,8 @@ describe("execute", () => {
close: () => true,
};
},
// @ts-ignore
_config: {},
};

// @ts-ignore
Expand All @@ -82,6 +85,12 @@ describe("execute", () => {
});

expect(result).toEqual([{ title }]);
// @ts-ignore
expect(driver._userAgent).toEqual(`${environment.NPM_PACKAGE_NAME}/${environment.NPM_PACKAGE_VERSION}`);
// @ts-ignore
expect(driver._config.userAgent).toEqual(
`${environment.NPM_PACKAGE_NAME}/${environment.NPM_PACKAGE_VERSION}`
);
})
);
});
Expand Down
13 changes: 10 additions & 3 deletions packages/graphql/src/utils/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,16 @@ async function execute(input: {
}
}

// @ts-ignore: Required to set connection user agent
input.context.driver._userAgent = `${environment.NPM_PACKAGE_VERSION}/${environment.NPM_PACKAGE_NAME}`; // eslint-disable-line no-underscore-dangle
const userAgent = `${environment.NPM_PACKAGE_NAME}/${environment.NPM_PACKAGE_VERSION}`;

// @ts-ignore: below
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);

Expand All @@ -67,7 +75,6 @@ async function execute(input: {
}

try {
// input.context.neoSchema.debug(`Cypher: ${input.cypher}\nParams: ${JSON.stringify(input.params, null, 2)}`);
debug(
"%s",
`About to execute Cypher:\nCypher:\n${input.cypher}\nParams:\n${JSON.stringify(input.params, null, 2)}`
Expand Down
3 changes: 2 additions & 1 deletion packages/graphql/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"types": ["node", "jest"]
"types": ["node", "jest"],
"resolveJsonModule": true
},
"references": [{ "path": "../src/tsconfig.json" }]
}
9 changes: 9 additions & 0 deletions packages/graphql/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": ".",
"outDir": ".",
"composite": true
},
"files": ["package.json"]
}
3 changes: 2 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"strict": true,
"target": "es6",
// TODO Refactor so that noImplicitAny is no longer needed
"noImplicitAny": false
"noImplicitAny": false,
"resolveJsonModule": true
}
}

0 comments on commit 6d1853d

Please sign in to comment.