Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
graphql-react
Advanced tools
A lightweight GraphQL client for React.
⚠️ SSR API coming soon.
.mjs
.module
entry for tree shaking bundlers.id
fields.__typename
insertion.Install with npm:
npm install graphql-react
Create and provide a GraphQL client:
import { GraphQL, GraphQLProvider } from 'graphql-react'
const graphql = new GraphQL()
const Page = () => (
<GraphQLProvider value={graphql}>
<!-- Now use GraphQLQuery or GraphQLMutation components -->
</GraphQLProvider>
)
See the example Next.js app and GraphQL API.
A React component to manage a GraphQL query.
Parameters
props
Object Component props.
props.variables
Object? GraphQL query variables.props.query
String GraphQL query.props.loadOnMount
Boolean Should the query load when the component mounts. (optional, default true
)props.loadOnReset
Boolean Should the query load when the GraphQL client cache is reset. (optional, default true
)props.resetOnLoad
Boolean Should the GraphQL client cache reset when the query loads. (optional, default false
)children
Function Render function.Examples
import { GraphQLQuery } from 'graphql-react'
const Profile = ({ userId }) => (
<GraphQLQuery
variables={{ userId }}
query={`
query user($userId: ID!) {
user(userId: $id) {
name
}
}
`}
>
{({ load, loading, httpError, parseError, graphQLErrors, data }) => (
<article>
{loading && <span>Loading…</span>}
{(httpError || parseError || graphQLErrors) && <strong>Error!</strong>}
{data && <h1>{data.user.name}</h1>}
<button onClick={load} disabled={loading}>
Reload
</button>
</article>
)}
</GraphQLQuery>
)
Returns String HTML.
A React component to manage a GraphQL mutation. The same as GraphQLQuery but with different default props.
Parameters
props
Object Component props.
props.variables
Object? GraphQL query variables.props.query
String GraphQL query.props.loadOnMount
Boolean Should the query load when the component mounts. (optional, default false
)props.loadOnReset
Boolean Should the query load when the GraphQL client cache is reset. (optional, default false
)props.resetOnLoad
Boolean Should the GraphQL client cache reset when the query loads. (optional, default true
)children
Function Render function.Examples
import { GraphQLMutation } from 'graphql-react'
const ClapArticleButton = ({ articleId }) => (
<GraphQLMutation
variables={{ articleId }}
query={`
mutation clapArticle($articleId: ID!) {
clapArticle(articleId: $id) {
clapCount
}
}
`}
>
{({ load, loading, httpError, parseError, graphQLErrors, data }) => (
<aside>
{(httpError || parseError || graphQLErrors) && <strong>Error!</strong>}
{data && <p>Clapped {data.clapArticle.clapCount} times.</p>}
<button onClick={load} disabled={loading}>
Clap
</button>
</aside>
)}
</GraphQLMutation>
)
Returns String HTML.
A lightweight GraphQL client with a request cache.
Parameters
options
Object Options. (optional, default {}
)
options.cache
Object Cache to import; useful once a SSR API is available. (optional, default {}
)options.requestOptions
RequestOptionsOverride? A function that accepts and modifies generated options for every request.Examples
const graphql = new GraphQL({
requestOptions: options => {
options.url = 'https://api.example.com/graphql'
options.credentials = 'include'
}
})
Resets the cache. Useful when a user logs out.
Examples
graphql.reset()
Queries a GraphQL server.
Parameters
operation
Operation GraphQL operation object.Returns ActiveQuery In-flight query details.
JSON serializable result of a request (including all errors and data) for caching purposes.
Type: Object
Properties
httpError
Object HTTP error.
parseError
String Parse error message.graphQLErrors
Object GraphQL response errors.data
Object GraphQL response data.A promise for an in-flight query that resolves the request cache.
Type: Promise<RequestCache>
Type: Object
Properties
pastRequestCache
RequestCache? Results from the last identical request.requestHash
String Request options hash.request
RequestCachePromise Promise that resolves fresh request cache.A way to override request options generated for a fetch. Modify the provided options object directly; no return.
Type: Function
Parameters
requestOptions
RequestOptionsExamples
options => {
options.url = 'https://api.example.com/graphql'
options.credentials = 'include'
}
A cache update listener callback.
Type: Function
Parameters
requestCache
RequestCache Request cache.Options for a GraphQL fetch request. See polyfillable fetch options.
Type: Object
Properties
url
String A GraphQL API URL.body
(String | FormData) HTTP request body.headers
Object HTTP request headers.credentials
String? Authentication credentials mode.A GraphQL operation object. Additional properties may be used; all are sent to the GraphQL server.
Type: Object
Properties
0.1.0
Initial release.
FAQs
A GraphQL client for React using modern context and hooks APIs that’s lightweight (< 4 kB) but powerful; the first Relay and Apollo alternative with server side rendering.
The npm package graphql-react receives a total of 1,669 weekly downloads. As such, graphql-react popularity was classified as popular.
We found that graphql-react demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.