Gathering detailed insights and metrics for graphql-let
Gathering detailed insights and metrics for graphql-let
Gathering detailed insights and metrics for graphql-let
Gathering detailed insights and metrics for graphql-let
next-plugin-graphql-let
Automatically configures webpack for graphql-let usage in Next.js.
graphql
A Query Language and Runtime which can target any service.
graphql-tag
A JavaScript template literal tag that parses GraphQL queries
graphql-ws
Coherent, zero-dependency, lazy, simple, GraphQL over WebSocket Protocol compliant server and client
npm install graphql-let
53.8
Supply Chain
57.3
Quality
69.2
Maintenance
25
Vulnerability
95.8
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
454 Stars
508 Commits
34 Forks
5 Watching
9 Branches
19 Contributors
Updated on 23 Oct 2024
Minified
Minified + Gzipped
TypeScript (97.24%)
JavaScript (2.76%)
Cumulative downloads
Total Downloads
Last day
-6.1%
4,492
Compared to previous day
Last week
6.3%
24,455
Compared to previous week
Last month
10.4%
99,049
Compared to previous month
Last year
50.4%
1,082,680
Compared to previous year
18
5
85
2
A webpack loader/babel-plugin/babel-plugin-macros/CLI/generated file manager of GraphQL code generator.
Try Create React App example and Next.js example integrating graphql-let. A blog post
One of the strengths of GraphQL is enforcing data types on runtime. Further, TypeScript and GraphQL code generator help it even safer by typing your codebase statically. Both make a truly type-protected development environment with rich IDE assists.
graphql-let enhances that development pattern by minimizing configuration setup, introducing intuitive syntax, and comfortable development experience through HMR (hot module replacement).
1import { useNewsQuery } from './news.graphql' // webpack 2// or 3import { gql, load } from "graphql-let/macro" // babel-plugin-macros 4const { useNewsQuery } = gql("query News { braa }") 5 6const News: React.FC = () => { 7 // Typed already️⚡️ 8 const { data: { news } } = useNewsQuery() 9 return <div>{news.map(...)}</div> 10}
Summary of characteristics of each entrypoint.
All of them mostly do the same behind the scene.
.graphql-let.yml
.graphql*
and .ts*
specified in your config.documents
.ts*
s. These are used for runtime..d.ts
s of the codegen results.
These are used for typing checking / IDE code completion.Note there are a few differences between the entrypoints.
Entry pointsYou need .graphql-let.yml and: | Getting codegen result from | Use values of codegen result | Use types of codegen result | Pros/Cons |
---|---|---|---|---|
webpack loader Configure "graphql-let/loader" to files "/.*\.(tsx?|graphql)$/" in webpack.config.(js|ts)
| File | ✅ Import both value and types from a GraphQL document as a module.import { useQuery, Query } from "./a.graphql" | HMR works as expected. Webpack config is required even though your project only uses Babel | |
String literal | ✅ byimport { gql } from "graphql-let" | ⚠️ You can, but you have to find the internal d.ts.import { gql } from "graphql-let" | ||
babel-plugin-macros If you've already setupbabel-plugin-macros,no config needed any more | File | ✅ byimport { load } from "graphql-let/macro" | ⚠️ You can, but you have to find the internally generated d.ts.import { load } from "graphql-let/macro" | Easiest to integrate if your project already has babel-plugin-macros. create-react-app is the great fit.Cannot load types from function call. Modifying *.graphql doesn't emit HMR. |
String literal | ✅ byimport { gql } from "graphql-let/macro" | ⚠️ You can, but you have to find the internally generated d.ts.import { gql } from "graphql-let/macro" | ||
babel-plugin Put "graphql-let/babel"to you .babelrc as a plugin | File | ✅ byimport { load } from "graphql-let" | ⚠️ You can, but you have to find the internally generated d.ts.import { load } from "graphql-let" | Mostly equivalent to babel-plugin-macros, but you always need your .babelrc configuration. Possibly, "import "./a.graphql"" could be implemented, but not supported yet.Cannot load types from function call. Modifying *.graphql doesn't emit HMR.Possibly I can make "--watch" option butlots to do for dependency management to detect file change. |
String literal | ✅ byimport { gql } from "graphql-let" | ⚠️ You can, but you have to find the internally generated d.ts.import { gql } from "graphql-let" |
There are things to make graphql-let light and stable.
"node"
and "web"
simultaneously. If sources are the same, the compilation should
be once.This is an example of TypeScript + React + Apollo Client on webpack. You may want TypeScript Vue Apollo or TypeScript Urql. Please replace the corresponding lines depending on your needs.
Note graphql-let is in devDependencies
.
1# Prerequisites 2yarn add -D typescript graphql 3 4# Install graphql-let with its peer dependencies 5yarn add -D graphql-let @graphql-codegen/cli @graphql-codegen/typescript @graphql-codegen/import-types-preset 6 7# Install GraphQL code generator plugins depending on your needs. These are in `plugins` of your .graphql-let.yml. 8yarn add -D @graphql-codegen/typescript-operations @graphql-codegen/typescript-react-apollo 9 10# Other libraries depending on your scenario 11yarn add @apollo/client
Run this command to generate a configuration template.
1yarn graphql-let init 2# This will generate .graphql-let.yml
Next, add graphql-codegen plugins in it. Please note that you have to generate a TypeScript source by the plugins.
Edit it like this:
1 schema: lib/type-defs.graphqls 2 documents: 3 - '**/*.graphql' 4 - '**/*.tsx' 5 plugins: 6+ - typescript-operations 7+ - typescript-react-apollo
cacheDir
cacheDir
will have .ts(x)
s that your sources will import. It's
node_modules/.cache/graphql-let
by default, but you may exclude node_modules
for webpack compilation. In that case, we recommend setting up like this.
1 schema: lib/type-defs.graphqls 2 documents: 3 - '**/*.graphql' 4 - '**/*.tsx' 5 plugins: 6 - typescript-operations 7 - typescript-react-apollo 8+ cacheDir: .cache
Please note that files in cacheDir
are only intermediate cache, possibly having wrong import paths. Your tsconfig.json
probably complains, so give it a line for exclusion.
1 // tsconfig.json 2 { 3+ "excludes": [".cache"] 4 }
Also, remember you have to .gitignore
the .cache
directory in the next section.
graphql-let will generate .d.ts
files in the same folder of .graphql
. Add
these lines in your .gitignore.
1+ *.graphql.d.ts 2+ *.graphqls.d.ts 3+ /.cache
The webpack loader also needs to be configured. Note that the content that
graphql-let/loader
generates is JSX-TypeScript. You have to compile it to
JavaScript with an additional loader such as babel-loader
.
1 const config: Configuration = { 2 module: { 3 rules: [ 4+ { 5+ test: /\.(tsx|graphql)$/, 6+ use: [ 7+ { loader: 'babel-loader', options: { presets: ['@babel/preset-typescript', '@babel/preset-react'] } }, 8+ { loader: 'graphql-let/loader' }, 9+ ] 10+ } 11 ] 12 } 13 }
Run this to generate .d.ts
.
1yarn graphql-let 2 3# This will generate files such as: 4# - src/query.graphql.d.ts 5# - src/schema.graphqls.d.ts
By --config
option, you can specify the custom path to the .graphql-let.yml
.
The directory .graphql-let.yml is located at is the basepath of the relative
paths in .grpahql-let.yml. Also, the basepath should be identical to webpack's
config.context
so the loader can find the config file.
1pwd # "/app" 2yarn graphql-let --config custom/path/.graphql-let.yml 3 4# This will point paths such as: 5# /app/custom/path/src/query.graphql.d.ts 6# /app/custom/path/src/schema.graphqls.d.ts
You may want to run it every time before calling tsc
. Please check your
package.json
and modify like this.
1 "scripts": { 2- "build": "tsc" 3+ "build": "graphql-let && tsc" 4 },
webpack serve
and CodeEnjoy HMR (Hot Module Replacement) of webpack with the generated react-apollo hooks and IDE code assists.
1import { gql } from 'graphql-let' 2import { useNewsQuery } from './news.graphql' 3 4const { useViewerQuery } = gql(`query Viewer { blaa }`) 5 6const News: React.FC = () => { 7 // Already typed⚡️ 8 const { data: { news } } = useNewsQuery() 9 const { data: { viewer } } = useViewerQuery() 10 return <div>{ news.map(...) }</div> 11}
babel-plugin-macros requires the least configuration to setup.
Please finish 1. Install dependencies, and 2. Configure .graphql-let.yml as you still need .graphql-let.yml.
Put a line "plugins": ["macros"]
to your .babelrc.
If you use Create React App, it contains
babel-plugin-macros out of the box.
If you want a custom path to .graphql-let.yml, you can use configFilePath
babel option. <projectRoot>${configFilePath}
should point to your
.graphql-let.yml.
Thanks to babel-plugin-macros's beautiful architecture, you're ready to use GraphQL codegen values.
1import { gql, load } from "graphql-let/macro" 2 3// Typed⚡️ 4const { useNewsQuery } = gql("query News { braa }") 5const { useViewerQuery } = load("./viewer.graphql")
Note that your schema types are generated in
graphql-let/__generated__/__types__
, instead of per-document outputs.
1import { News } from 'graphql-let/__generated__/__types__'
Mostly the same as babel-plugin-macros, only you need to import "graphql-let"
.
Please finish 1. Install dependencies and 2. Configure .graphql-let.yml as you still need .graphql-let.yml.
1 { 2+ "plugins": ["graphql-let/babel"] 3 }
1import { gql, load } from "graphql-let" 2 3const { useNewsQuery } = gql("query News { braa }") 4const { useViewerQuery } = load("./viewer.graphql")
graphql-let half passes your config options to GraphQL code generator API and half controls them. Here explains how different these and why. You can see this section as a migration guide, too.
1 schema: https://api.github.com/graphql 2 documents: "**/*.graphql" 3- generates: 4- ./__generated__/operations.ts: 5- config: 6- key: value 7- plugins: 8- - typescript 9- - typescript-operations 10- preset: xxx 11+ plugins: 12+ - typescript-operations 13+ config: 14+ key: value
typescript
should not be specifiedYou have to have @graphql-codegen/typescript
as a dev dependency. graphql-let
generates types by default, where it uses the plugin. The plugins
in
.graphql-let.yml is for per-document, which imports the shared types
automatically. If you specify typescript
as a plugin, it's
still okay, but you can imagine it's kind of redundant.
generates
codegen.yml has an option generates
, but it's strictly controlled under
graphql-let. Rather, think graphql-let as a tool to let you forget intermediate
outputs and import/call GraphQL directly.
Therefore, we don't support output-file level configuration such as Output-file level schema, Output-file level documents, and Output Level config right now. But this could be changed logically, so please vote by issuing if you'd like.
preset
Presets
decide how to split/import each other, which graphql-let manages basically.
graphql-let generates per-document .d.ts
and binds up schema types into a
shared file, that's why
@graphql-codegen/import-types-preset
is our peer dependency.
I think you don't need to configure Presets, because graphql-let takes care of what Presets does on your behalf. If you notice the use-case you need more flexibility, please issue it.
documents
expects string | string[]
Document-level options such as noRequir
or
Custom Document Loader
are not supported.
In addition to codegen.yml
options, graphql-let accepts these.
1# "plugins", required. The plugins for GraphQL documents to run GraphQL code 2# generator with. You should omit `typescript` plugin which graphql-let generates internally. 3# See here for more information. https://graphql-code-generator.com/docs/plugins/index 4# Example: 5plugins: 6 - typescript-operations 7 - typescript-react-apollo 8 - add: "/* eslint-disable */" 9 10# "respectGitIgnore", optional. `true` by default. 11# If true, graphql-let will ignore files in .gitignore. 12# Useful to prevent parsing files in such as `node_modules`. 13respectGitIgnore: true 14 15# "cacheDir", optional. `node_modules/.cache/graphql-let` by default. 16# graphql-let takes care of intermediate `.ts(x)`s that GraphQL code generator 17# generates, but we still need to write them on the disk for caching and 18# TypeScript API purposes. This is the directory we store them to. 19# Examples: 20cacheDir: node_modules/.cache/graphql-let 21cacheDir: .cache 22 23# "TSConfigFile", optional. `tsconfig.json` by default. 24# You can specify a custom config for generating `.d.ts`s. 25# Examples: 26TSConfigFile: tsconfig.json 27TSConfigFile: tsconfig.compile.json 28 29# "typeInjectEntrypoint", optional. 30# `node_modules/@types/graphql-let/index.d.ts` by default. Needs to end with ".d.ts". 31# Used as an entrypoint and directory of generated type declarations 32# for `gql()` and `load()` calls. 33typeInjectEntrypoint: node_modules/@types/graphql-let/index.d.ts 34 35# "silent", optional. `false` by default. 36# Pass `true` if you want to suppress all standard output from graphql-let. 37silent: false
Simple example:
1schema: "schema/**/*.graphqls" 2documents: 3 - "**/*.graphql" 4 - "!shouldBeIgnored1" 5plugins: 6 - typescript-operations 7 - typescript-react-apollo
Example with a bit more complicated options:
1schema: 2 - https://api.github.com/graphql: 3 headers: 4 Authorization: YOUR-TOKEN-HERE 5documents: 6 - "**/*.graphql" 7 - "!shouldBeIgnored1" 8plugins: 9 - typescript-operations 10 - typescript-react-apollo 11respectGitIgnore: true 12config: 13 reactApolloVersion: 3 14 apolloReactComponentsImportFrom: "@apollo/client/react/components" 15 useIndexSignature: true 16cacheDir: .cache 17TSConfigFile: tsconfig.compile.json 18typeInjectEntrypoint: typings/graphql-let.d.ts
graphql-let/babel
gql`query {}`
. This is the limitation of TypeScript.
Please answer me if you have any ideas.graphql-let/jestTransformer
is available. Configure your jest.config.js
as:
1 module.exports = { 2 transform: { 3+ "\\.graphql$": "graphql-let/jestTransformer", 4 }, 5 };
babel-jest
in Jestbabel-jest
is the default subsequent transformer of
graphql-let/jestTransformer
. Install these:
1yarn add -D graphql-let babel-jest
And make sure your babel config can compile generated .ts(x)
s.
ts-jest
or other subsequent transformers in JestThe option subsequentTransformer
is available. If you use ts-jest
, your
jest.config.js
will look like this:
1 const { defaults: tsjPreset } = require("ts-jest/presets"); 2 3 module.exports = { 4 preset: "ts-jest", 5 transform: { 6 ...tsjPreset.transform, 7+ "\\.graphql$": [ 8+ "graphql-let/jestTransformer", 9+ { subsequentTransformer: "ts-jest" }, 10+ ], 11 }, 12 };
.graphqls
in JestIf you use graphql-let/schema/loader
, you may want a corresponding
transformer, but remember graphql-let does not transform the content of GraphQL
schema. Just use what you need; it's most likely to be jest-transform-graphql
.
1 module.exports = { 2 transform: { 3 "\\.graphql$": "graphql-let/jestTransformer", 4+ "\\.graphqls$": "jest-transform-graphql", 5 }, 6 };
If you meet the following conditions, graphql-let generates Resolver Types.
schema
@graphql-codegen/typescript-resolvers
installedRun:
1yarn add -D @graphql-codegen/typescript-resolvers 2yarn graphql-let
Then you will get resolver types in graphql-let/__generated__/__types__
.
1import { Resolvers } from "graphql-let/__generated__/__types__"; 2 3const resolvers: Resolvers = { 4 Query: { 5 // All typed⚡️ 6 viewer(parent, args, context, info) { 7 return { ... } 8 }, 9 } 10}; 11 12export default resolvers;
graphql-let/schema/loader
is also available to update resolver types. It doesn't transpile anything;
just detects file modification and passes the content to the next loader.
1 // webpack.config.ts 2 const config: Configuration = { 3 module: { 4 rules: [ 5+ { 6+ test: /\.graphqls$/, 7+ use: [ 8+ { loader: 'graphql-let/schema/loader' }, 9+ ] 10+ } 11 ] 12 } 13 }
d.ts
...?Yes.
The above documentation should work basically, but some of the combinations may require more effort. Please vote by creating issues. Sponsoring me is another way to get my attention🍩🍦👀
These are the states/tools for the syntaxes.
states/tools for syntax | import GraphQL document asimport './a.graphql'; | Inline GraphQL document asimport {gql} from 'graphql-let'; gql(`query {}` ); |
---|---|---|
generating .d.ts s by command graphql-let | ✅ | ✅ |
importing GraphQL content from another as# import A from './a.graphql' | ✅ | ✅ |
webpack loader graphql-let/loader | ✅ | ✅ |
Babel Plugin graphql-let/babel | ✅ | ✅ |
Jest Transformer graphql-let/jestTransfomer | ✅ | Vote by issuing |
Experimental: Resolver Types for GraphQL schema | ✅ byimport {Resolvers} from 'graphql-let/__generated__/__types__' | (I think we don't need this) |
No. There are
more plugins that also generates .ts(x)
s from GraphQL documents.
gql`query News { baa }`;
?Sadly, you need gql()
instead of gql` `
because of
the limitation of TypeScript.
.graphql
and .graphqls
? Can I use .gql
or something else?You can use what you want. I wanted to recommend distinguishing GraphQL schema
and GraphQL documents in the extensions, which will lead to a more
understandable configuration for webpack loaders with fewer pitfalls. Another
reason for .graphqls
is that it's one of
the supported extensions in the internal library.
Query document exports DocumentNode
named ${QueryName}Document
that you can make use of.
.graphql
from another document, especially GraphQL Fragment?Thanks to
graphql-tools/import
,
the syntax # import X from './fragment.graphql'
is supported.
Define your fragment named as partial.graphql
1fragment Partial on User { 2 id 3 name 4}
and import it.
1# import Partial from './partial.graphql' 2query Viewer { 3 viewer { 4 ...Partial 5 } 6}
.tsx
es generated in cacheDir
(.cache
) throw TypeScript errors of wrong import pathsIt's not a bug. Please exclude cacheDir
from your TypeScript compilation. The files in cacheDir
are only intermediates, which will speed your next execution.
Your GraphQL documents -> (call GraphQL code generator API *1) -> .tsx *2 -> (call TypeScript to distribute declarations *3) -> .d.ts
You're seeing the *2
. It's used to skip *1
and *3
, and recodnized as generated implementations, which graphql-let/loader returns, for example.
npm run prepublishOnly
locally will get your local development
ready.Apache License Version 2.0
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 2/12 approved changesets -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
72 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More