简体   繁体   English

如何在 React native 中实现 UseMutation?

[英]How to implement UseMutation inside React native?

I'm trying to implement my CreatUser Service in my App but it's not working.我正在尝试在我的应用程序中实施我的 CreatUser 服务,但它不起作用。 I'm using NestJS for the backend with GraphQl for the API's and React Native in the FrontEnd with appolo Server.我将 NestJS 用于后端,将 GraphQl 用于 API,并在前端使用 appolo 服务器使用 React Native。 I always get this 2 errors:我总是得到这 2 个错误:

-POST http://localhost:3500/graphql 400 (Bad Request). -POST http://localhost:3500/graphql 400(错误请求)。 -Uncaught (in promise) Error: Response not successful: Received status code 400. -未捕获(承诺)错误:响应不成功:收到状态代码 400。

It's working in my playground:它在我的操场上工作: 在此处输入图像描述 在此处输入图像描述

and this my code for the creation of the mutation: This where I creat my mutation:这是我创建突变的代码:这是我创建突变的地方:

import {gql} from "@apollo/client";  
export const  Creat_User = gql`
    mutation createUser($createUserInput:createUserInput!)
      {
        createUser (createUserInput: $createUserInput)
        {  
      nom
      password
      prenom
      mail
      Numero_Telephone
      role
      societe
       }
      }
    `;

and this My CreatUserScreen where i want to use my mutation:以及我想在其中使用突变的 My CreatUserScreen:

import { useMutation } from '@apollo/client';
import React, { useState } from 'react';
import {
  Image,
  StyleSheet,
  TextInput,
  View,
  Text,
  KeyboardAvoidingView,
  Keyboard,
  TouchableOpacity,
  ScrollView} 
  from 'react-native';
import { Creat_User } from '../../../graphQl/CreateUserQL';

const CreateUserScreen = () => {

  const [nom,SetNom] = useState("Empty nom");
  const [Prenom,SetPrenom] = useState("Empty Prenom");
  const [Password,SetPassword] = useState("Empty Password");
  const [Mail,SetMail] = useState("Empty Mail");
  const [Societe,SetSociete] = useState("Empty Societe");
  const [Numero_Telephone,SetNumero_Telephone] = useState("Empty Numero_Telephone ");
  const [Role, SetRole] = useState("Empty Role");

  
const [data] = useMutation(Creat_User);

  const CreationUS = () => 
  {
    console.log("THIS MY nom ===>",nom)
    console.log("THIS MY Prenom ===>",Prenom)
    console.log("THIS MY Password ===>",Password)
    console.log("THIS MY Mail ===>",Mail)
    console.log("THIS MY Societe ===>",Societe)
    console.log("THIS MY Role ===>",Role)
    console.log("THIS MY Numero_Telephone ===>",Numero_Telephone)

    data({
      variables: {
        createUserInput: {
          nom:nom,
          password:Password,
          prenom:Prenom,
          mail: Mail,
          Numero_Telephone: Numero_Telephone,
          role:Role,
          societe: Societe
        },
      },
    })
    
  }



  return (
    <View style={{flex:1, backgroundColor:'white'}}>
      <ScrollView
        keyboardShouldPersistTaps="handled"
        contentContainerStyle=
        {{justifyContent: 'center',alignContent:'center'}}>
         
        <View style={{alignItems:'center'}}>
        <Image
          source={require('../../../images/logoMS.png')}
          style=
          {{
          width: "50%",
          height: 100,
          resizeMode: "contain",
          }}
        />
        </View>

        <KeyboardAvoidingView enabled>

          <View style={styles.SectionStyle}>
            <TextInput
              style={styles.inputStyle}
              underlineColorAndroid="black"
              placeholder="Enter Nom"
              placeholderTextColor="black"
              autoCapitalize="sentences"
              returnKeyType="next"
              onChangeText={(val)=>SetNom(val)}
              />
          </View>
          <View style={styles.SectionStyle}>
            <TextInput
              style={styles.inputStyle}
              underlineColorAndroid="black"
              placeholder="Enter Prenom"
              placeholderTextColor="black"
              returnKeyType="next"
              onChangeText={(val2)=>SetPrenom(val2)}
            />
          </View>

          <View style={styles.SectionStyle}>
            <TextInput
              style={styles.inputStyle}
              placeholder="Enter Password"
              placeholderTextColor="black"
              secureTextEntry={true}
              onChangeText={(val3)=>SetPassword(val3)}

            />
          </View>

          <View style={styles.SectionStyle}>
            <TextInput
              style={styles.inputStyle}
              placeholder="Enter Email"
              placeholderTextColor="black"
              keyboardType="email-address"
              returnKeyType="next"
              onChangeText={(val4)=>SetMail(val4)}

            />
          </View>

          <View style={styles.SectionStyle}>
            <TextInput
              style={styles.inputStyle}
              underlineColorAndroid="#f000"
              placeholder="Enter Numero Telephone"
              placeholderTextColor="#8b9cb5"  
              autoCapitalize="sentences"
              returnKeyType="next"
              keyboardType="numeric"
              blurOnSubmit={true}
              onChangeText={(val5)=>Number(SetNumero_Telephone(val5))}

            />
          </View>

          <View style={styles.SectionStyle}>
            <TextInput
              style={styles.inputStyle}
              underlineColorAndroid="#f000"
              placeholder="Choisir Role"
              placeholderTextColor="black"
              autoCapitalize="sentences"
              returnKeyType="next"
              onSubmitEditing={Keyboard.dismiss} 
              onChangeText={(val6)=>SetRole(val6)}

            />
          </View>

          <View style={styles.SectionStyle}>
            <TextInput
              style={styles.inputStyle}
              underlineColorAndroid="#f000"
              placeholder="Enter Nom Societe"
              placeholderTextColor="black"
              autoCapitalize="sentences"
              returnKeyType="next"
              onSubmitEditing={Keyboard.dismiss} 
              onChangeText={(val7)=>SetSociete(val7)}
 
            />
          </View>

          <TouchableOpacity
            style={styles.buttonStyle}
            activeOpacity={0.5}
            onPress={CreationUS}
          >
            <Text style={styles.buttonTextStyle}>REGISTER</Text>
          </TouchableOpacity>

        </KeyboardAvoidingView>
      </ScrollView>
    </View>
  
  );
};
export default CreateUserScreen;

