Creating a Form
1. Create a Client query
1a. Create a fragment to represent your form field keys
; const fragment = gql` fragment client on ClientData { name age }`;
1b. Create a query for your form state
; const inputQuery = gql` { sampleForm @client { ...client } }`;
1b. Create a query to represent your error state
Error queries are namespaced like so: {FORM_NAME}Errors
; const errorsQuery = gql` { sampleFormErrors @client { ...client } }`;
2. Creating Initial Props
2a. Create a validator
; const validator = ;
2b. Supply Initial State
const initialData = name: null age: null
3. Create a Form Provider w/ formName, and initialData
3a. Create your Submit Mutation
const sampleMutation = gql` mutation($inputData: PersonInput) { createSample(inputData: $inputData) }`;
3b. Create your form
; const Form = FormProvider;
3c. Pass in initialData and a formName
{ return <Form initialData=initialData formName="sampleForm" > </Form> ;}
4. Create an Input w/ a field prop
; const Input = ; { return <Form formName="sampleForm" > <Input field="name" /> <Input type="number" field="age" /> </Form> ;}
5. Add a Submit Control
{ return <FormProvider formName="sampleForm" > <Input field="name" /> <Input type="number" field="age" /> <button type="submit">Submit</button> </FormProvider> ;}
Hydrating a Form
As long as a FormProvider
gets initialData
the form will hydrate the appropriate fields in the form.
There are some utils provided that may help you hydrate your Form:
1. Create a HydrateProvider
; const query = gql` { query sample { sampleForm { name age } } }`; const HydrateProvider = ;
2. Use a render prop to pass it into your form:
{ return <HydrateProvider> { return <Form initialData=data formName="sampleForm" > <Input field="name" /> <Input type="number" field="age" /> <SubmitControls /> </Form> ; } </HydrateProvider> ;}
3. Or use withHandlers
; { return <HydrateProvider> renderForm </HydrateProvider> ;} { return { return <Form initialData=data formName="sampleForm" > <Input field="name" /> <Input type="number" field="age" /> <SubmitControls /> </Form> ; } }Root;
How this works
Under the hood, apollo-forms
creates a ApolloClient
instance with apollo-linked-state
. The form gets its own
state graph to work with keyed off formName
. When onChange
is called from the Input
components, both internal react state is updated as well as the local ApolloClient
cache.
Validation through the revalidate
library is run when the inputs have values and validation messages are passed as props to the base component.
onSubmit
, the FormProvider
component takes the form state and passes it to the supplied mutation
in the form. By default the variables are formatted like this: { inputData: FORM_STATE }
. To customize your mutation arguments, pass a transform
to the FormProvider to return the form state however you wish.