Skip to content

Commit

Permalink
fix(schema): add nodeInterface to viewer field, resolve viewer from n…
Browse files Browse the repository at this point in the history
…ode field
  • Loading branch information
hardchor committed Nov 5, 2015
1 parent 6917d92 commit 03b3c05
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 25 deletions.
6 changes: 6 additions & 0 deletions src/model/viewer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const viewer = {
_type: 'Viewer',
id: 'viewer'
};

export default viewer;
9 changes: 7 additions & 2 deletions src/query/query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {forEach} from 'lodash';
import {fromGlobalId, toGlobalId} from 'graphql-relay';
import getFieldList from './projection';
import viewer from '../model/viewer';

function processId({id, _id = id}) {
// global or mongo id
Expand Down Expand Up @@ -68,6 +69,7 @@ function updateOne(Collection, args, info) {
if (res.ok) {
return getOne(Collection, {_id}, info);
}

return null;
});
}
Expand Down Expand Up @@ -179,8 +181,11 @@ function getFirst(Collection) {
function getIdFetcher(graffitiModels) {
return function idFetcher(obj, {id: globalId}, info) {
const {type, id} = fromGlobalId(globalId);
const Collection = graffitiModels[type].model;
if (Collection) {

if (type === 'Viewer') {
return viewer;
} else if (graffitiModels[type]) {
const Collection = graffitiModels[type].model;
return getOne(Collection, {id}, info);
}

Expand Down
8 changes: 8 additions & 0 deletions src/query/query.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ describe('query', () => {
const result = await idFetcher({}, {id});
expect(result).to.eql(resultObj);
});

it('should return the Viewer instance', async function getIdFetcherTest() {
const id = toGlobalId('Viewer', 'viewer');

const idFetcher = getIdFetcher(graffitiModels);
const result = await idFetcher({}, {id});
expect(result).to.eql({_type: 'Viewer', id: 'viewer'});
});
});

describe('getOneResolver', () => {
Expand Down
43 changes: 22 additions & 21 deletions src/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import {
import {
mutationWithClientMutationId,
connectionArgs,
connectionDefinitions
connectionDefinitions,
globalIdField
} from 'graphql-relay';
import {getModels} from './../model';
import {getTypes, nodeInterface} from './../type';
import {getTypes, GraphQLViewer, nodeInterface} from './../type';
import {
idToCursor,
getIdFetcher,
Expand All @@ -26,6 +27,7 @@ import {
getDeleteOneMutateHandler,
connectionFromModel
} from './../query';
import viewer from '../model/viewer';

const idField = {
name: 'id',
Expand Down Expand Up @@ -196,23 +198,22 @@ function getMutationField(graffitiModel, type, viewer) {
function getFields(graffitiModels, {mutation = true} = {}) {
const types = getTypes(graffitiModels);

const viewer = {
name: 'viewer',
type: new GraphQLObjectType({
name: 'Viewer',
fields: reduce(types, (fields, type, key) => {
type.name = type.name || key;
const graffitiModel = graffitiModels[type.name];
return {
...fields,
...getConnectionField(graffitiModel, type),
...getSingularQueryField(graffitiModel, type)
};
}, {
id: idField
})
}),
resolve: () => ({id: 'viewer'})
GraphQLViewer._typeConfig.fields = reduce(types, (fields, type, key) => {
type.name = type.name || key;
const graffitiModel = graffitiModels[type.name];
return {
...fields,
...getConnectionField(graffitiModel, type),
...getSingularQueryField(graffitiModel, type)
};
}, {
id: globalIdField('Viewer')
});

const viewerField = {
name: viewer,
type: GraphQLViewer,
resolve: () => viewer
};

const {queries, mutations} = reduce(types, ({queries, mutations}, type, key) => {
Expand All @@ -225,7 +226,7 @@ function getFields(graffitiModels, {mutation = true} = {}) {
},
mutations: {
...mutations,
...getMutationField(graffitiModel, type, viewer)
...getMutationField(graffitiModel, type, viewerField)
}
};
}, {
Expand All @@ -236,7 +237,7 @@ function getFields(graffitiModels, {mutation = true} = {}) {
const RootQuery = new GraphQLObjectType({
name: 'RootQuery',
fields: {
viewer,
viewer: viewerField,
node: {
name: 'node',
description: 'Fetches an object given its ID',
Expand Down
9 changes: 8 additions & 1 deletion src/schema/schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import query from '../query';
import model from '../model';
import type from '../type';

describe('field', () => {
describe('schema', () => {
const types = {
Qux: new GraphQLObjectType({
name: 'Qux',
Expand Down Expand Up @@ -120,6 +120,13 @@ describe('field', () => {
id: {
type: new GraphQLNonNull(GraphQLID)
}
},
type: {
_implementations: [
{
name: 'Viewer'
}
]
}
}
}
Expand Down
16 changes: 15 additions & 1 deletion src/type/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ const {nodeInterface} = nodeDefinitions(null, (obj) => {
return obj._type ? types[obj._type] : null;
});

function addType(name, type) {
types[name] = type;
}

const GraphQLViewer = new GraphQLObjectType({
name: 'Viewer',
interfaces: [nodeInterface]
});

// register viewer type
addType('Viewer', GraphQLViewer);

/**
* Returns a GraphQL type based on a String representation.
*/
Expand Down Expand Up @@ -121,7 +133,7 @@ export default function getType(graffitiModels, {name, description, fields}, roo

// register type
if (root) {
types[name] = GraphQLObjectTypeDefinition;
addType(name, GraphQLObjectTypeDefinition);
}

return GraphQLObjectTypeDefinition;
Expand Down Expand Up @@ -163,9 +175,11 @@ function getTypes(graffitiModels) {
}

export default {
GraphQLViewer,
GraphQLDate,
GraphQLGeneric,
getType,
getTypes,
addType,
nodeInterface
};

0 comments on commit 03b3c05

Please sign in to comment.