Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
graphql-parse-resolve-info
Advanced tools
Parse GraphQLResolveInfo (the 4th argument of resolve) into a simple tree
The graphql-parse-resolve-info package is a utility for parsing GraphQL resolve info objects. It helps in extracting detailed information about the fields requested in a GraphQL query, which can be useful for optimizing database queries, logging, and other purposes.
Parsing Resolve Info
This feature allows you to parse the resolve info object provided by GraphQL resolvers. The parsed information includes details about the fields requested in the query, which can be used to optimize data fetching.
const { parseResolveInfo } = require('graphql-parse-resolve-info');
const resolveInfo = /* GraphQL resolve info object */;
const parsedInfo = parseResolveInfo(resolveInfo);
console.log(parsedInfo);
Getting Fields from Resolve Info
This feature helps in extracting the fields requested in the GraphQL query from the resolve info object. This can be useful for determining which fields need to be fetched from the database.
const { getFields } = require('graphql-parse-resolve-info');
const resolveInfo = /* GraphQL resolve info object */;
const fields = getFields(resolveInfo);
console.log(fields);
Handling Nested Fields
This feature allows you to handle nested fields in the GraphQL query. By parsing the resolve info object, you can access nested fields and optimize your data fetching accordingly.
const { parseResolveInfo } = require('graphql-parse-resolve-info');
const resolveInfo = /* GraphQL resolve info object */;
const parsedInfo = parseResolveInfo(resolveInfo);
const nestedFields = parsedInfo.fieldsByTypeName.TypeName.nestedFieldName;
console.log(nestedFields);
The graphql-fields package provides a simple way to get the fields requested in a GraphQL query. It is similar to graphql-parse-resolve-info but offers a more straightforward API for extracting fields. However, it may not provide as detailed information as graphql-parse-resolve-info.
Parses a GraphQLResolveInfo object into a tree of the fields that are being requested to enable optimisations to your GraphQL schema (e.g. to determine which fields are required from the SQL database).
Useful for optimising your GraphQL resolvers by allowing them to look ahead in the request, reducing the number of SQL queries or HTTP requests required to complete the GraphQL request.
Also provides a quick way to get the alias of the current field being resolved.
To get the tree of subfields of the current field that are being requested:
const parseResolveInfo = require('graphql-parse-resolve-info');
// or import parseResolveInfo from 'graphql-parse-resolve-info';
new GraphQLObjectType({
name: ...
fields: {
...
foo: {
type: new GraphQLNonNull(ComplexType),
resolve(data, args, context, resolveInfo) {
const parsedResolveInfoFragment = parseResolveInfo(resolveInfo);
const { fields } =
parseResolveInfo.simplifyParsedResolveInfoFragmentWithType(parsedResolveInfoFragment, ComplexType);
console.dir(fields);
...
}
}
}
});
(Note that because GraphQL supports interfaces and hence a resolver may return items of different types we key the fields by the GraphQL type name of the various fragments that were requested. Once you know what type the result was, you can then use this type (and its interfaces) to determine which sub-fields were requested. It's quite commont to know that your result will be of a single type, so we provide a helper that will simplify this for you by passing it the expected type.)
To get the alias of the current field being resolved (defaults to the field name if no alias was specified):
const parseResolveInfo = require('graphql-parse-resolve-info');
new GraphQLObjectType({
name: ...
fields: {
...
foo: {
type: new GraphQLNonNull(GraphQLString),
resolve(data, args, context, resolveInfo) {
const alias = parseResolveInfo(resolveInfo, { aliasOnly: true });
return alias;
}
}
}
});
This project was originally based on https://github.com/tjmehta/graphql-parse-fields, but has evolved a lot since then.
0.0.1-alpha1.0 (2017-07-05)
FAQs
Parse GraphQLResolveInfo (the 4th argument of resolve) into a simple tree
The npm package graphql-parse-resolve-info receives a total of 99,174 weekly downloads. As such, graphql-parse-resolve-info popularity was classified as popular.
We found that graphql-parse-resolve-info 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
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.