Skip to content

Commit

Permalink
Added access to all arguments from GraphQL resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorDiaconu committed Mar 16, 2020
1 parent 70c5f84 commit 84a98d4
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 75 deletions.
35 changes: 35 additions & 0 deletions .npm/package/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions __tests__/morpher/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { expose, db } from 'meteor/cultofcoders:apollo';

expose({
users: {
type: 'User',
collection: () => db.users,
update: () => true,
insert: () => true,
remove: () => true,
find: () => true,
},
});
94 changes: 83 additions & 11 deletions __tests__/morpher/server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,84 @@
import { expose, db } from 'meteor/cultofcoders:apollo';

expose({
users: {
type: 'User',
collection: () => db.users,
update: () => true,
insert: () => true,
remove: () => true,
find: () => true,
},
import "./bootstrap";
import { db } from "meteor/cultofcoders:apollo";
import setupMutations from "../../server/morpher/setupMutations";
import setupDataFetching from "../../server/morpher/setupDataFetching";
import { assert } from "chai";

describe.only("Unit tests", () => {
it("Should send all the right args to the config functions", () => {
const testRest = [
{},
{
payload: JSON.stringify({
document: { a: 1 },
selector: {},
modifier: {}
})
},
{},
{}
];

const checkArray = rest => {
assert.equal(4, rest.length);

for (let i = 0; i < testRest.length; i++) {
if (i === 1) {
continue;
}
assert.strictEqual(testRest[i], rest[i]);
}

// assert.equal(rest[1], {});
};

let count = 0;

const { Mutation } = setupMutations(
{
insert(ctx, special, ...rest) {
checkArray(rest);
count++;
},
update(ctx, special, ...rest) {
checkArray(rest);
count++;
},
remove(ctx, special, ...rest) {
checkArray(rest);
count++;
}
},
"test",
"TestEntity",
() => db.users
);

const { Query } = setupDataFetching(
{
find(ctx, special, ...rest) {
count++;
checkArray(rest);
}
},
"test",
"TestEntity",
() => db.users
);

const safeRun = fn => {
try {
fn();
} catch (e) {}
};
safeRun(() => Mutation.testInsert(...testRest));
safeRun(() => Mutation.testUpdate(...testRest));
safeRun(() => Mutation.testRemove(...testRest));

safeRun(() => Query.test(...testRest));
safeRun(() => Query.testSingle(...testRest));
safeRun(() => Query.testCount(...testRest));

assert.equal(count, 6);
});
});
12 changes: 6 additions & 6 deletions docs/morpher.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ expose({

```js
// Then on the client
import db, { setClient } from 'apollo-morpher';
import db, { setClient } from "apollo-morpher";

// Set your Apollo client
setClient(apolloClient);
Expand All @@ -67,31 +67,31 @@ const fields = {
firstName: 1,
lastName: 1,
lastInvoices: {
total: 1,
},
total: 1
}
};

// Or you could also define the fields in GraphQL style `firstName`

db.users
.find(fields, {
filters: {},
options: {},
options: {}
})
.then(users => {});

// find equivallent .findOne()
db.users
.findOne(fields, {
filters: { _id: 'XXX' },
filters: { _id: "XXX" }
})
.then(user => {});

// and for pagination purposes to retrieve the count
db.users
.count({
filters,
options,
options
})
.then(count => {});
```
Expand Down
54 changes: 27 additions & 27 deletions package.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
Package.describe({
name: 'cultofcoders:apollo',
version: '0.9.3',
name: "cultofcoders:apollo",
version: "0.9.3",
// Brief, one-line summary of the package.
summary: 'Meteor & Apollo integration',
summary: "Meteor & Apollo integration",
// URL to the Git repository containing the source code for this package.
git: 'https://github.com/cult-of-coders/apollo.git',
git: "https://github.com/cult-of-coders/apollo.git",
// By default, Meteor will default to using README.md for documentation.
// To avoid submitting documentation, set this field to null.
documentation: 'README.md',
documentation: "README.md"
});

Package.onUse(function(api) {
api.versionsFrom('1.3');
api.use('ecmascript');
api.use('check');
api.use('ejson');
api.use('mongo');

api.use('tmeasday:check-npm-versions@0.3.2');
api.use('cultofcoders:grapher@1.3.13');
api.use('cultofcoders:grapher-schema-directives@0.1.8');
api.use('accounts-base', { weak: true });

api.mainModule('client/index.js', 'client');
api.mainModule('server/index.js', 'server');
api.versionsFrom("1.3");
api.use("ecmascript");
api.use("check");
api.use("ejson");
api.use("mongo");

api.use("tmeasday:check-npm-versions@0.3.2");
api.use("cultofcoders:grapher@1.3.13");
api.use("cultofcoders:grapher-schema-directives@0.1.8");
api.use("accounts-base", { weak: true });

api.mainModule("client/index.js", "client");
api.mainModule("server/index.js", "server");
});

Npm.depends({
cookie: '0.4.0',
cookie: "0.4.0"
});

Package.onTest(function(api) {
api.use('cultofcoders:apollo');
api.use("cultofcoders:apollo");

var packages = [
'ecmascript',
'accounts-password',
'cultofcoders:apollo-accounts@3.4.0',
'mongo',
"ecmascript",
"accounts-password",
"cultofcoders:apollo-accounts@3.4.0",
"mongo"
];

api.use(['meteortesting:mocha']);
api.use(["meteortesting:mocha"]);

api.use(packages);

api.mainModule('__tests__/server.js', 'server');
api.mainModule('__tests__/client.js', 'client');
api.mainModule("__tests__/server.js", "server");
api.mainModule("__tests__/client.js", "client");
});
28 changes: 18 additions & 10 deletions server/morpher/setupDataFetching.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EJSON } from 'meteor/ejson';
import { DOCUMENTATION_FETCH } from './docs';
import { EJSON } from "meteor/ejson";
import { DOCUMENTATION_FETCH } from "./docs";

export default function setupDataFetching(config, name, type, collection) {
let Query = {};
Expand All @@ -25,25 +25,33 @@ export default function setupDataFetching(config, name, type, collection) {
const resolveSelectors = (_, { params }, ctx, ast) => {
let astToQueryOptions;

if (typeof config.find === 'function') {
if (typeof config.find === "function") {
params = Object.assign(
{
filters: {},
options: {},
options: {}
},
params
);

let astToQueryOptions = config.find.call(null, ctx, params, ast);
let astToQueryOptions = config.find.call(
null,
ctx,
params,
_,
{ params },
ctx,
ast
);
if (astToQueryOptions === false) {
throw new Error('Unauthorized');
throw new Error("Unauthorized");
}
}

if (astToQueryOptions === undefined || astToQueryOptions === true) {
astToQueryOptions = {
$filters: params.filters || {},
$options: params.options || {},
$options: params.options || {}
};
}

Expand All @@ -61,18 +69,18 @@ export default function setupDataFetching(config, name, type, collection) {

Query = {
[name]: fn,
[name + 'Count'](_, { payload }, ctx, ast) {
[name + "Count"](_, { payload }, ctx, ast) {
const params = EJSON.parse(payload);
const astToQueryOptions = resolveSelectors(_, { params }, ctx, ast);

return collection()
.find(astToQueryOptions.$filters || {})
.count();
},
[name + 'Single'](_, args, ctx, ast) {
[name + "Single"](_, args, ctx, ast) {
const result = fn.call(null, _, args, ctx, ast);
return result[0] || null;
},
}
};

/**
Expand Down
Loading

0 comments on commit 84a98d4

Please sign in to comment.