简体   繁体   English

使用 Mutation hook 更新 Graphql 服务器中的数据

[英]Using Mutation hook to update data in Graphql server

Actually I am new to React Native.其实我是 React Native 的新手。 In my recent project, I have faced an issue while using useMutation hook for updating the data in my server.在我最近的项目中,我在使用 useMutation 挂钩更新服务器中的数据时遇到了一个问题。 I have attached the file in which I am using it.我附上了我正在使用它的文件。

For your reference, I have attached the screenshot of the playground.为了您的参考,我附上了游乐场的屏幕截图。

Note:- I am not getting an error and the values are not updated in the server.注意:- 我没有收到错误,并且服务器中的值未更新。 If I click on the button.如果我点击按钮。 It is not required to refetch it and display the info using a query, just an update would be enough.不需要重新获取它并使用查询显示信息,只需更新就足够了。

Mutation structure :突变结构

突变结构 . .

graphql request sent :发送的 graphql 请求

已发送 graphql 请求

 import React, { Component,useState } from 'react'; import { View, Text, StyleSheet, Dimensions,ScrollView,AsyncStorage,TouchableHighlight,Alert,KeyboardAvoidingView} from 'react-native'; import TopHeader from '../../Header/TopHeader'; import { gql } from 'apollo-boost'; import Input from '../../SignIn/Input'; import { ScreenLoader } from '../../Loader'; import { useMutation } from '@apollo/react-hooks'; export const UPDATE_USER_DETAILS = gql` mutation UpdateUser($input: AccountInput!){ accountUpdate(input: $input){ accountErrors{ message } user{ id email firstName lastName } } } `; const getToken = async () => { const token = await AsyncStorage.getItem('token'); console.log("Token = "+token); return token; } function UpdateUserComponent({ firstname,lastname }) { const [newfirstName, setNewfirstName] = useState(firstname); const [newlastName, setNewlastName] = useState(lastname); var token = getToken(); console.log("Inside Update User Component"); const [updateUser,{loading,data,error}] = useMutation(UPDATE_USER_DETAILS,{ headers: { Authorization: token ? `Bearer ${token}` : ''} }) if (loading) return <ScreenLoader/> if (error){ console.log(error); return <Text>Error...</Text> } console.log(data); return ( <KeyboardAvoidingView style={styles.container} behavior='padding' enabled> <ScrollView> <View style={{ paddingTop: 36 }}> <Input type="text" value={newfirstName} onChange={e => setNewfirstName(e.target.value)} /> <View style={{ paddingTop: 10 }}> <Input type="text" value={newlastName} onChange={e => setNewlastName(e.target.value)}/> </View> <TouchableHighlight underlayColor='#fff' onPress={() => updateUser({ variables: { "input": {"firstName": newfirstName,"lastName": newlastName }} })} > <View style={{ paddingTop: 50 }}> <View style={styles.buttonLayout}> <Text style={styles.buttonText}>Save Changes</Text> </View> </View> </TouchableHighlight> </View> </ScrollView> </KeyboardAvoidingView> ) } const width = Dimensions.get('window').width; class AccountSettings extends Component { render() { return ( <View style={{ flex: 1 }}> <TopHeader text='Account Settings'/> <UpdateUserComponent/> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'flex-start', alignItems: 'center', paddingTop: 20, }, buttonLayout: { backgroundColor: '#C5C5C5', width: width * 85 / 100, height: 45, justifyContent: 'center', alignItems: 'center', borderRadius: 20, }, buttonText: { color: '#919191', fontSize: 14 }, }) export default AccountSettings;

Update :更新

There is some error in getting the values from Form Input Elements.从表单输入元素获取值时出现一些错误。 If I assign values directly it is updating.如果我直接分配值,它正在更新。 For Example:- if I use this statement on button press, the data is updated on the server.例如:- 如果我在按下按钮时使用此语句,则数据会在服务器上更新。

onPress={() => UpdateUser({ variables: { "input": {"firstName": "Prabhu","lastName": "Visu" }} }) }

Please help me in fixing this issue, making it work dynamically.请帮助我解决此问题,使其动态工作。 Thanks .!谢谢 。!

Replace This:替换这个:

  *onChange={e => setNewfirstName(e.target.value)}*

With This:有了这个:

  *onChangeText={text => setNewfirstName(text)}*

It will be working.!它会起作用的。!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM