New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@epfl-si/react-graphql-simple

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@epfl-si/react-graphql-simple

Await authentication then perform GraphQL requests

  • 0.4.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@epfl-si/react-graphql-simple

A nothing-up-our-sleeves binding for graphql-request in a React application.

  • No frills, cache, cache model, auto-reload, bells or whistles — The request starts as soon as your component calls the function that useGraphQLRequest returns, and is never retried unless and until you restart() it.
  • (therefore) Works indifferently for GraphQL queries and mutations.
  • Optionally provides authentication (only bearer tokens are supported for now); when using this feature, GraphQL requests will be delayed until the first authentication token is received.
  • If you want more (i.e. pagination and infinite queries), see @epfl-si/react-graphql-paginated.

Provide a GraphQL context

The <GraphQLProvider> React component takes a GraphQL endpoint URL and an optional authentication mechanism as parameters.

Here is an example where the authentication part is handled using @epfl-si/react-appauth:


import { OIDCContext, useOpenIDConnectContext } from "@epfl-si/react-appauth";
import { GraphQLProvider } from "@epfl-si/react-graphql-simple";

function App() {
      const oidcContextProps = { authServerUrl, ... }

      return <OIDCContext { ...oidcContextProps }>
        <GraphQLProvider endpoint="/graphql" authentication={
          { bearer: () => useOpenIDConnectContext().accessToken }
        }>
          <MoreComponentsForYourApp/>
        </GraphQLProvider>
      </OIDCContext>
}

Perform GraphQL requests from within the context

The <GraphQLProvider> component provides a React context, meaning that within its children elements, any component may call useGraphQLRequest to perform GraphQL requests against the configured endpoint and wielding credentials from the configured authentication (if any).

Except for the part where it waits for authentication to succeed before sending the request, useGraphQLRequest is implemented in terms of plain graphql-request with no further ado.

Here is an example of a functional React component that calls useGraphQLRequest:

import * as React from "react";
import { gql } from 'graphql-request';
import { useGraphQLRequest } from '@epfl-si/react-graphql-simple';

type Item = { id : number, title : string, description : string };

export function MyGraphQLCapableComponent() {
 const { loading, data, error } = useGraphQLRequest<{ items: Item[] }>(gql`
{
  items {
     id
     title
     description
  }
}`);

    if (loading) return <Throbber/>;
    if (error) return <p className="error">{error.toString()}</p>;
    return <>{ items.map((item) => <p className="item">{item.title}</p>) }</>;
}

Keywords

FAQs

Package last updated on 04 Mar 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