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

refactor: remove neoSchema from OGM properties #256

Merged
merged 1 commit into from
Jun 15, 2021
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
11 changes: 0 additions & 11 deletions packages/ogm/src/classes/OGM.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* limitations under the License.
*/

import { printSchema } from "graphql";
import OGM from "./OGM";

describe("OGM", () => {
Expand All @@ -27,16 +26,6 @@ describe("OGM", () => {
});

describe("methods", () => {
describe("constructor", () => {
test("should evoke the filterDocument method", () => {
const ogm = new OGM({ typeDefs: `type User @auth {id:ID}` });

const printed = printSchema(ogm.neoSchema.schema);

expect(printed.includes("@auth")).toBeFalsy();
});
});

describe("checkNeo4jCompat", () => {
test("should neo4j-driver Driver missing", async () => {
// @ts-ignore
Expand Down
24 changes: 11 additions & 13 deletions packages/ogm/src/classes/OGM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,26 @@ import { filterDocument } from "../utils";
export type OGMConstructor = Neo4jGraphQLConstructor;

class OGM {
public neoSchema: Neo4jGraphQL;

public models: Model[];

public input: OGMConstructor;
checkNeo4jCompat: () => Promise<void>;

constructor(input: OGMConstructor) {
this.input = input;
const { typeDefs, ...rest } = input;

this.neoSchema = new Neo4jGraphQL({
const neoSchema = new Neo4jGraphQL({
...rest,
typeDefs: filterDocument(typeDefs),
});

this.models = this.neoSchema.nodes.map((n) => {
this.checkNeo4jCompat = function checkNeo4jCompat() {
return neoSchema.checkNeo4jCompat({
driver: rest.driver,
...(rest.config?.driverConfig ? { driverConfig: rest.config.driverConfig } : {}),
});
};

this.models = neoSchema.nodes.map((n) => {
const selectionSet = `
{
${[n.primitiveFields, n.scalarFields, n.enumFields, n.dateTimeFields].reduce(
Expand All @@ -50,7 +54,7 @@ class OGM {
`;

return new Model({
neoSchema: this.neoSchema,
neoSchema,
name: n.name,
selectionSet,
});
Expand All @@ -66,12 +70,6 @@ class OGM {

return found;
}

checkNeo4jCompat(): Promise<void> {
const { driver, config: { driverConfig } = {} } = this.input;

return this.neoSchema.checkNeo4jCompat({ driver, driverConfig });
}
}

export default OGM;
33 changes: 33 additions & 0 deletions packages/ogm/tests/integration/ogm.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,39 @@ describe("OGM", () => {
await session.close();
});

test("should filter the document and remove directives such as auth", async () => {
const session = driver.session();

const typeDefs = `
type Movie @auth(rules: [{ isAuthenticated: true }]){
id: ID
}
`;

const ogm = new OGM({ typeDefs, driver });

const id = generate({
charset: "alphabetic",
});

try {
await ogm.checkNeo4jCompat();

await session.run(`
CREATE (:Movie {id: "${id}"})
`);

const Movie = ogm.model("Movie");

const movies = await Movie?.find({ where: { id } });

// should return without error due to the fact auth should be removed
expect(movies).toEqual([{ id }]);
} finally {
await session.close();
}
});

describe("find", () => {
test("find a single node", async () => {
const session = driver.session();
Expand Down