Skip to content

Commit

Permalink
feat(sort): support orderBy argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Andras Toth committed Nov 20, 2015
1 parent 6de5888 commit 480ca7c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"lodash": "^3.10.1"
},
"peerDependencies": {
"graphql": "^0.4.12"
"graphql": "^0.4.13"
},
"devDependencies": {
"babel": "^5.8.29",
Expand Down
39 changes: 39 additions & 0 deletions src/e2e.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,45 @@ describe('e2e', () => {
{node: {name: 'Foo'}}
]);
});

it('should return results in ascending order', async function Test() {
const result = await graphql(schema, `{
viewer {
users(orderBy: NAME_ASC) {
edges {
node {
name
}
}
}
}
}`);

expect(result.data.viewer.users.edges).to.be.eql([
{ node: { name: 'Bar' } },
{ node: { name: 'Foo' } },
{ node: { name: 'Mother' } }
]);
});

it('should return results in descending order', async function Test() {
const result = await graphql(schema, `{
viewer {
users(orderBy: NAME_DESC, first: 2) {
edges {
node {
name
}
}
}
}
}`);

expect(result.data.viewer.users.edges).to.be.eql([
{ node: { name: 'Mother' } },
{ node: { name: 'Foo' } }
]);
});
});

describe('mutations', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/query/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ async function connectionFromModel(graffitiModel, args, info) {
return emptyConnection();
}

const {before, after, first, last, id, ...selector} = args;
const {before, after, first, last, id, orderBy = {_id: 1}, ...selector} = args;

const begin = getId(after);
const end = getId(before);
Expand All @@ -280,7 +280,7 @@ async function connectionFromModel(graffitiModel, args, info) {
const result = await getList(Collection, selector, {
skip: offset,
limit: limit,
sort: {_id: 1}
sort: orderBy
}, info);
const count = await getCount(Collection, selector);

Expand Down
38 changes: 34 additions & 4 deletions src/type/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
GraphQLList,
GraphQLObjectType,
GraphQLNonNull,
GraphQLScalarType
GraphQLScalarType,
GraphQLEnumType
} from 'graphql/type';
import {addHooks} from '../utils';
import GraphQLDate from './custom/date';
Expand Down Expand Up @@ -77,14 +78,43 @@ function setTypeFields(type, fields) {
type._typeConfig.fields = () => fields;
}

const orderByTypes = {};
function getOrderByType({name}, fields) {
if (!orderByTypes[name]) {
orderByTypes[name] = new GraphQLEnumType({
name: `orderBy${name}`,
values: reduce(fields, (values, field) => {
if (field.type instanceof GraphQLScalarType) {
const upperCaseName = field.name.toUpperCase();
values[`${upperCaseName}_ASC`] = {
[field.name]: 1
};
values[`${upperCaseName}_DESC`] = {
[field.name]: -1
};
}

return values;
}, {})
});
}
return orderByTypes[name];
}

function getArguments(type, args = {}) {
const fields = getTypeFields(type);

fields.orderBy = {
name: 'orderBy',
type: getOrderByType(type, fields)
};

return reduce(fields, (args, field) => {
if (field.type instanceof GraphQLNonNull && field.name !== 'id') {
field.type = field.type.ofType;
}

if (field.type instanceof GraphQLScalarType) {
if (field.type instanceof GraphQLScalarType || field.type instanceof GraphQLEnumType) {
args[field.name] = field;
}

Expand Down Expand Up @@ -115,7 +145,7 @@ export default function getType(graffitiModels, {name, description, fields}, roo
graphQLField.type = new GraphQLList(stringToGraphQLType(subtype));
if (reference) {
resolveReference[graphQLType.name][name] = {
name: name,
name,
type: reference,
args: connectionArgs,
resolve: addHooks((rootValue, args, info) => {
Expand All @@ -133,7 +163,7 @@ export default function getType(graffitiModels, {name, description, fields}, roo

if (reference && (graphQLField.type === GraphQLID || graphQLField.type === new GraphQLNonNull(GraphQLID))) {
resolveReference[graphQLType.name][name] = {
name: name,
name,
type: reference,
resolve: addHooks((rootValue, args, info) => {
const resolver = getOneResolver(graffitiModels[reference]);
Expand Down

0 comments on commit 480ca7c

Please sign in to comment.