GraphQL Query Mapper
GraphQL Query Mapper will help you to build GraphQL API
without overfetching data on the server.
GraphQL Query Mapper
transform resolvers info
argument into structure that can be
used to filter data based on fields queried by client.
Example use case
When building public GraphQL API external developers can often missuse the queries.
For example getProfile
query that does expensive fetch from different servers can be used
only to fetch username
for home page. To prevent from overfetching we can extract
information about required fields from the info object and avoid expensive queries.
Query Mapping
Query mapping will prevent from server side database overfetching data by
providing list of the fields that were requested in the client side query.
Developers can use them to perform targeted queries against their database and rest endpoints.
Library exposes following methods:
getQueryObject
: provides list of fields that user queried with additional helpers for database access
Example:
import { getQueryObject} from '@aerogear/graphql-query-mapper'
const resolvers = {
Query: {
models (_, params, context, info) {
const queryData = getQueryObject(info)
console.log(`${queryData.getRootFields()}`)
}
}
}
API
getQueryObject
returns following type
fields: string[];
relations: {
[relationName: string]: any;
};
hasRelations(): boolean;
hasRelation(name: string): boolean;
getRootFields(separator?: string): string;
getRelationFields(relation: string, mapper?: string, separator?: string): any;
expandToGraph(data: any): any;
Limitations
Derived fields will still require additional checks in the resolver.
For example fullname
that consist of the firstName
+secondName
from database:
if(fields.fullname){
fields.push("firstName")
fields.push("secondName")
}
Performance consideration
When using query mapper we can opt out from default GraphQL query execution logic and only use only top level (root) query resolvers. Root resolvers can fetch all data required from relationships and deliver it much faster than in classical execution plan that needs to traverse thru entire graph.
Additionally developers can use graphql compiler to provide V8 optimializations for Node.js queries.
See https://github.com/zalando-incubator/graphql-jit for more information.
Applying this patterns will help to archieve up to ~15 times better performance comparimng to using graphql reference implementation. This aproach will not require Facebook Data Loader cache layer sice all queries and data will be controlled from the root.
Roadmap
Contributing
- Star repository
- Check CONTRIBUTING.md
License
Apache-2.0
Notes
Project maintained by AeroGear GraphQL Team:
https://github.com/aerogear/graphql-home