{"size": 1292, "ext": "js", "max_stars_repo_path": "services/graphql/src/mutations/createVocation.js", "max_stars_repo_name": "dherault/aquest", "max_stars_repo_stars_event_min_datetime": "2016-02-29T19:47:55.000Z", "max_stars_repo_stars_event_max_datetime": "2016-03-01T16:38:58.000Z", "max_issues_repo_path": "services/graphql/src/mutations/createVocation.js", "max_issues_repo_name": "dherault/aquest", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "services/graphql/src/mutations/createVocation.js", "max_forks_repo_name": "dherault/aquest", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 3, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.512195122, "max_line_length": 95, "alphanum_fraction": 0.636996904}
const { GraphQLNonNull, GraphQLString } = require('graphql');
const { mutationWithClientMutationId, cursorForObjectInConnection } = require('graphql-relay');

const _ = require('../graph');
const { query, createResource } = require('../db');
const IndividualsType = require('../customTypes/IndividualsType');
const ensureAuth = require('../utils/ensureAuth');
const createResourceObject = require('../utils/createResourceObject');

module.exports = mutationWithClientMutationId({
  name: 'CreateVocation',
  inputFields: {
    label: {
      type: new GraphQLNonNull(GraphQLString),
    },
  },
  outputFields: {
    vocationEdge: {
      type: _.getEdgeType('http://foo.com#Vocation'),
      resolve: ({ vocation: { id } }) => query(db => db
        .collection('Vocation')
        .find()
        .toArray()
      ).then(vocations => {
        const vocation = vocations.find(s => s.id === id);

        return {
          cursor: cursorForObjectInConnection(vocations, vocation),
          node: vocation,
        };
      }),
    },
    individuals: IndividualsType.field,
  },
  mutateAndGetPayload: ensureAuth(({ label }, context) => {
    const vocation = createResourceObject('Vocation', context, { label });

    return createResource(vocation).then(() => ({ vocation }));
  }),
});
