
Security News
Risky Biz Podcast: Making Reachability Analysis Work in Real-World Codebases
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
@volst/graphql-form-helpers
Advanced tools
Makes it easy to work with nested data in forms with GraphQL.
A light-weight (1kb) package for dealing with complicated forms that have nested data and use GraphQL.
We use it in combination with Formik, Apollo Client and Prisma, but it is not specific to one of those.
Features:
When dealing with GraphQL you often have to write boilerplate code to load the fetched data in a form and some code so the form data becomes a correct mutation. It might not be so much boilerplate if you have a basic form, but it can quickly become a lot for more complicated forms with nested data. This package intents to reduce that boilerplate code.
Install with Yarn or npm:
yarn add @volst/graphql-form-helpers
npm i @volst/graphql-form-helpers
Imagine you have a form which, when the onSubmit
event is called, outputs this data:
const data = {
restaurant: 'kjrrqxy08001309',
categories: [
{
id: 'dgfrqxfaf000v',
name: 'Drinks'
},
{
name: 'Burgers'
}
]
};
First I'll explain what's going on here:
restaurant
field refers to an ID
, so it already exists and should be connected to the given restaurant.categories
field is an array of categories, the first one already exists (since it has an id
field), and the second one doesn't exist yet.Now we need to write a mutation which saves this data to your backend. Your GraphQL scheme probably looks different from this data
scheme. For example, if you use Prisma, your mutation data would need to look like this:
const graphqlData = {
restaurant: { connect: { id: 'kjrrqxy08001309' } },
categories: {
create: [
{
name: 'Burgers'
}
],
update: [
{
where: { id: 'dgfrqxfaf000v' },
data: { name: 'Drinks' }
}
]
}
};
As you can see this is a lot different to the data we have above. You could write some custom code everytime to make it look the same, but I'm already sweating even thinking about that. That's where parseFormToMutation
comes in:
import {
create,
connect,
save,
parseFormToMutation
} from '@volst/graphql-form-helpers';
const scheme = {
restaurant: connect,
categories: save
};
const graphqlData = parseFormToMutation(values, scheme);
But what if you have nested data? Imagine that a category can have items and subitems, the schema would look like this:
const scheme = {
restaurant: connect,
categories: {
__format: save,
items: {
__format: save,
subitems: save
}
}
};
The
__format
property applies the formatter (save
) on the parent property.
As you can see, it is now very easy to mutate nested data, even if it's an array of objects.
Currently there are three formatters out of the box:
connect
- wraps an object around a connect
mutation.create
- wraps an object around a create
mutation.save
- wraps an object around an update
mutation if there is an id
field, or create
if there is none.Psst, want to see a real-world example?
Writing a custom formatter is very easy!
function decimalToFloat(value: string | number) {
return parseFloat(value);
}
const scheme = {
items: {
price: decimalToFloat // In this example you could even pass `parseFloat` directly
}
};
When performing a GraphQL query, you can't just load the results of the query directly into a form. The results contain some extra info which is not relevant to the form, like __typename
(Apollo Client adds this field automatically for caching purposes). Stripping this field recursively is painful. Also, the top-level id
is not relevant since you already have that id
.
parseQueryToForm
removes this irrelevant data for you. An example with Formik:
import { Query } from 'react-apollo';
import { Formik } from 'formik';
import { parseQueryToForm } from '@volst/graphql-form-helpers';
const GET_RESTAURANT = gql`
query($id: ID!) {
restaurant(where: { id: $id }) {
id
name
}
}
`;
...
<Query query={GET_RESTAURANT} variables={{ id }}>
{({ data }) => (
<Formik
initialValues={parseQueryToForm(data.restaurant, {})}
/>
)}
</Query>
Checkout this real-world example
This project is still in early phases. Please don't hesistate to create an issue with feedback or send a PR! See contributing guide for more info.
FAQs
Makes it easy to work with nested data in forms with GraphQL.
The npm package @volst/graphql-form-helpers receives a total of 0 weekly downloads. As such, @volst/graphql-form-helpers popularity was classified as not popular.
We found that @volst/graphql-form-helpers demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.