Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
graphql-generate-flow-schema-assets
Advanced tools
A small lib to help generating various Flow assets from a GraphQL schema.
It currently finds all enum
and object
types in your schema and outputs them both as JS constants and as Flow types.
The point of this lib is to:
Ideally, this lib is used in combination with saving your schema from your backend.
Example in package.json
:
"scripts": {
...
"graphql:save-schema": "some-script-to-save-your-schema && npm run graphql:generate-assets",
"graphql:generate-assets": "graphql-generate-flow-schema-assets -s path/to/schema.json --enums --object-types --enum-file-path ./src/constants/enums.js --object-types-file-path ./src/constants/object-types.js"
...
This way, your code always relies auto generated code 100% in sync with your backend. If something changes in a incompatible way in the schema, Flow will tell you since the generated types will change.
npm install -g graphql-generate-flow-schema-assets
All options can be seen by running:
graphql-generate-flow-schema-assets -h
graphql-generate-flow-schema-assets -s path/to/schema.json --enums
This will generate a file containing all enums from your GraphQL schema both as types and as actual objects. This means that instead of doing:
if (user.status === 'Active') {
...
You can do:
import { UserStatuses } from '../path/to/enums.js';
if (user.status === UserStatuses.Active) {
...
...meaning it'll be type checked, auto completed by your editor/IDE, and all of that good stuff.
graphql-generate-flow-schema-assets -s path/to/schema.json --object-types
This will generate a file containing all object types from your GraphQL schema both as types and as properties on one root object. This means that instead of doing:
if (userOrSomeOtherType.__typename === 'User') {
...
You can do:
import { ObjectTypes } from '../path/to/object-types.js';
if (userOrSomeOtherType.__typename === ObjectTypes.User) {
...
...meaning it'll be type checked, auto completed by your editor/IDE, and all of that good stuff.
The lib flow-enum-validator
is well suited to work with the enum output of this library to help validate unknown strings to enums. An example:
// @flow
import { UserStatuses } from '../path/to/enums.js';
import { createEnumValidator } from 'flow-enum-validator';
// This will return a function that takes a string and returns if it's a valid part of the UserStatuses enum object, or void if it's not
const validateUserStatus = createEnumValidator(UserStatuses);
const userStatus = validateUserStatus(someRandomStringHere);
/**
* Say UserStatuses is an object that looks like this: { Active: 'Active', Inactive: 'Inactive' }
* Flow will now have refined userStatus to 'Active' | 'Inactive' | null | void.
*/
if (userStatus) {
// userStatus is now 'Active' | 'Inactive'
}
Check out flow-enum-validator
here for more info and examples.
FAQs
Generate Flow assets from GraphQL schema.
The npm package graphql-generate-flow-schema-assets receives a total of 8 weekly downloads. As such, graphql-generate-flow-schema-assets popularity was classified as not popular.
We found that graphql-generate-flow-schema-assets demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.