0

when I am trying to update a record in Relay using Relay Mutations I getting an error like this.

doesn't have a field "clientMutationId"

But I did't declare any clientMutationId in my GraphQL schema. Here is my code in Relay

function updateZipdata1(){
  console.log("update zip");
   return new Promise((resolve, reject) => {
    var Connection = require('tedious').Connection;
    var Request = require('tedious').Request;
    var config = {
        userName: 'xxx',
        password: 'xxxx',
        server: 'xxxx', 
        options: {
            database: 'xxxx'

        }
     }
    var results = [];
    var connection = new Connection(config);
    connection.on('connect', (err)=> {
         if (err) {
            console.log(err)
            reject(err);
        }
        var ZipCode="123456";
        var RecNo="789456";

        var sql = "update dbo.xrxZip set ZipCode='"+ZipCode+"' where RecNo='"+RecNo+"'";
        var request = new Request(sql,
            (err, rowCount)=> {
                if (err) {
                    console.log('SQLError');
                    connection.close();
                    reject(err);
                }
                else {
                  console.log("sql:"+rowCount)
                    connection.close();
                    resolve(results);   
              }
            });
            request.on('row', (columns)=> {
            var row = {};
            columns.forEach((column)=> {
                if (column.isNull) {
                    row[column.metadata.colName] = null;
                } else {
                    row[column.metadata.colName] = column.value;
                }
            });
            console.log("hai:"+results)
            results.push(row);

        });
          connection.execSql(request);
      })
   })
};  

my GraphQL schema:

var zipType = new GraphQLObjectType({
  name: 'Zipcode',
  fields: () => ({

    ZipCode: {type: GraphQLString},
    City: {type: GraphQLString},
    ShortCode:{type: GraphQLString},
    State:{type: GraphQLString},
    Phone:{type: GraphQLString},
    TaxCode:{type: GraphQLString},
    Tax:{type: GraphQLString},
    RecNo:{type: GraphQLString}

  }),
});

var zipCodeType = new GraphQLObjectType({
  name: 'Zip',
  fields: () => ({
    zipcodes: {type: new GraphQLList(zipType)},
  }),
});

export var Schema = new GraphQLSchema({
  mutation: new GraphQLObjectType({
    name: "Mutation",
     fields: () => ({
      UpdateZipcode:{
        type: zipCodeType,
        args: {
          ZipCode: {type: GraphQLString}
        },
        resolve: (root,{ZipCode}) =>updateZipdata,

      }
    })
  })
})
 const updateZipdata = {
 zipcodes: updateZipdata1(), // Calling the function
};

When I run it in terminal for hard coded ZipCode and RecNo it is working without any error. But when I try to execute in Relay I am getting an error like as mentioned above.

Hear is my Relay code:

This is subclass in Relay for mutations.

export default class updateZipMutation extends Relay.Mutation {
  getMutation() {
    alert("getMutation");
    return Relay.QL`mutation{UpdateZipcode}` ;
  }
  getVariables() {
    alert("getVariables");
    return { RecNo: this.props.data.RecNo,ZipCode:this.props.data.ZipCode,City:this.props.data.City,Phone:this.props.data.Phone,ShortCode:this.props.data.ShortCode,Tax:this.props.data.Tax,TaxCode:this.props.data.TaxCode,State:this.props.data.State};
  }
  getFatQuery() {
    alert("getFatQuery")
    return Relay.QL`
      fragment on Zipcode {
          RecNo,
          ZipCode,
          City,
          Phone,
          ShortCode,
          Tax,
          TaxCode,
          State
      }
    `;
  }
  getConfigs() {
    alert("getConfigs");
    return [{
      type: 'zipCodeType',
      fieldIDs:{
        RecNo: this.props.data.RecNo
      },
    }];
  }
}

Here is my main class in Relay calling the subclass for mutations:

export default class zips extends React.Component {
 Relay.Store.update(
      new updateZipMutation({        //I am getting the data from form
        data: { RecNo, ZipCode,City,Phone,ShortCode,Tax,TaxCode,State},
      })
    );
  }
}

any one please give me suggestions how to resolve it, and how to work on mutations. Any help much appreciated.

Mahesh
  • 395
  • 1
  • 2
  • 18

2 Answers2

3

Relay mutations must have clientMutationId field that is accepted in their input and is returned as is in the resulting payload. Relay uses them to track which mutation server have response to. In addition for Relay, mutations must accept one argument - input. It can be a InputObject type, but it must be formed like that. Here is detailed guide on Relay GraphQL Mutation inteface https://facebook.github.io/relay/docs/graphql-mutations.html#content

In your case you could do something like this:

export var Schema = new GraphQLSchema({
  mutation: new GraphQLObjectType({
    name: "Mutation",
     fields: () => ({
      UpdateZipcode:{
        type: zipCodeType,
        args: {
          input: {type: new GraphQLInputObjectType({
            name: "UpdateZipCodeInput",
            fields: {
              clientMutationId: { type: String },
              ZipCode: { type: String },
          })}
        },
        resolve: (root,{input: { clientMutationId, ZipCode}) => ({
          ...updateZipCode(ZipCode),
          clientMutationId,
        }),
      }
    })
  })
})
freiksenet
  • 3,569
  • 3
  • 28
  • 28
1

Just use Relay helper function from graphql-relay package

mutationWithClientMutationId

It will provide a Relay compatible mutation

https://github.com/graphql/graphql-relay-js#mutations

LordDave
  • 1,118
  • 1
  • 11
  • 22