Skip to content

Commit

Permalink
feat(schema): use connection type on plural fields on viewer query
Browse files Browse the repository at this point in the history
  • Loading branch information
tothandras committed Oct 16, 2015
1 parent 367305c commit 4fda024
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
14 changes: 12 additions & 2 deletions src/e2e.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,22 @@ describe('e2e', () => {
const result = await graphql(schema, `{
viewer {
users {
name
edges {
cursor
node {
name
}
}
}
}
}`);

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

Expand Down
53 changes: 47 additions & 6 deletions src/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
GraphQLScalarType,
GraphQLBoolean
} from 'graphql';
import {mutationWithClientMutationId} from 'graphql-relay';
import {
mutationWithClientMutationId,
connectionArgs,
connectionDefinitions
} from 'graphql-relay';
import {getModels} from './../model';
import {getTypes, nodeInterface} from './../type';
import {
Expand All @@ -17,13 +21,13 @@ import {
getListResolver,
getAddOneMutateHandler,
getUpdateOneMutateHandler,
getDeleteOneMutateHandler
getDeleteOneMutateHandler,
connectionFromModel
} from './../query';

function getQueryField(graffitiModel, type) {
function getSingularQueryField(graffitiModel, type) {
const {name} = type;
const singularName = name.toLowerCase();
const pluralName = `${name.toLowerCase()}s`;

return {
[singularName]: {
Expand All @@ -35,7 +39,15 @@ function getQueryField(graffitiModel, type) {
}
},
resolve: getOneResolver(graffitiModel)
},
}
};
}

function getPluralQueryField(graffitiModel, type) {
const {name} = type;
const pluralName = `${name.toLowerCase()}s`;

return {
[pluralName]: {
type: new GraphQLList(type),
args: reduce(type._typeConfig.fields(), (args, field) => {
Expand Down Expand Up @@ -63,6 +75,27 @@ function getQueryField(graffitiModel, type) {
};
}

function getQueryField(graffitiModel, type) {
return {
...getSingularQueryField(graffitiModel, type),
...getPluralQueryField(graffitiModel, type)
};
}

function getConnectionField(graffitiModel, type) {
const {name} = type;
const pluralName = `${name.toLowerCase()}s`;
const {connectionType} = connectionDefinitions({name: name, nodeType: type});

return {
[pluralName]: {
args: connectionArgs,
type: connectionType,
resolve: (rootValue, args, info) => connectionFromModel(graffitiModel, args, info)
}
};
}

function getMutationField(graffitiModel, type) {
const {name} = type;

Expand Down Expand Up @@ -153,7 +186,15 @@ function getFields(graffitiModels, {mutation} = {mutation: true}) {
name: 'viewer',
type: new GraphQLObjectType({
name: 'Viewer',
fields: queries
fields: reduce(types, (fields, type, key) => {
type.name = type.name || key;
const graffitiModel = graffitiModels[type.name];
return {
...fields,
...getConnectionField(graffitiModel, type),
...getSingularQueryField(graffitiModel, type)
};
}, {})
}),
resolve: () => ({})
},
Expand Down

0 comments on commit 4fda024

Please sign in to comment.