Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gql-ts-builder

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gql-ts-builder

The main goal of this package is simplify creation of graphql queries

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Graphql query builder

This npm package aims to simplify the process of creating GraphQL queries by providing a type-first approach. It addresses common issues such as mistyped field names and inconsistencies between query fields and types.

With this package, you can:

  1. Define the type upfront to streamline GraphQL query construction, ensuring clarity and consistency in your codebase.
  2. Use TypeScript and VS Code functionalities to enable intelligent auto-completion tailored to the defined type, enhancing development efficiency.
  3. Use TypeScript's capabilities to automatically infer the data types returned by the GraphQL queries constructed using this package, eliminating manual type writing.

Installation:

npm install gql-ts-builder

Example:

import { useQuery, gql, DocumentNode } from "@apollo/client";
import { createQueryBuilder, InferSelection } from "gql-ts-builder";

// Define types
type User = {
  id: string;
  name?: string;
  email: string;
};

// Set up query builder
const buildUserQuery = createQueryBuilder<User, DocumentNode>(
  (selection) => gql`
    query User {
        user {
            ${selection}
        }
    }
`
);

// Construct a query
const query = buildUserQuery({
  id: true,
  name: true,
});

// Infer result types
// Payload type will be { id: string; name?: string; }
type Payload = InferSelection<typeof query>;

type Data = {
  user: Payload;
};

// Create custom hook
export const useUser = () => {
  const { data, loading, error } = useQuery<Data>(query);

  return {
    userData: data?.user,
    userLoading: loading,
    userError: error,
  };
};

This package aims to enhance the developer experience when working with GraphQL, reducing errors and improving productivity.

Keywords

FAQs

Package last updated on 07 Feb 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc