{"size": 1978, "ext": "js", "max_stars_repo_path": "packages/docs/code-examples/paper/with-graphql-mutation.source.js", "max_stars_repo_name": "graphql-mocks/graphql-mocks", "max_stars_repo_stars_event_min_datetime": "2020-12-29T13:37:04.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-13T12:09:21.000Z", "max_issues_repo_path": "packages/docs/code-examples/paper/with-graphql-mutation.source.js", "max_issues_repo_name": "graphql-mocks/graphql-mocks", "max_issues_repo_issues_event_min_datetime": "2020-12-30T19:58:19.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-08T17:44:05.000Z", "max_forks_repo_path": "packages/docs/code-examples/paper/with-graphql-mutation.source.js", "max_forks_repo_name": "graphql-mocks/graphql-mocks", "max_forks_repo_forks_event_min_datetime": "2021-04-16T07:56:08.000Z", "max_forks_repo_forks_event_max_datetime": "2021-04-16T07:56:08.000Z"}
{"max_stars_count": 15, "max_issues_count": 28, "max_forks_count": 1, "avg_line_length": 25.6883116883, "max_line_length": 124, "alphanum_fraction": 0.608190091}
codegen(`
const {output} = require('../helpers');
module.exports = output("import schemaString from './with-graphql-schema.source';", "import schemaString from './schema';");
`);
import { Paper } from 'graphql-paper';
import { graphql, buildSchema } from 'graphql';

async function run() {
  const graphqlSchema = buildSchema(schemaString);
  const paper = new Paper(graphqlSchema);

  const mutationType = graphqlSchema.getMutationType();
  const mutationTypeFields = mutationType.getFields();

  mutationTypeFields.addFilm.resolve = function filmsResolver(root, args, context, info) {
    return paper.mutate(({ create, getStore }) => {
      const store = getStore();
      const maxIdReducer = (previous, { id }) => Math.max(Number(previous), Number(id)).toString();
      let lastFilmId = store.Film.reduce(maxIdReducer, '0');
      let lastActorId = store.Actor.reduce(maxIdReducer, '0');

      const newFilm = create('Film', {
        id: ++lastFilmId,
        title: args.input.title,
        year: args.input.year,
      });

      newFilm.actors = args.input.actors.map((actor) => {
        return create('Actor', {
          id: ++lastActorId,
          ...actor,
        });
      });

      return newFilm;
    });
  };

  const mutation = `
    mutation($addFilmInput: AddFilmInput!) {
      addFilm(input: $addFilmInput) {
        id
        title
        year
        actors {
          id
          name
        }
      }
    }
  `;

  const mutationInput = {
    title: 'My Girl',
    year: '1991',
    actors: [{ name: 'Anna Chlumsky' }],
  };

  const result = await graphql({
    source: mutation,
    schema: graphqlSchema,
    variableValues: { addFilmInput: mutationInput },
  });

  console.log(result);
  codegen(`
const {output} = require('../helpers');
module.exports = output("return result", "");
`);
}

// kick everything off!
codegen(`
const {output} = require('../helpers');
module.exports = output("module.exports.query = run", "run();");
`);
