Skip to content

Commit

Permalink
feat(schema): allow to use functions for custom mutations and queries…
Browse files Browse the repository at this point in the history
… to be able to use defined types
  • Loading branch information
I committed May 2, 2016
1 parent d58b449 commit 0cb244f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 23 deletions.
38 changes: 20 additions & 18 deletions src/e2e.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-unused-expressions */

import {expect} from 'chai';
import {spy} from 'sinon';

Expand Down Expand Up @@ -261,7 +263,7 @@ describe('e2e', () => {
}`);

const {id, users} = result.data.viewer;
expect(id).to.be.ok; // eslint-disable-line
expect(id).to.be.ok;
expect(users.count).to.be.equal(3);

expect(users.edges).to.containSubset([
Expand Down Expand Up @@ -445,7 +447,7 @@ describe('e2e', () => {
}
`);

expect(result.errors).not.to.be.ok; // eslint-disable-line
expect(result.errors).not.to.be.ok;
});

it('should update data', async function Test() {
Expand Down Expand Up @@ -557,58 +559,58 @@ describe('e2e', () => {
pre.reset();
post.reset();

expect(pre.called).to.be.false; // eslint-disable-line
expect(post.called).to.be.false; // eslint-disable-line
expect(pre.called).to.be.false;
expect(post.called).to.be.false;
await graphql(schema, `{
viewer {
users {
count
}
}
}`);
expect(pre.called).to.be.true; // eslint-disable-line
expect(post.called).to.be.true; // eslint-disable-line
expect(pre.called).to.be.true;
expect(post.called).to.be.true;
});

it('should call singular hooks on a singular query', async () => {
const {pre, post} = hooks.singular;
pre.reset();
post.reset();

expect(pre.called).to.be.false; // eslint-disable-line
expect(post.called).to.be.false; // eslint-disable-line
expect(pre.called).to.be.false;
expect(post.called).to.be.false;
await graphql(schema, `{
user(id: "${user2._id}") {
_id
}
}`);
expect(pre.called).to.be.true; // eslint-disable-line
expect(post.called).to.be.true; // eslint-disable-line
expect(pre.called).to.be.true;
expect(post.called).to.be.true;
});

it('should call plural hooks on a plural query', async () => {
const {pre, post} = hooks.plural;
pre.reset();
post.reset();

expect(pre.called).to.be.false; // eslint-disable-line
expect(post.called).to.be.false; // eslint-disable-line
expect(pre.called).to.be.false;
expect(post.called).to.be.false;
await graphql(schema, `{
users(age: 28) {
_id
}
}`);
expect(pre.called).to.be.true; // eslint-disable-line
expect(post.called).to.be.true; // eslint-disable-line
expect(pre.called).to.be.true;
expect(post.called).to.be.true;
});

it('should call mutation hooks on a mutation', async () => {
const {pre, post} = hooks.mutation;
pre.reset();
post.reset();

expect(pre.called).to.be.false; // eslint-disable-line
expect(post.called).to.be.false; // eslint-disable-line
expect(pre.called).to.be.false;
expect(post.called).to.be.false;
await graphql(schema, `
mutation addUserMutation {
addUser(input: {name: "Test User", clientMutationId: "1"}) {
Expand All @@ -620,8 +622,8 @@ describe('e2e', () => {
}
}
`);
expect(pre.called).to.be.true; // eslint-disable-line
expect(post.called).to.be.true; // eslint-disable-line
expect(pre.called).to.be.true;
expect(post.called).to.be.true;
});
});
});
Expand Down
8 changes: 5 additions & 3 deletions src/schema/schema.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {reduce, isArray} from 'lodash';
import {reduce, isArray, isFunction, mapValues} from 'lodash';
import {
GraphQLList,
GraphQLNonNull,
Expand Down Expand Up @@ -258,8 +258,10 @@ function getFields(graffitiModels, {
}
};
}, {
queries: customQueries,
mutations: customMutations
queries: isFunction(customQueries) ?
customQueries(mapValues(types, (type) => createInputObject(type))) : customQueries,
mutations: isFunction(customMutations) ?
customMutations(mapValues(types, (type) => createInputObject(type))) : customMutations
});

const RootQuery = new GraphQLObjectType({
Expand Down
52 changes: 50 additions & 2 deletions src/schema/schema.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-unused-expressions */

import {expect} from 'chai';
import {
GraphQLObjectType,
Expand Down Expand Up @@ -165,7 +167,7 @@ describe('schema', () => {
expect(schema._mutationType).to.be.equal(undefined);
});

it('should return a GraphQL schema with custom queries', () => {
it('should return a GraphQL schema with custom queries (object)', () => {
const graphQLType = types.TestQuery;
const customQueries = {
testQuery: {
Expand All @@ -185,7 +187,7 @@ describe('schema', () => {
expect(schema._queryType._fields.testQuery.type._fields.fetchCount.resolve()).to.be.equal('42');
});

it('should return a GraphQL schema with custom mutations', () => {
it('should return a GraphQL schema with custom mutations (object)', () => {
const graphQLType = types.TestQuery;
const customMutations = {
testQuery: {
Expand All @@ -204,5 +206,51 @@ describe('schema', () => {
expect(schema._mutationType._fields.testQuery.name).to.be.equal('testQuery');
expect(schema._mutationType._fields.testQuery.type._fields.fetchCount.resolve()).to.be.equal('42');
});

it('should return a GraphQL schema with custom queries (function)', () => {
const graphQLType = types.TestQuery;
const customQueries = (types) => ({
testQuery: {
type: graphQLType,
args: {
qux: {
type: types.Qux
}
}
}
});
const schema = getSchema({}, {customQueries});
expect(schema).instanceOf(GraphQLSchema);
expect(schema._queryType.name).to.be.equal('RootQuery');
expect(schema._mutationType.name).to.be.equal('RootMutation');
expect(schema._queryType._fields.testQuery.args[0].name).to.be.equal('qux');
expect(schema._queryType._fields.testQuery.args[0].type._fields.bar).to.be.defined;
expect(schema._queryType._fields.testQuery.args[0].type._fields.baz).to.be.defined;
expect(schema._queryType._fields.testQuery.name).to.be.equal('testQuery');
expect(schema._queryType._fields.testQuery.type._fields.fetchCount.resolve()).to.be.equal('42');
});

it('should return a GraphQL schema with custom mutations (function)', () => {
const graphQLType = types.TestQuery;
const customMutations = (types) => ({
testQuery: {
type: graphQLType,
args: {
qux: {
type: types.Qux
}
}
}
});
const schema = getSchema({}, {customMutations});
expect(schema).instanceOf(GraphQLSchema);
expect(schema._queryType.name).to.be.equal('RootQuery');
expect(schema._mutationType.name).to.be.equal('RootMutation');
expect(schema._mutationType._fields.testQuery.args[0].name).to.be.equal('qux');
expect(schema._mutationType._fields.testQuery.args[0].type._fields.bar).to.be.defined;
expect(schema._mutationType._fields.testQuery.args[0].type._fields.baz).to.be.defined;
expect(schema._mutationType._fields.testQuery.name).to.be.equal('testQuery');
expect(schema._mutationType._fields.testQuery.type._fields.fetchCount.resolve()).to.be.equal('42');
});
});
});

0 comments on commit 0cb244f

Please sign in to comment.