graphql-query-utils
v0.0.1
Published
GraphQL query utilities
Downloads
2
Readme
GraphQL Query Utilities
A node.js module with utilities for GraphQL queries:
- Complexity cost calculation for query resolvers
- maximum of 100(%) complexity
- complexity can be assign per object and per resolver
- multiplies complexity of the resolver per item when querying "per_page" on a list
- Arguments definition for query list of objects
- page, per_page
Installation
npm install graphql-query-utils
yarn add graphql-query-utils
Usage
Resolver complexity cost calculation
import { resolveCost } from 'graphql-query-cost'
/**
* complexity is assigned per object
* it has a maximum of 100(%) of complexity budget
*/
const complexity = 1
new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: () => ({
user: {
type: UserType,
resolve: resolveCost((source: any, args: any) => {
return { name: 'User 1' }
}, complexity)
},
}),
}),
})
Arguments for list of types
import { ListArguments, IListArgumentsType, resolveCost } from 'graphql-query-cost'
const complexity = 1
new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: () => ({
users: {
type: new GraphQLList(UserType),
args: { ...ListArguments },
resolve: resolveCost((source: any, args: any) => {
console.log('page ', args.page)
console.log('per page ', args.per_page)
return [ { name: 'User 2' }, { name: 'User 3' } ]
}, complexity)
}
}),
}),
})