Skip to content

Commit

Permalink
fix(schema): add arguments to connections based on the fields of the …
Browse files Browse the repository at this point in the history
…type
  • Loading branch information
tothandras committed Nov 7, 2015
1 parent f1e1ace commit b6f6684
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 25 deletions.
22 changes: 22 additions & 0 deletions src/e2e.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,28 @@ describe('e2e', () => {
{node: {name: 'Bar'}}
]);
});

it('should filter connections by arguments', async function Test() {
const result = await graphql(schema, `{
viewer {
users(name: "Foo") {
count
edges {
node {
name
}
}
}
}
}`);

const {users} = result.data.viewer;
expect(users.count).to.be.eql(1);

expect(users.edges).to.containSubset([
{node: {name: 'Foo'}}
]);
});
});

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

const {before, after, first, last, id} = args;
const {before, after, first, last, id, ...selector} = args;

const begin = getId(after);
const end = getId(before);

const offset = (first - last) || 0;
const limit = last || first;

const selector = {};

if (id) {
selector.id = id;
}
Expand Down
27 changes: 11 additions & 16 deletions src/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
GraphQLID,
GraphQLObjectType,
GraphQLSchema,
GraphQLScalarType,
GraphQLBoolean,
GraphQLFloat
} from 'graphql';
Expand All @@ -16,7 +15,13 @@ import {
globalIdField
} from 'graphql-relay';
import {getModels} from './../model';
import {getTypes, GraphQLViewer, nodeInterface} from './../type';
import {
getTypes,
GraphQLViewer,
nodeInterface,
getTypeFields,
getArguments
} from './../type';
import {
idToCursor,
getIdFetcher,
Expand Down Expand Up @@ -56,17 +61,7 @@ function getPluralQueryField(graffitiModel, type) {
return {
[pluralName]: {
type: new GraphQLList(type),
args: reduce(type._typeConfig.fields(), (args, field) => {
if (field.type instanceof GraphQLNonNull && field.name !== 'id') {
field.type = field.type.ofType;
}

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

return args;
}, {
args: getArguments(type, {
id: {
type: new GraphQLList(GraphQLID),
description: `The ID of a ${name}`
Expand Down Expand Up @@ -100,7 +95,7 @@ function getConnectionField(graffitiModel, type) {

return {
[pluralName]: {
args: connectionArgs,
args: getArguments(type, connectionArgs),
type: connectionType,
resolve: (rootValue, args, info) => connectionFromModel(graffitiModel, args, info)
}
Expand All @@ -110,7 +105,7 @@ function getConnectionField(graffitiModel, type) {
function getMutationField(graffitiModel, type, viewer) {
const {name} = type;

const fields = type._typeConfig.fields();
const fields = getTypeFields(type);
const inputFields = reduce(fields, (inputFields, field) => {
if (field.type instanceof GraphQLObjectType) {
if (field.type.name.endsWith('Connection')) {
Expand All @@ -122,7 +117,7 @@ function getMutationField(graffitiModel, type, viewer) {

// TODO support objects
// else {
// args = {...args, ...field.type._typeConfig.fields()};
// args = {...args, ...getTypeFields(field.type)};
// }
}

Expand Down
42 changes: 36 additions & 6 deletions src/type/type.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {reduce, forEach} from 'lodash';

import {
reduce,
forEach,
isFunction
} from 'lodash';
import {
globalIdField,
connectionArgs,
Expand All @@ -13,7 +16,8 @@ import {
GraphQLID,
GraphQLList,
GraphQLObjectType,
GraphQLNonNull
GraphQLNonNull,
GraphQLScalarType
} from 'graphql/type';
import GraphQLDate from './custom/date';
import GraphQLBuffer from './custom/buffer';
Expand Down Expand Up @@ -63,6 +67,30 @@ function stringToGraphQLType(type) {
}
}

function getTypeFields(type) {
const fields = type._typeConfig.fields;
return isFunction(fields) ? fields() : fields;
}

function setTypeFields(type, fields) {
type._typeConfig.fields = () => fields;
}

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

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

return args;
}, args);
}

// holds references to fields that later has to be resolved
const resolveReference = {};

Expand Down Expand Up @@ -149,7 +177,7 @@ function getTypes(graffitiModels) {
forEach(resolveReference, (fields, typeName) => {
const type = types[typeName];
if (type) {
const typeFields = type._typeConfig.fields();
const typeFields = getTypeFields(type);
forEach(fields, (field, fieldName) => {
if (field.args === connectionArgs) {
// it's a connection
Expand All @@ -167,7 +195,7 @@ function getTypes(graffitiModels) {

typeFields[fieldName] = field;
});
type._typeConfig.fields = () => typeFields;
setTypeFields(type, typeFields);
}
});

Expand All @@ -181,5 +209,7 @@ export default {
getType,
getTypes,
addType,
nodeInterface
nodeInterface,
getTypeFields,
getArguments
};

0 comments on commit b6f6684

Please sign in to comment.