Glavin001/graphql-sequelize-crud

Custom mutations and queries

Open

#25 opened on May 4, 2017

View on GitHub
 (1 comment) (4 reactions) (1 assignee)TypeScript (23 forks)github user discovery
good first issuehelp wanted

Repository metrics

Stars
 (128 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

I had to check the source code to figure out how create custom mutations, it would be very helpful include better explanation on the docs.

It would be good mentioning that custom queries and mutations should be included in sequelize models definitions, then provide an example like this.

const user = sequelizeClient.define('user', {
  email: {
    type: Sequelize.STRING,
    allowNull: true,
    unique: false,
  },
  password: {
    type: Sequelize.STRING,
    allowNull: true,
  }
}, {
  classMethods: {
    //queries: function(){},
    mutations: function(Models, ModelTypes, resolver) {
      console.log("Creating custom mutations for model user");
      return {
        myMutationA: {
          type: MyCustomType,
          args: {
            dataA: {
              data: 'dataA',
              type: new GraphQLNonNull(GraphQLString)
            },
            dataB: {
              data: 'dataB',
              type: new GraphQLNonNull(GraphQLString)
            }
          },
          resolve: (obj, {
            name
          }) => {

            console.log("Calling custom mutator with data")
            console.log(name)
            // Here call sequelize using Models in the scope
            let myFirstPromise = new Promise((resolve, reject) => {
              setTimeout(function() {
                resolve("Success!");
              }, 250);
            });

            return myFirstPromise;
          }
        }
      };
    }
  }
});

Contributor guide