Skip to content

Commit

Permalink
fix mutators document fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-burel committed Apr 26, 2021
1 parent 1e4417c commit 4fd2d8b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/graphql/server/resolvers/mutators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ export const updateMutator = async <TModel extends VulcanDocument>({

// get original document from database or arguments
const connector = getModelConnector(context, model);
const currentDocument = await connector.findOne(model, selector);
const currentDocument = await connector.findOne(selector);

if (!currentDocument) {
throw new Error(
Expand Down Expand Up @@ -429,7 +429,7 @@ export const deleteMutator = async <TModel extends VulcanDocument>({
const connector = getModelConnector<TModel>(context, model);

// get document from database
let document = await connector.findOne(model, selector);
let document = await connector.findOne(selector);

if (!document) {
throw new Error(
Expand Down
75 changes: 62 additions & 13 deletions packages/graphql/server/tests/mutators/mutators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,24 @@ describe("graphql/resolvers/mutators", function () {

describe("create and update mutator", () => {
// create fake context
const defaultContext: {
let defaultContext: {
Foo: { connector: Partial<Connector>; model: VulcanGraphqlModel };
} = {
Foo: {
model: Foo,
connector: {
create: async () => "1", // returns the new doc id
findOneById: async () => ({
id: "1",
}),
findOne: async () => ({ id: "1" }),
update: async () => ({ id: "1" }),
},
},
};
beforeEach(() => {
defaultContext = {
Foo: {
model: Foo,
connector: {
create: async () => "1", // returns the new doc id
findOneById: async () => ({
id: "1",
}),
findOne: async () => ({ id: "1" }),
update: async () => ({ id: "1" }),
},
},
};
});
test("can run createMutator", async function () {
const { data: resultDocument } = await createMutator({
...createArgs,
Expand Down Expand Up @@ -137,13 +140,46 @@ describe("graphql/resolvers/mutators", function () {
});
expect(data).toEqual(dataOriginal);
});
test("update mutator should pass the right selector to get the current document", async () => {
const data = { _id: "1", foo: "fooUpdate" };
defaultContext.Foo.connector.findOne = jest.fn(async () => data);
await updateMutator({
...updateArgs,
dataId: data._id,
context: defaultContext,
data,
});
expect(defaultContext.Foo.connector.findOne).toHaveBeenCalledWith({
_id: "1",
});
});
});
describe("delete mutator", () => {
// create fake context
let defaultContext: {
Foo: { connector: Partial<Connector>; model: VulcanGraphqlModel };
};
const defaultParams = {
model: Foo,
context: {},
asAdmin: true, // bypass field restriction
};
beforeEach(() => {
defaultContext = {
Foo: {
model: Foo,
connector: {
create: async () => "1", // returns the new doc id
findOneById: async () => ({
id: "1",
}),
findOne: async () => ({ id: "1" }),
update: async () => ({ id: "1" }),
delete: async () => {},
},
},
};
});
test("refuse deletion if selector is empty", async () => {
const emptySelector = {};

Expand Down Expand Up @@ -201,6 +237,19 @@ describe("graphql/resolvers/mutators", function () {
deleteMutator({ ...params, selector: validSlugSelector })
).resolves.toEqual({ data: foo });
});

test("pass the right selector to get the current document", async () => {
const data = { _id: "1", foo: "fooUpdate" };
defaultContext.Foo.connector.findOne = jest.fn(async () => data);
await deleteMutator({
...defaultParams,
context: defaultContext,
selector: { _id: "1" },
});
expect(defaultContext.Foo.connector.findOne).toHaveBeenCalledWith({
_id: "1",
});
});
});

describe("field onCreate/onUpdate callbacks", () => {
Expand Down

0 comments on commit 4fd2d8b

Please sign in to comment.