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

@graphql-codegen/typescript-react-apollo

Package Overview
Dependencies
Maintainers
4
Versions
4424
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@graphql-codegen/typescript-react-apollo

GraphQL Code Generator plugin for generating a ready-to-use React Components/HOC/Hooks based on GraphQL operations

  • 4.3.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
767K
increased by16.03%
Maintainers
4
Weekly downloads
 
Created

What is @graphql-codegen/typescript-react-apollo?

@graphql-codegen/typescript-react-apollo is a plugin for GraphQL Code Generator that generates TypeScript typings and React Apollo components/hooks from your GraphQL queries and schemas. This helps in creating type-safe GraphQL operations in React applications.

What are @graphql-codegen/typescript-react-apollo's main functionalities?

Generate TypeScript Types

Generates TypeScript types for your GraphQL queries, ensuring type safety and autocompletion in your code editor.

/* GraphQL Query */
const GET_USER = gql`
  query GetUser($id: ID!) {
    user(id: $id) {
      id
      name
      email
    }
  }
`;

/* Generated TypeScript Types */
export type GetUserQuery = {
  user: {
    id: string;
    name: string;
    email: string;
  };
};

export type GetUserQueryVariables = {
  id: string;
};

Generate React Apollo Hooks

Generates custom React hooks for your GraphQL queries, making it easy to fetch data in your React components.

/* Generated React Apollo Hook */
import { useQuery } from '@apollo/client';
import { GetUserQuery, GetUserQueryVariables } from './generated/graphql';

const useGetUser = (variables: GetUserQueryVariables) => {
  return useQuery<GetUserQuery, GetUserQueryVariables>(GET_USER, { variables });
};

/* Usage in a React Component */
const UserComponent = ({ userId }) => {
  const { data, loading, error } = useGetUser({ id: userId });

  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error: {error.message}</p>;

  return (
    <div>
      <h1>{data.user.name}</h1>
      <p>{data.user.email}</p>
    </div>
  );
};

Generate React Apollo Components

Generates React components for your GraphQL queries, providing a declarative way to fetch and display data.

/* Generated React Apollo Component */
import { Query } from '@apollo/client/react/components';
import { GetUserQuery, GetUserQueryVariables } from './generated/graphql';

const GetUserComponent = ({ variables }: { variables: GetUserQueryVariables }) => (
  <Query<GetUserQuery, GetUserQueryVariables> query={GET_USER} variables={variables}>
    {({ data, loading, error }) => {
      if (loading) return <p>Loading...</p>;
      if (error) return <p>Error: {error.message}</p>;

      return (
        <div>
          <h1>{data.user.name}</h1>
          <p>{data.user.email}</p>
        </div>
      );
    }}
  </Query>
);

Other packages similar to @graphql-codegen/typescript-react-apollo

FAQs

Package last updated on 09 Sep 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