I used useState Hook to get all the input from the user and it's working:我使用 useState Hook 从用户那里获取所有输入并且它正在工作:

const [nom,SetNom] = useState("Empty nom");
  const [Prenom,SetPrenom] = useState("Empty Prenom");
  const [Password,SetPassword] = useState("Empty Password");
  const [Mail,SetMail] = useState("Empty Mail");
  const [Societe,SetSociete] = useState("Empty Societe");
  const [Numero_Telephone,SetNumero_Telephone] = useState("Empty Numero_Telephone ");
  const [Role, SetRole] = useState("Empty Role");

And this is the UseMutation function I want implement:这是我想要实现的 UseMutation function:

const [data] = useMutation(Creat_User);
  const CreationUS = () => 
  {
    console.log("THIS MY nom ===>",nom)
    console.log("THIS MY Prenom ===>",Prenom)
    console.log("THIS MY Password ===>",Password)
    console.log("THIS MY Mail ===>",Mail)
    console.log("THIS MY Societe ===>",Societe)
    console.log("THIS MY Role ===>",Role)
    console.log("THIS MY Numero_Telephone ===>",Numero_Telephone)

    data({
      variables: {
        createUserInput: {
          nom:nom,
          password:Password,
          prenom:Prenom,
          mail: Mail,
          Numero_Telephone: Numero_Telephone,
          role:Role,
          societe: Societe
        },
      },
    })
  } 

In the front i get this:在前面我得到这个: 在此处输入图像描述

It looks like you're imitating the syntax of useQuery .看起来你在模仿useQuery的语法。

You need to pass the variables into useMutation :您需要将变量传递给useMutation

const variables = {
  nom:nom,
  password:Password,
  prenom:Prenom,
  mail: Mail,
  Numero_Telephone: Numero_Telephone,
  role:Role,
  societe: Societe
};

const [myMutation] = useMutation(Creat_User,{ variables });

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

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