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

urql

Package Overview
Dependencies
Maintainers
20
Versions
190
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

urql

A highly customizable and versatile GraphQL client for React

  • 4.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
85K
decreased by-62.71%
Maintainers
20
Weekly downloads
 
Created

What is urql?

urql is a highly customizable and versatile GraphQL client for React and other frameworks. It provides a set of tools to handle GraphQL queries, mutations, and subscriptions with ease, offering a flexible and extensible API.

What are urql's main functionalities?

GraphQL Queries

This feature allows you to perform GraphQL queries to fetch data from a GraphQL server. The `useQuery` hook is used to execute the query and manage the loading, error, and data states.

const { useQuery } = require('urql');

const query = `
  query {
    todos {
      id
      text
      completed
    }
  }
`;

function Todos() {
  const [result] = useQuery({ query });
  const { data, fetching, error } = result;

  if (fetching) return <p>Loading...</p>;
  if (error) return <p>Oh no... {error.message}</p>;

  return (
    <ul>
      {data.todos.map(todo => (
        <li key={todo.id}>{todo.text}</li>
      ))}
    </ul>
  );
}

GraphQL Mutations

This feature allows you to perform GraphQL mutations to modify data on the server. The `useMutation` hook is used to execute the mutation and handle the result.

const { useMutation } = require('urql');

const mutation = `
  mutation($text: String!) {
    addTodo(text: $text) {
      id
      text
      completed
    }
  }
`;

function AddTodo() {
  const [result, executeMutation] = useMutation(mutation);

  const addTodo = async (text) => {
    await executeMutation({ text });
  };

  return (
    <button onClick={() => addTodo('New Todo')}>Add Todo</button>
  );
}

GraphQL Subscriptions

This feature allows you to subscribe to real-time updates from a GraphQL server. The `useSubscription` hook is used to handle the subscription and manage the incoming data.

const { useSubscription } = require('urql');

const subscription = `
  subscription {
    newTodo {
      id
      text
      completed
    }
  }
`;

function NewTodos() {
  const [result] = useSubscription({ query: subscription });
  const { data, error } = result;

  if (error) return <p>Oh no... {error.message}</p>;

  return (
    <ul>
      {data && data.newTodo && (
        <li key={data.newTodo.id}>{data.newTodo.text}</li>
      )}
    </ul>
  );
}

Other packages similar to urql

Keywords

FAQs

Package last updated on 05 Nov 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