Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
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 backend).
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 help us develop this software sustainably under the MIT license, we ask all individuals and businesses that use it to help support its ongoing maintenance and development via sponsorship.
And please give some love to our featured sponsors 🤩:
Surge * | Netflix * | Qwick * | The Guild * |
Chad Furman * | Fanatics * | Dovetail * | Enzuzo * |
Stellate * |
* Sponsors the entire Graphile suite
parseResolveInfo(resolveInfo)
Alias: parse
Gets the tree of subfields of the current field that is being requested, returning the following properties (recursively):
name
: the name of the GraphQL fieldalias
: the alias this GraphQL field has been requested as, or if no alias was specified then the name
args
: the arguments this field was called with; at the root level this
will be equivalent to the args
that the resolve(data, args, context, resolveInfo) {}
method receives, at deeper levels this allows you to get the
args
for the nested fields without waiting for their resolvers to be called.fieldsByTypeName
: an object keyed by GraphQL object type names, where the
values are another object keyed by the aliases of the fields requested with
values of the same format as the root level (i.e. {alias, name, args, fieldsByTypeName}
); see below for an exampleNote that because GraphQL supports interfaces a resolver may return items of
different types. For this reason, we key the fields by the GraphQL type name of
the various fragments that were requested into the fieldsByTypeName
field.
Once you know which specific type the result is going to be, you can then use
this type (and its interfaces) to determine which sub-fields were requested -
we provide a simplifyParsedResolveInfoFragmentWithType
helper to aid you with
this. In many cases you will know what type the result will be (because it can
only be one type) so you will probably use this helper heavily.
Example usage:
const {
parseResolveInfo,
simplifyParsedResolveInfoFragmentWithType
} = require('graphql-parse-resolve-info');
// or import { parseResolveInfo, simplifyParsedResolveInfoFragmentWithType } 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 } = simplifyParsedResolveInfoFragmentWithType(
parsedResolveInfoFragment,
ComplexType
);
console.dir(fields);
...
}
}
}
});
simplifyParsedResolveInfoFragmentWithType(parsedResolveInfoFragment, ReturnType)
Alias: simplify
Given an object of the form returned by parseResolveInfo(...)
(which can be
the root-level instance, or it could be one of the nested subfields) and a
GraphQL type this method will return an object of the form above, with an
additional field fields
which only contains the fields compatible with the
specified ReturnType
.
Or, in other words, this simplifies the fieldsByTypeName
to an object of only
the fields compatible with ReturnType
.
Example usage:
const {
parseResolveInfo,
simplifyParsedResolveInfoFragmentWithType
} = require('graphql-parse-resolve-info');
new GraphQLObjectType({
name: ...
fields: {
...
foo: {
type: new GraphQLNonNull(ComplexType),
resolve(data, args, context, resolveInfo) {
const parsedResolveInfoFragment = parseResolveInfo(resolveInfo);
const { fields } = simplifyParsedResolveInfoFragmentWithType(
parsedResolveInfoFragment,
ComplexType
);
...
}
}
}
});
getAliasFromResolveInfo(resolveInfo)
Alias: getAlias
Returns the alias of the field being requested (or, if no alias was specified, then the name of the field).
Example:
const { getAliasFromResolveInfo } = require('graphql-parse-resolve-info');
new GraphQLObjectType({
name: ...
fields: {
...
foo: {
type: new GraphQLNonNull(GraphQLString),
resolve(data, args, context, resolveInfo) {
const alias = getAliasFromResolveInfo(resolveInfo);
return alias;
}
}
}
});
For the following GraphQL query:
{
allPosts {
edges {
cursor
node {
...PostDetails
author: personByAuthorId {
firstPost {
...PostDetails
}
friends {
nodes {
...PersonDetails
}
totalCount
pageInfo {
startCursor
}
}
}
}
}
}
}
fragment PersonDetails on Person {
id
name
firstName
}
fragment PostDetails on Post {
id
headline
headlineTrimmed
author: personByAuthorId {
...PersonDetails
}
}
The following resolver in the allPosts
field:
const Query = new GraphQLObjectType({
name: "Query",
fields: {
allPosts: {
type: new GraphQLNonNull(PostsConnection),
resolve(parent, args, context, resolveInfo) {
const parsedResolveInfoFragment = parseResolveInfo(resolveInfo);
const simplifiedFragment = simplifyParsedResolveInfoFragmentWithType(
parsedResolveInfoFragment,
resolveInfo.returnType
);
// ...
},
},
// ...
},
});
has parsedResolveInfoFragment
:
{ alias: 'allPosts',
name: 'allPosts',
args: {},
fieldsByTypeName:
{ PostsConnection:
{ edges:
{ alias: 'edges',
name: 'edges',
args: {},
fieldsByTypeName:
{ PostsEdge:
{ cursor:
{ alias: 'cursor',
name: 'cursor',
args: {},
fieldsByTypeName: {} },
node:
{ alias: 'node',
name: 'node',
args: {},
fieldsByTypeName:
{ Post:
{ id: { alias: 'id', name: 'id', args: {}, fieldsByTypeName: {} },
headline:
{ alias: 'headline',
name: 'headline',
args: {},
fieldsByTypeName: {} },
headlineTrimmed:
{ alias: 'headlineTrimmed',
name: 'headlineTrimmed',
args: {},
fieldsByTypeName: {} },
author:
{ alias: 'author',
name: 'personByAuthorId',
args: {},
fieldsByTypeName:
{ Person:
{ id: { alias: 'id', name: 'id', args: {}, fieldsByTypeName: {} },
name: { alias: 'name', name: 'name', args: {}, fieldsByTypeName: {} },
firstName:
{ alias: 'firstName',
name: 'firstName',
args: {},
fieldsByTypeName: {} },
firstPost:
{ alias: 'firstPost',
name: 'firstPost',
args: {},
fieldsByTypeName:
{ Post:
{ id: { alias: 'id', name: 'id', args: {}, fieldsByTypeName: {} },
headline:
{ alias: 'headline',
name: 'headline',
args: {},
fieldsByTypeName: {} },
headlineTrimmed:
{ alias: 'headlineTrimmed',
name: 'headlineTrimmed',
args: {},
fieldsByTypeName: {} },
author:
{ alias: 'author',
name: 'personByAuthorId',
args: {},
fieldsByTypeName:
{ Person:
{ id: { alias: 'id', name: 'id', args: {}, fieldsByTypeName: {} },
name: { alias: 'name', name: 'name', args: {}, fieldsByTypeName: {} },
firstName:
{ alias: 'firstName',
name: 'firstName',
args: {},
fieldsByTypeName: {} } } } } } } },
friends:
{ alias: 'friends',
name: 'friends',
args: {},
fieldsByTypeName:
{ PeopleConnection:
{ nodes:
{ alias: 'nodes',
name: 'nodes',
args: {},
fieldsByTypeName:
{ Person:
{ id: { alias: 'id', name: 'id', args: {}, fieldsByTypeName: {} },
name: { alias: 'name', name: 'name', args: {}, fieldsByTypeName: {} },
firstName:
{ alias: 'firstName',
name: 'firstName',
args: {},
fieldsByTypeName: {} } } } },
totalCount:
{ alias: 'totalCount',
name: 'totalCount',
args: {},
fieldsByTypeName: {} },
pageInfo:
{ alias: 'pageInfo',
name: 'pageInfo',
args: {},
fieldsByTypeName:
{ PageInfo:
{ startCursor:
{ alias: 'startCursor',
name: 'startCursor',
args: {},
fieldsByTypeName: {} } } } } } } } } } } } } } } } } } },
and the simplified simplifiedFragment
is the same as
parsedResolveInfoFragment
, but with the additional root-level property
fields
which compresses the root-level property fieldsByTypeName
to a
single-level object containing only the fields compatible with
resolveInfo.returnType
(in this case: only edges
):
{ alias: 'allPosts',
name: 'allPosts',
args: {},
fieldsByTypeName:
...as before...
fields:
{ edges:
{ alias: 'edges',
name: 'edges',
args: {},
fieldsByTypeName:
...as before...
This project was originally based on https://github.com/tjmehta/graphql-parse-fields, but has evolved a lot since then.
FAQs
Parse GraphQLResolveInfo (the 4th argument of resolve) into a simple tree
The npm package graphql-parse-resolve-info receives a total of 104,900 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
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.