Skip to content

Calculate query comlexity outside validation context #16

Closed
@BeeHiveJava

Description

@BeeHiveJava

I was wondering if it's possible to calculate complexity based on the current query, statically. In an ideal situation we'd be able to pass a string containing a GraphQL query or an info object which would return the complexity for that query.

This way we would be able to display the cost of a query to the user similar to how GitHub does: https://developer.github.com/v4/guides/resource-limitations/#returning-a-calls-rate-limit-status.

I've tried fiddling around by manually invoking the nodeComplexity function:

class SomeResolver {
    public async resolve(root: any, args: any, ctx: IAppContext, info: GraphQLResolveInfo): Promise<number> {
        const context = new ValidationContext(info.schema, ... /* I have no idea how to get this */ , new TypeInfo(info.schema));
        const complexity = this.getQueryComplexity(ctx)(context) as QueryComplexity;
        const result = complexity.nodeComplexity(..., ... /* I have no idea how to get these */ );

        return result;
    }

    private getQueryComplexity(ctx: IAppContext): Function {
        return queryComplexity({
            maximumComplexity: 1000,
            variables: ctx.request.query.variables,
            estimators: [
                fieldConfigEstimator(),
                simpleEstimator({
                    defaultComplexity: 1
                })
            ]
        });
    }
}

interface IAppContext {
    request: express.Request;
    response: express.Response;
}

But as you can see I have no idea how to manually get a DocumentNode for the ValidationContext and I have no idea how to get a FieldNode and typeDef for the nodeComplexity function.

That being said I don't even know if this would be the right way to do this, maybe I'm missing something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions