Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
graphql-request
Advanced tools
š” Minimal GraphQL client supporting Node and browsers for scripts or simple apps
The graphql-request npm package is a minimal GraphQL client for Node.js and browsers, providing a simple and flexible way to execute GraphQL queries, mutations, and subscriptions. It is designed for making HTTP requests to a GraphQL server, and it's a lightweight alternative to more complex GraphQL clients like Apollo Client or Relay.
Simple Query Execution
Execute a simple GraphQL query and log the result.
const { request } = require('graphql-request');
const query = `{ hello }`;
request('https://api.graph.cool/simple/v1/movies', query).then((data) => console.log(data));
Mutation Execution
Execute a GraphQL mutation to create a new resource and log the result.
const { request } = require('graphql-request');
const mutation = `mutation ($title: String!) { createMovie(title: $title) { id } }`;
const variables = { title: 'Inception' };
request('https://api.graph.cool/simple/v1/movies', mutation, variables).then((data) => console.log(data));
Error Handling
Handle errors that may occur during the execution of a GraphQL request.
const { request } = require('graphql-request');
const query = `{ hello }`;
request('https://api.graph.cool/simple/v1/movies', query).catch((error) => console.error(error.response.errors));
GraphQL Subscriptions
Set up a GraphQL subscription to listen for real-time updates.
const { SubscriptionClient } = require('graphql-request');
const ws = require('ws');
const subscriptionClient = new SubscriptionClient('wss://api.graph.cool/simple/v1/movies', { reconnect: true }, ws);
const query = `subscription { newMovie { id title } }`;
const observer = subscriptionClient.request({ query }).subscribe({
next(data) { console.log(data); },
error(err) { console.error(err); }
});
Apollo Client is a comprehensive state management library for JavaScript that enables you to manage both local and remote data with GraphQL. It is more feature-rich than graphql-request, offering advanced features like caching, optimistic UI updates, and integration with various view layers.
urql is a highly customizable and versatile GraphQL client for React applications. It includes features like caching, pagination, and subscriptions. It is similar to graphql-request in its simplicity and ease of use but offers more extensibility and integration with the React ecosystem.
Relay is a framework for building data-driven React applications with GraphQL. It provides a powerful and opinionated set of tools for fetching, storing, and managing GraphQL data. Relay is more complex than graphql-request and is designed for use cases that require fine-grained control over data management and performance optimization.
š” Minimal GraphQL client supporting Node and browsers for scripts or simple apps
async
/ await
)npm install graphql-request
Send a GraphQL query with a single line of code. ā¶ļø Try it out.
import { request } from 'graphql-request'
const query = `{
Movie(title: "Inception") {
releaseDate
actors {
name
}
}
}`
request('https://api.graph.cool/simple/v1/movies', query).then(data => console.log(data))
import { request, GraphQLClient } from 'graphql-request'
// Run GraphQL queries/mutations using a static function
request(endpoint, query, variables).then(data => console.log(data))
// ... or create a GraphQL client instance to send requests
const client = new GraphQLClient(endpoint, { headers: {} })
client.request(query, variables).then(data => console.log(data))
import { GraphQLClient } from 'graphql-request'
const client = new GraphQLClient('my-endpoint', {
headers: {
Authorization: 'Bearer my-jwt-token',
},
})
const query = `{
Movie(title: "Inception") {
releaseDate
actors {
name
}
}
}`
client.request(query).then(data => console.log(data))
import { GraphQLClient } from 'graphql-request'
const client = new GraphQLClient('my-endpoint', {
credentials: 'include',
mode: 'cors'
})
const query = `{
Movie(title: "Inception") {
releaseDate
actors {
name
}
}
}`
client.request(query).then(data => console.log(data))
import { request } from 'graphql-request'
const query = `query getMovie($title: String!) {
Movie(title: $title) {
releaseDate
actors {
name
}
}
}`
const variables = {
title: 'Inception',
}
request('my-endpoint', query, variables).then(data => console.log(data))
import { request } from 'graphql-request'
const wrongQuery = `{
some random stuff
}`
request('my-endpoint', query)
.then(data => console.log(data))
.catch(err => {
console.log(err.response.errors) // GraphQL response errors
console.log(err.response.data) // Response data if available
})
require
instead of import
const { request } = require('graphql-request')
const query = `{
Movie(title: "Inception") {
releaseDate
actors {
name
}
}
}`
request('my-endpoint', query).then(data => console.log(data))
node
npm install fetch-cookie/node-fetch
import { GraphQLClient } from 'graphql-request'
// use this instead for cookie support
global['fetch'] = require('fetch-cookie/node-fetch')(require('node-fetch'))
const client = new GraphQLClient('my-endpoint')
const query = `{
Movie(title: "Inception") {
releaseDate
actors {
name
}
}
}`
client.request(query).then(data => console.log(data))
graphql-tag
graphql-request
, Apollo and Relay?graphql-request
is the most minimal and simplest to use GraphQL client. It's perfect for small scripts or simple apps.
Compared to GraphQL clients like Apollo or Relay, graphql-request
doesn't have a built-in cache and has no integrations for frontend frameworks. The goal is to keep the package and API as minimal as possible.
Lokka is great but it still requires a lot of setup code to be able to send a simple GraphQL query. graphql-request
does less work compared to Lokka but is a lot simpler to use.
Join our Slack community if you run into issues or have questions. We love talking to you!
FAQs
Minimal GraphQL client supporting Node and browsers for scripts or simple apps.
The npm package graphql-request receives a total of 2,445,690 weekly downloads. As such, graphql-request popularity was classified as popular.
We found that graphql-request demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Ā It has 0 open source maintainers 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
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.