Skip to content

Commit

Permalink
feat(schema): add customQueries and customMutations to options
Browse files Browse the repository at this point in the history
  • Loading branch information
zopf authored and nodkz committed Mar 24, 2016
1 parent 5df7147 commit b2b841e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ function getMutationField(graffitiModel, type, viewer, hooks = {}, allowMongoIDM

if (field.type instanceof GraphQLList && field.type.ofType instanceof GraphQLObjectType) {
// TODO support objects nested in lists
} else if (!(field.type instanceof GraphQLObjectType) && field.name !== 'id' && field.name !== '__v' && (allowMongoIDMutation || field.name !== '_id')) {
} else if (!(field.type instanceof GraphQLObjectType)
&& field.name !== 'id' && field.name !== '__v'
&& (allowMongoIDMutation || field.name !== '_id')) {
inputFields[field.name] = field;
}

Expand Down Expand Up @@ -206,10 +208,13 @@ function getMutationField(graffitiModel, type, viewer, hooks = {}, allowMongoIDM
/**
* Returns query and mutation root fields
* @param {Array} graffitiModels
* @param {{Object, Boolean}} {hooks, mutation}
* @param {{Object, Boolean}} {hooks, mutation, allowMongoIDMutation}
* @return {Object}
*/
function getFields(graffitiModels, {hooks = {}, mutation = true, allowMongoIDMutation = false} = {}) {
function getFields(graffitiModels, {
hooks = {}, mutation = true, allowMongoIDMutation = false,
customQueries = {}, customMutations = {}
} = {}) {
const types = type.getTypes(graffitiModels);
const {viewer, singular} = hooks;

Expand Down Expand Up @@ -246,8 +251,8 @@ function getFields(graffitiModels, {hooks = {}, mutation = true, allowMongoIDMut
}
};
}, {
queries: {},
mutations: {}
queries: customQueries,
mutations: customMutations
});

const RootQuery = new GraphQLObjectType({
Expand Down
51 changes: 51 additions & 0 deletions src/schema/schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ describe('schema', () => {
type: GraphQLID
}
})
}),
TestQuery: new GraphQLObjectType({
name: 'TestQuery',
type: GraphQLString,
fields: () => ({
fetchCount: {
name: 'fetchCount',
type: GraphQLString,
resolve: () => '42'
}
})
})
};

Expand Down Expand Up @@ -160,5 +171,45 @@ describe('schema', () => {
expect(schema._queryType.name).to.be.equal('RootQuery');
expect(schema._mutationType).to.be.equal(undefined);
});

it('should return a GraphQL schema with custom queries', () => {
const graphQLType = types.TestQuery;
const customQueries = {
testQuery: {
type: graphQLType,
args: {
id: {
type: new GraphQLNonNull(GraphQLID)
}
}
}
};
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.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', () => {
const graphQLType = types.TestQuery;
const customMutations = {
testQuery: {
type: graphQLType,
args: {
id: {
type: new GraphQLNonNull(GraphQLID)
}
}
}
};
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.name).to.be.equal('testQuery');
expect(schema._mutationType._fields.testQuery.type._fields.fetchCount.resolve()).to.be.equal('42');
});
});
});

0 comments on commit b2b841e

Please sign in to comment.