npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pretto/graphql-codegen-loader

v1.5.0

Published

[![npm version](https://img.shields.io/npm/v/@pretto/graphql-codegen-loader.svg?style=flat)](https://www.npmjs.com/package/@pretto/graphql-codegen-loader)

Downloads

1,849

Readme

@pretto/graphql-codegen-loader

npm version

A Webpack loader to seamlessly integrate GraphQL Code Generator into your development workflow.

Table of contents

Installation

yarn add graphql typescript
yarn add --dev @pretto/graphql-codegen-loader

Introduction

graphql-codegen-loader works like any other loader. You define the test pattern for which file path you want the loader to be run, and then specify what other loader you want to be run next before the result is being handled back to the bundler. graphql-codegen-loader outputs a Typescript result so it's important that you run that result through another loader that makes it understandable to the bundler. By default, Webpack does not understand Typescript. In this documentation, I'll be using babel as a Typescript compiler but you may use whatever compiler your may like.

Example

const path = require('path')

module.exports = {
  entry: path.join(__dirname, 'src/index'),

  mode: 'development',

  module: {
    rules: [
      {
        oneOf: [
          {
            exclude: /node_modules/,
            test: /\.gql$/,
            use: [
              { loader: 'babel-loader', options: { presets: ['@babel/preset-typescript'] } },
              {
                loader: '@pretto/graphql-codegen-loader',
                options: {
                  schema: 'https://swapi-graphql.netlify.app/.netlify/functions/index',
                  schemaTypesPath: path.join(__dirname, 'src/types'),
                },
              },
            ],
          },
        ],
      },
    ],
  },

  output: {
    publicPath: '/',
  },

  resolve: {
    extensions: ['.js', '.ts', '.tsx'],
  },
}

Options

Root

| Name | Type | Required | Default value | Description | | ---------------- | ---------------------- | -------- | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | emitFiles | boolean | ❌ | false | If true, graphql-codegen-loader will generate a declaration file beside all imported document files (see. Bare usage) | | fragmentsPaths | string[] | ❌ | [] | List of fragments file path that are shared across multiple queries and therefore need to be interpreted by graphql-codegen-loader. | | plugins | (Plugin \| string)[] | ❌ | [] | Plugins to be added to the GraphQl Code Generator configuration (cf. plugins). | | schema | string | ✅ | N/A | GraphQL schema (cf. schema field). | | schemaTypesPath | string | ✅ | N/A | Folder where the default types will be added. During compile time, graphql-codegen-loader will generate a declaration file for all your schema types (interfaces, scalars...): schema.d.ts. If your schema contains enums, it will also generate a Typescript file: enums.ts, to refer to enums (as a type and as a value) in your application. | | typescriptConfig | string | ❌ | tsconfig.json | Path to your Typescript configuration file. | | useWorkspaces | boolean | ❌ | false | If true, all the generated files will refer to other files using the @workspace/ syntax instead of a relative import. |

Plugin

| Name | Type | Required | Default value | Description | | ------- | ------ | -------- | ------------- | ----------------- | | options | object | ❌ | {} | Plugin's options. | | plugin | string | ✅ | N/A | Plugin's name. |

Usage

Now that we cover the options API, let's see how to use the loader in different contexts. As we said, graphql-codegen-loader is a wrapper around GraphQL Code Generator, and without additional configuration, what it does is very minimal:

  • It tells Webpack how to read a GraphQL file, running this file through the GraphQL Code Generator with for only plugin: typescript-operations. Which basically, does nothing but exporting Typescript types.

Bare usage

Let's consider the following configuration:

// webpack.config.js
{
  schema: 'https://swapi-graphql.netlify.app/.netlify/functions/index',
  schemaTypesPath: path.join(__dirname, 'src/types'),
}
# films.gql
query Films {
  allFilms {
    films {
      director
      id
      releaseDate
      title
    }
  }
}
// index.ts
import { FilmsQuery } from './films.gql'

// FilmsQuery is a type

Here, FilmsQuery is a type that could be use to type data coming from a corresponding GraphQL request but at the end of the day, nothing will be procuded by Webpack. What we have here is just for typing purposes. At this point, you'll even get a error from your IDE because it does not know how to read *.gql files. That is where the emitFiles options comes to the rescue. Let's adjust the Webpack configuration a tiny bit:

// webpack.config.js
{
+ emitFiles: true,
  schema: 'https://swapi-graphql.netlify.app/.netlify/functions/index',
  schemaTypesPath: path.join(__dirname, 'src/types'),
}

This comes very handy. It generates 2 files:

  • A declaration file containing all schema types (interfaces, scalars...). Its location depends on the schemaTypesPath option.
  • A declaration file beside all imported files, in our case:
// films.gql.d.ts
import * as Types from '../../types/schema'

export type FilmsQueryVariables = Types.Exact<{
  [key: string]: never
}>
export type FilmsQuery = {
  __typename?: 'Root'
  allFilms?: {
    __typename?: 'FilmsConnection'
    films?: Array<{
      __typename?: 'Film'
      director?: string | null
      id: string
      releaseDate?: string | null
      title?: string | null
    } | null> | null
  } | null
}

From now on, your IDE should be able to understand *.gql imports, and your type checking should not throw any errors.

Generic usage

Here, we're going to use GraphQL Code Generator plugin Typescript Generic SDK to illustrate how to perform a GraphQL request and type the output using NO framework at all. Let's consider the following configuration:

// webpack.config.js
{
  emitFiles: true,
  plugins: ['typescript-generic-sdk'],
  schema: 'https://swapi-graphql.netlify.app/.netlify/functions/index',
  schemaTypesPath: path.join(__dirname, 'src/types'),
}
// index.ts
import { DocumentNode } from 'graphql'
import { print } from 'graphql/language/printer'
import { useEffect, useState } from 'react'

import { FilmsDocument, FilmsQuery } from './films.gql'

const request = async <T,>(document: DocumentNode): Promise<T> => {
  const query = print(document)

  const response = await fetch('https://swapi-graphql.netlify.app/.netlify/functions/index', {
    body: JSON.stringify({ query }),
    headers: { 'Content-Type': 'application/json' },
    method: 'POST',
  })

  const result = await response.json()

  return result
}

const useGraphQL = <T,>(document: DocumentNode) => {
  const [data, setData] = useState<T | null>(null)
  const [error, setError] = useState<string | null>(null)
  const [loading, setIsLoading] = useState(true)

  useEffect(() => {
    request<T>(document)
      .then(setData)
      .catch(setError)
      .finally(() => {
        setIsLoading(false)
      })
  }, [])

  return { data, error, loading }
}

export const Films = () => {
  const { data, loading } = useGraphQL<FilmsQuery>(FilmsDocument)
  // data is of type FilmsQuery | null

  return null
}

Works like a charm! Looks familiar though? Yep, let's see how we could save some time here.

React Apollo usage

Here, we're going to use GraphQL Code Generator plugin Typescript React Apollo to illustrate how to perform a GraphQL request and type the output using @apollo/client. Let's consider the following configuration:

// webpack.config.js
{
  emitFiles: true,
  plugins: ['typescript-react-apollo'],
  schema: 'https://swapi-graphql.netlify.app/.netlify/functions/index',
  schemaTypesPath: path.join(__dirname, 'src/types'),
}
// index.ts
import { useQuery } from '@apollo/client'

import { FilmsDocument, FilmsQuery } from './films.gql'

export const Films = () => {
  const { data, loading } = useQuery<FilmsQuery>(FilmsDocument)
  // data is of type FilmsQuery | undefined

  return null
}

Same as our generic example but very much simpler! Even cooler, by default the React Apollo plugin exports custom hooks to save more time:

// index.ts
export const Films = () => {
  const { data, loading } = useFilmsQuery()
  // data is of type FilmsQuery | undefined

  return null
}

Isn't it awesome? But this is could be disabled by passing option to the plugin, let's see how by adjusing the Webpack configuration:

// webpack.config.js
{
  emitFiles: true,
- plugins: ['typescript-react-apollo'],
+ plugins: [{ plugin: 'typescript-react-apollo', options: { withHooks: false } }],
  schema: 'https://swapi-graphql.netlify.app/.netlify/functions/index',
  schemaTypesPath: path.join(__dirname, 'src/types'),
}

Other usage

There are many possible use cases by using the amazing GraphQL Code Generator plugins and even combining them together:

Example

This repository comes with a working example using React Apollo. Give it a try!