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

next-graphql-let-plugin

v1.0.0

Published

Frictionless graphql-let setup for Next.js

Downloads

3

Readme

Next.js + graphql-let plugin

This Next.js plugin makes it easy to setup graphql-let in your Next.js project. The main purpose of this plugin is to eliminate configuration and re-generation friction that you might experience with pure graphql-let setup. Mainly this plugin make next.config.js cleaner and re-generates the types and query hooks on every yarn dev or yarn build run.

Getting started

Although Next.js plugins usually don't require a lot of setup to get started, this one required couple of steps to be completed before it will work:

1. Install dependencies

Note that some of these dependencies are here to support @apollo/client integration specifically.

With yarn:

yarn add @apollo/client graphql graphql-let
yarn add -D next-graphql-let-plugin @graphql-codegen/cli @graphql-codegen/import-types-preset @graphql-codegen/typescript @graphql-codegen/typescript-operations @graphql-codegen/typescript-react-apollo

With npm:

npm i @apollo/client graphql graphql-let
npm i -D next-graphql-let-plugin @graphql-codegen/cli @graphql-codegen/import-types-preset @graphql-codegen/typescript @graphql-codegen/typescript-operations @graphql-codegen/typescript-react-apollo

2. Add the plugin to next.config.js.

// next.config.js
const withGraphQLLetPlugin = require("next-graphql-let-plugin")({
  // Plugin options go here...
});

module.exports = withGraphQLLetPlugin({
  // Regular config goes here...
});

3. Configure graphql-let.yml

Since graphql-let does not allow to use json configuration that could be defined through the code, we have to create a graphql-let.yml file with the configuration. Luckily, this plugin will create that file for you if you don't have one - su just run:

# yarn
yarn dev

# npm
npm run dev

Then, you just have to customize the configuration to your needs. For starters and simple schema we recommend following schema:

schema: "**/*.graphqls"
documents: "**/*.graphql"
plugins:
  - typescript-operations
  - typescript-react-apollo
cacheDir: .cache

You can also provide a remote schema in the config by pointing it to the GraphQL API url.

You can find full options that are available for you in graphql-let.yml config here.

4. Add client provider

You will need to create a GraphQL client provider. To have the provider work on every page in your Next.js project it's best to add it to the _app.tsx file. You can find a full guide on how to setup Apollo Client in a frontend app on this page, but here's a simple example you can use:

import type { AppProps } from "next/app";
import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client";

const client = new ApolloClient({
  uri: "", // Enter URL to your API here
  cache: new InMemoryCache(),
});

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <ApolloProvider client={client}>
      <Component {...pageProps} />
    </ApolloProvider>
  );
}

export default MyApp;

5. Run yarn dev or npm run dev

That's it! You can now add .graphql files to your project with queries defined in them and graphql-let will generate fetching functions for you that you can use straight in your project like so:

# lib/queries/author.graphql
query GetAuthors {
  authors {
    id
    name
  }
}

The above query gets converted into a query hook like this:

import { useGetAuthorsQuery } from "../lib/queries/author.graphql";

export default function Home() {
  const { data } = useGetAuthorsQuery();

  return (
    <ul>
      {data?.authors.map((author) => (
        <li key={author.id}>author.name</li>
      ))}
    </ul>
  );
}

Contributors

License

MIT