New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

graphql-request-react

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-request-react

React wrapper for graphql-request

latest
Source
npmnpm
Version
1.1.2
Version published
Maintainers
1
Created
Source

graphql-request-react

npm version

React wrapper for graphql-request. 💣

Install

Yarn:

yarn add graphql-request-react

or

Npm:

npm install graphql-request-react

Quickstart

Send a GraphQL query to render an image of Pikachu ⚡. Try the demo ➡

const App = () => {
  const url= 'https://graphql-pokemon.now.sh'
  
  const query = `{
    pokemon(name: "Pikachu") {
      image
    }
  }`

  return (
    <Request url={url} query={query}
      render={data => 
        <img alt={"pokemon"} src={data.pokemon.image}/>
      }
    />
  )
}

Import

import Request from 'graphql-request-react'

Examples

Providing variables for a query

const query = `getPokemon($name: String!) {
  pokemon(name: $name) {
    image
  }
}`

return (
  <Request url={url} query={query}
    /*Add variables object with the variable values*/
    variables={ {name: "Pikachu"} }
    render={data => 
      <img alt={"pokemon"} src={data.pokemon.image}/>
    }
  />
)

Adding HTTP header

<Request url={url} query={query}
  /*Add an options object with appropriate HTTP headers*/
  options={ {headers: {authorization: 'AUTH_TOKEN'}} }
  render={data => 
    <img alt={"pokemon"} src={data.pokemon.image}/>
  }
/>

Handling Loading

You can add a loading function that will be used for rendering during the fetching process.

<Request url={url} query={query}
  render={data => 
    <img alt={"pokemon"} src={data.pokemon.image}/>
  }
  /*Add loading function*/
  loading={() => <h4>Loading Pikachu from Pokedex...</h4>}
/>

Handling Errors

You can add a error function that will be used for rendering in case of an error.

<Request url={url} query={query}
  render={data => 
    <img alt={"pokemon"} src={data.pokemon.image}/>
  }
  /*Add error function*/
  error={(err) => <h4>Couldn't find Pikachu in Pokedex because of {err.message}!</h4>}
/>

Props

NameRequiredDescriptionType
urlYesUrl of the graphql endpointString
queryYesGraphql queryString
renderYesRender function that gets passed in the requested data as an objectFunction
loadingNoRender function during loading stageFunction
errorNoRender function in case of an errorFunction
variablesNoObject that provides the variables to a given queryObject
optionsNoObject that contains fetch options like http-headers.Object

More coming soon...

  • Example for mutations
  • Trigger function

Keywords

graphql

FAQs

Package last updated on 10 Apr 2019

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