Form management
Before we can identify the user when we receive a request, we need to create a form to collect credentials.

We will use Formik to build our forms.
$ yarn add @types/react formik
We need 2 components to build our form, Input
and Button
which we can then use to build our form. We send the request to the server using useMutation
.
import * as React from "react";
import { FieldProps } from "formik";
import styled from "styled-components";
import { theme } from "../../config";
const Container = styled.div``;
const StyledInput = styled.input`
border: 1px solid ${theme.colors.border};
border-radius: 4px;
padding: 8px;
font-size: 14px;
width: 100%;
box-sizing: border-box;
&:focus {
border-color: ${theme.colors.background2};
outline: none;
}
`;
const Error = styled.div`
color: red;
`;
export const Input: React.SFC<FieldProps<any>> = ({
field, // { name, value, onChange, onBlur }
form: { touched, errors }, // also values, setXXXX, handleXXXX, dirty, isValid, status, etc.
...props
}) => (
<Container>
<StyledInput type="text" {...field} {...props} />
{touched[field.name] && errors[field.name] && (
<Error>{errors[field.name]}</Error>
)}
</Container>
);
We also use React Router and Lazy:
import React, { Suspense, lazy } from "react";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import reset from "styled-reset";
import ApolloClient from "apollo-boost";
import { ApolloProvider } from "react-apollo";
import { LoadingScreen } from "./components/Loading";
import { Header } from "./components/Header";
import { createGlobalStyle } from "styled-components";
const SignupPage = lazy(() => import("./pages/signup"));
const LinksPage = lazy(() => import("./pages/links"));
const client = new ApolloClient({ uri: "/graphql" });
const GlobalStyle = createGlobalStyle`
${reset}
body {
font-family: "Open Sans", sans-serif;
}
a {
text-decoration: none;
color: inherit;
}
`;
const App: React.FC = () => {
return (
<ApolloProvider client={client}>
<GlobalStyle />
<div className="App">
<Router>
<Header />
<Switch>
<Route path="/signup">
<Suspense fallback={<LoadingScreen />}>
<SignupPage />
</Suspense>
</Route>
<Route path="/users">
<Suspense fallback={<LoadingScreen />}>
<LinksPage />
</Suspense>
</Route>
<Route path="/">
<div>Home</div>
</Route>
</Switch>
</Router>
</div>
</ApolloProvider>
);
};
export default App;
This cover everything needed to interact with the GraphQL API and display the results.
Last updated
Was this helpful?