Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
graphql-fields
Advanced tools
The graphql-fields npm package is a utility for extracting the fields requested in a GraphQL query. It helps in resolving only the necessary fields, optimizing the performance of your GraphQL server by avoiding over-fetching data.
Extracting Fields from a GraphQL Query
This feature allows you to extract the fields requested in a GraphQL query. By using the `graphqlFields` function and passing the `info` object from the resolver, you can get an object representing the fields requested. This can be used to optimize data fetching by only querying the necessary fields from your data source.
const graphqlFields = require('graphql-fields');
const resolvers = {
Query: {
user: (parent, args, context, info) => {
const fields = graphqlFields(info);
console.log(fields); // Logs the fields requested in the query
// Fetch and return the user data based on the requested fields
}
}
};
Handling Nested Fields
This feature allows you to handle nested fields in a GraphQL query. The `graphqlFields` function will return an object that includes nested fields, enabling you to understand the full structure of the query and fetch data accordingly.
const graphqlFields = require('graphql-fields');
const resolvers = {
Query: {
user: (parent, args, context, info) => {
const fields = graphqlFields(info);
console.log(fields); // Logs nested fields as well
// Fetch and return the user data based on the requested fields
}
}
};
The graphql-parse-resolve-info package provides similar functionality by parsing the resolve info to extract the fields requested in a GraphQL query. It offers more detailed information about the query, including arguments and fragments, making it a more comprehensive solution compared to graphql-fields.
Turns GraphQLResolveInfo into a map of the requested fields. Flattens all fragments and duplicated fields into a neat object to easily see which fields were requested at any level.
Schema Type definition
const graphqlFields = require('graphql-fields');
const graphql = require('graphql')
module.exports = new graphql.GraphQLObjectType({
name: 'User',
fields: {
profile: {type: new graphql.GraphQLObjectType({
name: 'Profile',
fields: {
firstName: {type: graphql.GraphQLString},
lastName: {type: graphql.GraphQLString},
middleName: {type: graphql.GraphQLString}
}
}),
email: {type: graphql.GraphQLString},
id: {type: graphql.GraphQLID}
},
resolve(root, args, info) {
console.log(
JSON.stringify(graphqlFields(info), null, 2);
);
...
}
});
Query
{
user {
...A
profile {
...B
firstName
}
}
}
fragment A on User {
...C
id,
profile {
lastName
}
}
Fragment B on Profile {
firstName
}
Fragment C on User {
email,
profile {
middleName
}
}
will log
{
"profile": {
"firstName": {},
"lastName": {},
"middleName": {}
},
"email": {},
"id": {}
}
An underlying REST api may only return fields based on query params.
{
user {
profile {
firstName
},
id
}
}
should request /api/user?fields=profile,id
while
{
user {
email
}
}
should request /api/user?fields=email
Implement your resolve method like so:
resolve(root, args, info) {
const topLevelFields = Object.keys(graphqlFields(info})
return fetch(`/api/user?fields=${topLevelFields.join(',}`);
}
npm test
FAQs
Turns GraphQLResolveInfo into a map of the requested fields
The npm package graphql-fields receives a total of 100,922 weekly downloads. As such, graphql-fields popularity was classified as popular.
We found that graphql-fields 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
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.