polling-interval-too-short

Try in Playground
apollo-graphql-client-javascriptBest PracticeWarning

0

No tags

No CWE or CVE

Check the polling interval of a GraphQL query. If the polling interval is too short, it may then generate too many queries to the backend and overload the backend with too many queries at the same time. Instead, a frontend application should poll at most every 100 ms.

Ast Rule: function call


polling-interval-too-short

How to write a rule
function visit(node, filename, code) {
  if (node.functionName && node.functionName.value) {
    if (node.arguments && node.arguments.values && node.arguments.values.length == 2) {
      const secondArgument = node.arguments.values[1];
      if (secondArgument && secondArgument.value) {
        const secondArgumentValue = secondArgument.value;
        if (secondArgumentValue.astType === "object") {
          const pollIntervalValues = secondArgumentValue.elements.filter(e => e.name && e.name.value === "pollInterval");
          if (pollIntervalValues && pollIntervalValues.length > 0) {
            const pollIntervalValue = pollIntervalValues[0];
            if (pollIntervalValue.value && pollIntervalValue.value.astType === "string") {
              if (pollIntervalValue.value.value < 100) {
                const error = buildError(pollIntervalValue.start.line, pollIntervalValue.start.col,
                  pollIntervalValue.end.line, pollIntervalValue.end.col,
                  "Poll interval too short and risk of overloading the backend, put a value higher than 100ms", "WARNING", "BEST_PRACTICE");
								const edit = buildEditUpdate(pollIntervalValue.value.start.line, pollIntervalValue.value.start.col,
        																		 pollIntervalValue.value.end.line, pollIntervalValue.value.end.col, "100");
      					const fix = buildFix("change the polling interval to 100", [edit]);
      					addError(error.addFix(fix));
              }
            }

          }
        }
      }
    }
  }
}

large-poll-interval.js

Expected test result: no error

const { loading, error, data } = useQuery(CSV_IMPORT, {
		variables: { id: this.state.importId },
		pollInterval: 500,
	});

too-short.js

Expected test result: has error

const { loading, error, data } = useQuery(CSV_IMPORT, {
		variables: { id: this.state.importId },
		pollInterval: 50,
	});

no-poll-interval.js

Expected test result: no error

const { loading, error, data } = useQuery(CSV_IMPORT, {
		variables: { id: this.state.importId },
	});
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.