Skip to content

Commit

Permalink
feat(mutation): add viewer and edge fields to payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
tothandras committed Oct 19, 2015
1 parent 4b71175 commit e0031ef
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 44 deletions.
49 changes: 32 additions & 17 deletions src/e2e.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,44 +223,55 @@ describe('e2e', () => {
const result = await graphql(schema, `
mutation addUserMutation {
addUser(input: {name: "Test User", clientMutationId: "1"}) {
_id
name
changedUserEdge {
node {
_id
name
}
}
}
}
`);

expect(typeof result.data.addUser._id).to.be.eql('string');
expect(result).to.containSubset({
data: {
addUser: {
name: 'Test User'
}
}
});
const node = result.data.addUser.changedUserEdge.node;
expect(typeof node._id).to.be.equal('string');
expect(node.name).to.be.equal('Test User');
});

it('should update data', async function Test() {
let result = await graphql(schema, `
mutation addUserMutation {
addUser(input: {name: "Test User", clientMutationId: "1"}) {
_id
name
changedUserEdge {
node {
_id
}
}
}
}
`);
const id = result.data.addUser._id;
const id = result.data.addUser.changedUserEdge.node._id;

result = await graphql(schema, `
mutation updateUserMutation {
updateUser(input: {id: "${id}", name: "Updated Test User", clientMutationId: "2"}) {
name
changedUserEdge {
node {
_id
name
}
}
}
}
`);
expect(result).to.containSubset({
data: {
updateUser: {
name: 'Updated Test User'
changedUserEdge: {
node: {
name: 'Updated Test User'
}
}
}
}
});
Expand All @@ -270,11 +281,15 @@ describe('e2e', () => {
let result = await graphql(schema, `
mutation addUserMutation {
addUser(input: {name: "Test User", clientMutationId: "1"}) {
_id
changedUserEdge {
node {
_id
}
}
}
}
`);
const id = result.data.addUser._id;
const id = result.data.addUser.changedUserEdge.node._id;

result = await graphql(schema, `
mutation deleteUserMutation {
Expand Down
1 change: 1 addition & 0 deletions src/query/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ async function connectionFromModel(graffitiModel, args, info) {

export default {
_idToCursor: idToCursor,
idToCursor,
getIdFetcher,
getOneResolver,
getAddOneMutateHandler,
Expand Down
73 changes: 46 additions & 27 deletions src/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import {getModels} from './../model';
import {getTypes, nodeInterface} from './../type';
import {
idToCursor,
getIdFetcher,
getOneResolver,
getListResolver,
Expand Down Expand Up @@ -96,14 +97,14 @@ function getConnectionField(graffitiModel, type) {
};
}

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

const allFields = type._typeConfig.fields();
const args = reduce(allFields, (args, field) => {
const fields = type._typeConfig.fields();
const inputFields = reduce(fields, (inputFields, field) => {
if (field.type instanceof GraphQLObjectType) {
if (field.type.name.endsWith('Connection')) {
args[field.name] = {
inputFields[field.name] = {
name: field.name,
type: new GraphQLList(GraphQLID)
};
Expand All @@ -114,32 +115,48 @@ function getMutationField(graffitiModel, type) {
// }
}
if (!(field.type instanceof GraphQLObjectType) && field.name !== 'id' && !field.name.startsWith('_')) {
args[field.name] = field;
inputFields[field.name] = field;
}
return args;
return inputFields;
}, {});

const Name = name[0].toUpperCase() + name.slice(1);
const edgeName = `changed${Name}Edge`;
const outputFields = {
viewer,
[edgeName]: {
type: connectionDefinitions({name: edgeName, nodeType: new GraphQLObjectType({
name: edgeName,
fields
})}).edgeType,
resolve: (node) => ({
node,
cursor: idToCursor(node.id)
})
}
};

const addName = `add${name}`;
const updateName = `update${name}`;
const deleteName = `delete${name}`;

return {
[addName]: mutationWithClientMutationId({
name: addName,
inputFields: args,
outputFields: allFields,
inputFields,
outputFields,
mutateAndGetPayload: getAddOneMutateHandler(graffitiModel)
}),
[updateName]: mutationWithClientMutationId({
name: updateName,
inputFields: {
...args,
...inputFields,
id: {
type: new GraphQLNonNull(GraphQLID),
description: `The ID of a ${name}`
}
},
outputFields: allFields,
outputFields,
mutateAndGetPayload: getUpdateOneMutateHandler(graffitiModel)
}),
[deleteName]: mutationWithClientMutationId({
Expand All @@ -161,6 +178,23 @@ function getMutationField(graffitiModel, type) {
function getFields(graffitiModels, {mutation} = {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)
};
}, {})
}),
resolve: () => ({})
};

const {queries, mutations} = reduce(types, ({queries, mutations}, type, key) => {
type.name = type.name || key;
const graffitiModel = graffitiModels[type.name];
Expand All @@ -171,7 +205,7 @@ function getFields(graffitiModels, {mutation} = {mutation: true}) {
},
mutations: {
...mutations,
...getMutationField(graffitiModel, type)
...getMutationField(graffitiModel, type, viewer)
}
};
}, {
Expand All @@ -182,22 +216,7 @@ function getFields(graffitiModels, {mutation} = {mutation: true}) {
const RootQuery = new GraphQLObjectType({
name: 'RootQuery',
fields: {
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)
};
}, {})
}),
resolve: () => ({})
},
viewer,
node: {
name: 'node',
description: 'Fetches an object given its ID',
Expand Down

0 comments on commit e0031ef

Please sign in to comment.