From 4fda024acfabf0bb018598230b7b67168eeb6cec Mon Sep 17 00:00:00 2001 From: Andras Toth Date: Fri, 16 Oct 2015 17:16:02 +0200 Subject: [PATCH] feat(schema): use connection type on plural fields on viewer query --- src/e2e.spec.js | 14 ++++++++++-- src/schema/schema.js | 53 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/src/e2e.spec.js b/src/e2e.spec.js index 824a517..4d9fd26 100644 --- a/src/e2e.spec.js +++ b/src/e2e.spec.js @@ -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'}} + ]); }); }); diff --git a/src/schema/schema.js b/src/schema/schema.js index 439bba5..5af8d5b 100644 --- a/src/schema/schema.js +++ b/src/schema/schema.js @@ -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 { @@ -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]: { @@ -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) => { @@ -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; @@ -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: () => ({}) },