Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

This project is aimed to provide actual query duration statistics from apollo-studio.

NotificationsYou must be signed in to change notification settings

Grishezz/apollo-studio-data-fetcher

Repository files navigation

What is this repo for?

This project is aimed to provide actual query duration statistics fromapollo-studio.

These timing metrics (AKA timing-hints) can then be utilized in a custom graphql complexity estimator and plugged into thegraphql-query-complexity library designed to"... protect your GraphQL servers against resource exhaustion and DoS attacks".

Usage

  • create aschema.graphql containing the AST version of your graphql schema inside thedata folder
  • create a.env file, based on.env.example
  • build withyarn
  • run withyarn run

What it does

  1. logins into apollo-studio using puppeteer and intercepts your auth cookies, and the timing metrics persistent query hash
  2. reads your schema fromschema.graphql, and runs a graphql timing metric query, via apollo-studio graphql api(using cookies and persistent query hash from [1])
  3. arranges the data into an object of the following structure:
{"Address.city":0.003780965612170304,"Address.country":0.0036712738793939926,"Address.entrance":0.0037941881736387904,...}
  1. stores this data in an aws s3 bucket

Note: you can (and it makes sense to) run the above logic periodically.

Example for a possible implementation of complexity-estimator using the timing metrics

In yourapollo-server implementation, add a plugin:

import{getComplexity,simpleEstimator}from'graphql-query-complexity';...classComplexityValidatorimplementsApolloServerPlugin<any>{    ...requestDidStart(requestContext:GraphQLRequestContext<any>):GraphQLRequestListener<any>|void{return{didResolveOperation({ request, document}){constquery=request.operationName                    ?separateOperations(document)[request.operationName]                    :document;constcomplexity=getComplexity({               schema,               query,variables:request.variables,estimators:[// where timingMetrics is the data generated by this (apollo-studio-data-fetcher) projecttimingBasedEstimator(timingMetrics),simpleEstimator({defaultComplexity:100})],});validateComplexity(...);}};}}

while the timingBasedEstimator implementation can be something along these lines:

import{ComplexityEstimatorArgs}from'graphql-query-complexity';import{get}from'lodash';/** * Complexity estimator based on actual query timing metrics fetched from apollo-studio. * The return unit is milliseconds-equivalent. * *@param timingMetrics */exportfunctiontimingBasedEstimator(timingMetrics){return(options:ComplexityEstimatorArgs)=>{consttimingMetricKey=`${options.type.name}.${options.field.name}`;consttimingMetricValue=timingMetrics[timingMetricKey];if(timingMetricValue){// Your actual calculation might be different, using such a multiplier, is just an examle.constmultiplier=Math.max(get(options,'args.first',1),get(options,'args.last',1));return(timingMetricValue+options.childComplexity)*multiplier;}returnundefined;// Fallback to simpleEstimator.};}

TODO:

  • add some tests
  • support mutation / interfaces / unions etc.

About

This project is aimed to provide actual query duration statistics from apollo-studio.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp