Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
gql-profiler
Advanced tools
A standalone performance profiler for GraphQL resolvers
Before considering to use GraphQL Profiler, you might consider Apollo Engine (or just apollo-tracing-js). These tools are more complete, more powerful and they are maintained by the awesome Apollo team.
But GraphQL Profiler has some advantages compared to the Apollo Suite:
Installation
Add the profiler to your project dependencies:
# If you like npm
npm install --save gql-profiler
# or if you like yarn
yarn add gql-profiler
Choose a reporter, wrap your resolvers with the profiler, and use the reporter whenever you want:
import express from 'express';
import { makeExecutableSchema } from 'graphql-tools';
import bodyParser from 'body-parser';
import { graphqlExpress, graphiqlExpress } 'apollo-server-express';
import { profileResolvers, htmlReporter } from 'gql-profiler';
import typeDefs from './typeDefs';
import resolvers from './resolvers';
const reporter = htmlReporter();
const schema = makeExecutableSchema({
typeDefs,
// wrap your resolvers with the profiler
resolvers: profileResolvers(resolvers, { reporter }),
});
const app = express();
// add a route to display profiles
app.get('/profiler', (req, res) => {
res.send(reporter.getHtml());
});
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));
app.listen(3000, () => {
console.log('Server listening at http://localhost:3000');
});
The profiler report will be available at http://localhost:3000/profiler
profileResolvers(resolvers, {
reporter, // A reporter to gather your data (see the list below) - Mandatory
enabled: true, // If false, the resolvers aren't profiled
disableInProduction: true, // Force disable the profiler the env is production
env: process.env.NODE_ENV,
getResolverName: () => '', // Function to get the resolver name
});
Available reporters:
memoryReporter
import { memoryReporter } from 'gql-reporter';
const reporter = memoryReporter();
const events = reporter.getEvents(); // Retrieve each resolver calls
const hierarchy = reporter.getHierarchy(); // Same, but in a dependency tree
reporter.reset(); // Delete all data in the reporter
htmlReporter
import { htmlReporter } from 'gql-reporter';
const reporter = htmlReporter();
const events = reporter.getEvents(); // Retrieve each resolver calls
const hierarchy = reporter.getHierarchy(); // Same, but in a dependency tree
const html = reporter.getHtml(); // Build a nice HTML page with charts
reporter.reset(); // Delete all data in the reporter
nullReporter
import { nullReporter } from 'gql-reporter';
const reporter = nullReporter();
Does literaly nothing, and doesn't have useful API. Used in tests.
More soon?
Writing a reporter is very simple, here is an example of a consoleReporter
:
import uuid from 'uuid';
const consoleReporter = () => ({
// Instanciate a new event that will be passed through the other functions
newEvent: (resolver, args, name) => {
return { id: uuid.v4(), name };
},
start: (evt) => {
console.log(`${evt.name}#${evt.id} started at`, new Date());
},
end: (evt, result) => {
console.log(`${evt.name}#${evt.id} ended at`, new Date());
},
error: (evt, error) => {
console.log(`${evt.name}#${evt.id} encountered an error at`, new Date());
console.error(error);
},
});
Only the newEvent
function is mandatory.
But if you don't provide any of these function, the profiler will fail silently without crashing the resolver.
GraphQL Profiler is licensed under the MIT License, courtesy of marmelab and ARTE.
FAQs
A standalone performance profiler for GraphQL resolvers
We found that gql-profiler 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.