env.gql
Use GraphQL Type Definitions To Validate process.env
$ npm i env.gql
This library does not modify process.env directly.
Motivation
Fed up of not knowing what config to pass to a server? Use GraphQL to explain and validate your environment variables. Never see cannot connect to 'undefined'
again 🎉
Inspiration
Example
process.env.PORT will be casted to a number, process.env.DEV will be casted to a boolean.
const envGQL = ; const typeDefs = ` input Config { PORT: Number! URL: String! SECRET: String! DEV: Boolean! }`; const config = ;
Using .gql file
The idiomatic file convention is
.env.gql
const path = ;const envGQL = ; const typeDefsPath = path;const config = ;
Using directives
const envGQL = ;const constraintDirective constraintDirectiveTypeDefs = const typeDefs = ` input Config { PORT: Number URL: String @constraint(format: "uri") SECRET: String } `; const config = ;
Override
Use to validate not just process.env
const envGQL = ; const typeDefs = ` input Config { PORT: Number URL: String SECRET: String }`; const config = ;
graphql-tag
const envGQL = ;const gql = ; const typeDefs = gql` input Config { PORT: Int URL: String SECRET: String }`; const config = ;
TypeScript
; ; ;
Defaults
const typeDefs = ` input Config { PORT: Int = 123 URL: String = "http://github.com" SECRET: String = "supersecret" DEV: Boolean = false }`; const config = ; typeof configPORT === "number"typeof configURL === "string"typeof configSECRET === "string"typeof configDEV === "boolean"
Required
Use GraphQL
!
symbol
const typeDefs = ` input Config { PORT: Int! # Required URL: String SECRET: String DEV: Boolean }`; const config = ;