What is info in resolver in GraphQL: Explanation and Example
info argument in a resolver provides detailed information about the execution state of the query, including the query's abstract syntax tree (AST) and schema details. It helps you understand the query structure and can be used for advanced tasks like query analysis or optimization.How It Works
Imagine you are a chef preparing a meal based on a customer's order. The info argument is like the detailed recipe that tells you exactly what dishes the customer wants and how they want them prepared. In GraphQL, when a resolver runs, it receives this info object that contains the full details of the query being executed.
This info object includes the query's abstract syntax tree (AST), which is a tree-like structure representing the query's fields and subfields. It also contains the schema information and the path to the current field being resolved. This helps the resolver understand the context and structure of the query, allowing it to make smarter decisions or optimizations.
Example
This example shows a simple GraphQL resolver using the info argument to log the requested fields in the query.
const { graphql, buildSchema } = require('graphql'); // Define a simple schema const schema = buildSchema(` type Query { user: User } type User { id: ID name: String email: String } `); // Resolver function using info const root = { user: (parent, args, context, info) => { // Log the fields requested by the client const requestedFields = info.fieldNodes[0].selectionSet.selections.map(field => field.name.value); console.log('Requested fields:', requestedFields); // Return dummy data return { id: '1', name: 'Alice', email: 'alice@example.com' }; } }; // Sample query requesting id and name const query = `{ user { id name } }`; graphql(schema, query, root).then(response => { console.log('Response:', response.data); });
When to Use
You use the info argument when you need to understand more about the query being executed beyond just the arguments or parent data. For example, if you want to optimize database queries by only fetching requested fields, info helps you know exactly which fields the client asked for.
It is also useful for building advanced features like query complexity analysis, authorization checks based on requested fields, or custom logging of query details. In short, whenever you want your resolver to be aware of the query structure or execution context, info is the tool to use.
Key Points
infoprovides the query's structure and execution details.- It contains the abstract syntax tree (AST) of the query.
- Useful for optimizing data fetching and advanced query handling.
- Helps in understanding which fields the client requested.
- Commonly used in complex resolvers and middleware.
Key Takeaways
info argument gives resolvers detailed query structure and context.info to optimize data fetching by knowing requested fields.info object contains the query's abstract syntax tree (AST).info improves resolver flexibility and efficiency.