Форма входа React Native и Apollo - PullRequest
0 голосов
/ 09 января 2019

Я изучаю Graphql при разработке реального проекта, это потрясающе, я уже построил сервер, сервер Apollo великолепен, теперь проблема в том, что реагирует клиент, у него есть теги, я не могу знать, как запустить мутацию на нажмите кнопку формы. Заранее спасибо.

МУТАЦИЯ

const LOGIN_CUSTOMER = gql`

mutation LoginCustomers($email: String!, $password: String!) {
    loginCustomers(input: { email: $email, secretop: $password }) {
      sign
      email
      id
    }
  }
`;

ФУНКЦИЯ МУТАЦИИ

const { email, password } = this.state;
    return (
      <Mutation
        mutation={LOGIN_CUSTOMER}
        variables={{ email, password }}
        onCompleted={data => {
          console.log("MUTATION_DATA", data);
          AsyncStorage.setItem("token", data.token);
          AsyncStorage.setItem("email", data.email);
          AsyncStorage.setItem("_id", data._id);
          this.props.navigation.navigate("App");
        }}
      >
        {(loginCustomers, { data }) => (
          <Container>
            <Content>
              <Image
                source={require("../../assets/images/log-header.jpg")}
                style={{ height: 300, width: null, flex: 1 }}
              />

              <Form style={{ paddingLeft: 15, paddingRight: 15 }}>
                <Item>
                  <Input
                    keyboardType="email-address"
                    autoCapitalize="none"
                    onChangeText={value => {
                      this.changeStateValues("email", value);
                    }}
                    placeholder="Email"
                  />
                </Item>
                <Item last>
                  <Input
                    secureTextEntry={true}
                    onChangeText={value => {
                      this.changeStateValues("password", value);
                    }}
                    placeholder="Contraseña"
                  />
                </Item>

                <Button
                  style={{ marginTop: 20 }}
                  full
                  warning
                  onPress={() => this.signIn()}
                >
                  <RobotoText style={styles.ingreso}>Ingresar</RobotoText>
                </Button>

                <Button
                  onPress={() => this.props.navigation.navigate("Forgot")}
                  transparent
                  full
                >
                  <RobotoText style={{ color: "#E3A80D" }}>
                    Perdí mi contraseña
                  </RobotoText>
                </Button>
              </Form>
            </Content>

            <Footer>
              <Button onPress={() => this.setType()} transparent>
                <RobotoText>Registrarse</RobotoText>
              </Button>
            </Footer>
          </Container>
        )}
      </Mutation>
    );

Ответы [ 2 ]

0 голосов
/ 26 февраля 2019

В конце концов, это то, что я сделал, передайте обратный вызов с именем мутации и вызовите его при нажатии. {(логин, {данные}) => (

                  <Form
                    style={{
                      paddingLeft: 15,
                      paddingRight: 15
                    }}
                  >
                    <Item>
                      <Input
                        keyboardType="email-address"
                        autoCapitalize="none"
                        onChangeText={value => {
                          this.changeStateValues("email", value);
                        }}
                        placeholder="Email"
                        value={this.state.email}
                      />
                    </Item>
                    <Item last>
                      <Input
                        secureTextEntry={true}
                        onChangeText={value => {
                          this.changeStateValues("password", value);
                        }}
                        placeholder="Contraseña"
                        value={this.state.password}
                      />
                    </Item>

                    <Button
                      style={{ marginTop: 20 }}
                      full
                      warning
                      onPress={() => {
                        login({
                          variables: {
                            email: this.state.email,
                            password: this.state.password
                          }
                        })
                          .then((res: any) => {
                            signIn(res.data.login.token);
                            this.props.navigation.navigate("App");
                          })
                          .catch(err => (
                            <RobotoText style={styles.ingreso}>
                              {err}
                            </RobotoText>
                          ));
                      }}
                    >
                      <RobotoText style={styles.ingreso}>Ingresar</RobotoText>
                    </Button>

                    <Button
                      onPress={() => this.props.navigation.navigate("Forgot")}
                      transparent
                      full
                    >
                      <RobotoText style={{ color: "#E3A80D" }}>
                        Perdí mi contraseña
                      </RobotoText>
                    </Button>
                  </Form>
                </Content>

                <Footer>
                  <Button
                    onPress={() => this.props.navigation.navigate("Signup")}
                    transparent
                  >
                    <RobotoText style={{}}>Registrarse</RobotoText>
                  </Button>
                </Footer>
              </Container>
            )}
          </Mutation>
0 голосов
/ 09 января 2019

Ваша форма может выглядеть примерно так ...

<Form method="POST" onSubmit={async event => {
  event.preventDefault()
  await loginCustomer()
}}>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...