2016-08-19 69 views
0

我已經查詢相關繼電器突變請看看 我試圖從設置集合更新量場,但沒有刷新頁面。價值得到了更新,但頁面刷新每次。 任何人都可以給我任何建議嗎?繼電器突變是不工作動態

下面是我schema.js

var settingType = new GraphQLObjectType({ 
    name: 'Setting', 
    fields: { 
    _id: { 
     type: new GraphQLNonNull(GraphQLID) 
    }, 
    id: globalIdField('Setting'), 
    amount: { 
     type: GraphQLString 
    }, 
    }, 
    interfaces: [nodeInterface] 
}); 

var Root = new GraphQLObjectType({ 
    name: 'Root', 
    fields:() => ({ 
    setting: { 
     type: settingType, 
     args: { 
     ...connectionArgs, 
      currency: {type: GraphQLString} 
     }, 
     resolve: (rootValue, args) => { 
     return getSetting(args.currency).then(function(data){ 
     return data[0]; 
     }).then(null,function(err){ 
     return err; 
     }); 
     } 
    }, 
    }) 
}); 

let EditAmountMutation = mutationWithClientMutationId({ 
    name: 'EditAmount', 
    inputFields: { 
    amount: { type: new GraphQLNonNull(GraphQLString) }, 
    }, 
    outputFields: { 
    viewer: { 
     type: settingType, 
     resolve:() => { 
     return getSettingedit().then(function(data){ 
      return data.setting[0] 
     }) 
     }, 
    }, 
    }, 
    mutateAndGetPayload: ({amount}) => { 
    return EditAmount({amount}).then(function(data){ 
     return data.setting 
    }) 
    }, 
}); 

export var schema = new GraphQLSchema({ 
    query: Root, 
    mutation: new GraphQLObjectType({ 
    name: 'Mutation', 
    fields:() => ({ 
     EditAmount: EditAmountMutation, 
    }) 
    }) 
}); 

,下面將我的EditAmountMutation.js文件

import Relay from "react-relay"; 

class EditAmountMutation extends Relay.Mutation { 
    static fragments = { 
    viewer:() => Relay.QL` 
     fragment on Setting { 
     amount, 
     } 
    `, 
    }; 
    getMutation() { 
    return Relay.QL` 
     mutation { EditAmount } 
    `; 
    } 

    getVariables() { 
    return { 
     amount: this.props.amount, 
    } 
    } 

    getFatQuery() { 
    return Relay.QL` 
     fragment on EditAmountPayload{ 
     viewer{ 
      id, 
      amount 
     } 
     } 
    `; 
    } 

    getConfigs() { 
    return [{ 
     type: 'FIELDS_CHANGE', 
     fieldIDs: { 
     viewer: this.props.viewer, 
     }, 
    }] 
    } 
    getOptimisticResponse() { 
    return { 
     viewer: { 
     amount: this.props.amount, 
     }, 
    }; 
    } 
} 

export default EditAmountMutation; 

以下是我的看法頁Setting.js

import React, { Component } from 'react'; 
import EditAmountMutation from "../mutations/EditAmountMutation"; 
import Relay from 'react-relay'; 

class Setting extends Component { 

    handleSubmitt = (e) => { 
    Relay.Store.commitUpdate(
     new EditAmountMutation({ 
     viewer: this.props.viewer, 
     amount: this.refs.amount.value, 
     }), 
    ); 
    this.refs.amount.value = ""; 
    } 
    render() { 
    let viewer =this.props.viewer; 
    return (
     <div className="col-md-4 col-md-offset-4"> 
     <form onSubmit={this.handleSubmitt} className="settingForm photo-gallry"> 
      <div className="form-group"> 
       <label>Current Amount:&nbsp;&nbsp;</label> 
       <span>{viewer.amount}</span> 
       </div> 
       <div className="form-group"> 
       <label>Change Amount</label> 
       <input className="form-control" type="text" required placeholder="Amount" ref="amount" /> 
       </div> 
       <button className="btn btn-primary allbtn-btn" type="submit">Submit</button> 
      </form> 
     </div> 
    ); 
    } 
} 

export default Relay.createContainer(Setting, { 
    fragments: { 
    viewer:() => Relay.QL` 
     fragment on Setting { 
     id, 
     amount, 
     ${EditAmountMutation.getFragment('viewer')} 
     } 
    `, 
    }, 
}); 

及以下是route.js文件

const SettingQueries = { 
viewer:() => Relay.QL`query{ 
    setting(currency: "USD") 
}`, 
} 

export default [{ 
    path: '/', 
    component: App, 
    queries: UserQueries,PostQueries,SettingQueries, 
    indexRoute: { 
    component: IndexBody, 
    }, 
    childRoutes: [{ 
    path: 'settings', 
    component: Setting, 
    queries: SettingQueries, 
    }] 
}] 
以下

是database.js

export function getSetting(params) { 
    // console.log("getSetting",params) 
    return Setting.find({currency: "USD"}) 
    .exec() 
    .then(function(setting) { 
    return setting; 
    }); 
} 

export function EditAmount(params) { 
    // console.log(params) 
    return Setting.findOneAndUpdate({currency: "USD"}, {$set:{amount:params.amount}}, {new: true}, function(err, setting){ 
    if (err) return err; 
     return setting; 
    }); 
} 

同樣的事情正在處理/ graphql作爲

mutation EditAmountMutation($input:EditAmountInput!) { 
    EditAmount(input:$input) { 
    clientMutationId, 
    viewer{ 
     amount 
    } 
    } 
} 

{ 
    "input": { 
    "clientMutationId": "32", 
    "amount": "222" 
    } 
} 

,並得到輸出如下

{ 
    "data": { 
    "EditAmount": { 
     "clientMutationId": "32", 
     "viewer": { 
     "amount": "222" 
     } 
    } 
    } 
} 

但網絡選項卡我的JavaScript控制檯將其顯示爲發佈請求 console log

+0

也得到奇怪的行爲,金額在Chrome上更新,但不在Firefox上更新。 – akshay

+0

while perfoming update我認爲輸出字段在接力突變中不起作用 – akshay

+0

更新'Settings'容器的數據'amount'並更新'Settings'容器。我在這裏沒有看到任何問題。 –

回答

2

EditAmountMutation.js文件,修復getConfigs功能。不是提供查看器的ID,而是提供了整個對象。改成這樣:

fieldIDs: { 
    viewer: this.props.viewer.id, 
}, 

此外,包括idfragments財產,因爲突變對瀏覽器的依賴:

static fragments = { 
    viewer:() => Relay.QL` 
     fragment on Setting { 
     id, 
     amount, 
    } 
    `, 
}; 

the mutation example的文件中。

+0

仍然在同一個地方。 正如你所見 ' {「query」:「mutation EditAmountMutation($ input_0:EditAmountInput!){\ n EditAmount(input:$ input_0){\ n clientMutationId \ n} \ n}」,「variables」:{ 「input_0」:{「amount」:「23」,「clientMutationId」:「0」}}}' 查詢中沒有查看器 – akshay

+0

您是否已將'id'添加到'fragments'?我更新了我的答案。 –

+0

是的我更新了,但仍然在同一個地方。感謝您的努力。 – akshay