Datalog Cheat Sheet

Grafema uses Datalog queries to search the code graph. This cheat sheet explains the basics and provides copy-paste queries for common tasks.

Quick Syntax Reference

violation(X) :- condition1, condition2, ...

This reads as: “X is a violation if condition1 AND condition2 AND … are all true.”

SyntaxMeaning
node(X, "TYPE")X is a node of type TYPE
edge(X, Y, "TYPE")There’s an edge of type TYPE from X to Y
attr(X, "name", Value)Node X has attribute “name” with value Value
\+NOT (negation)
,AND
;OR

Common Queries

Find Unresolved Calls

Problem: Which function calls couldn’t be traced to their definitions?

violation(X) :- node(X, "CALL"), \+ edge(X, _, "CALLS").

Translation: X is a violation if X is a CALL node and there is NO CALLS edge from X to anything.

Find Unresolved Method Calls

violation(X) :- node(X, "METHOD_CALL"), \+ edge(X, _, "CALLS").

Find All HTTP Routes

violation(X) :- node(X, "http:route").

Note: “violation” is just the output variable name. It doesn’t mean something is wrong.

Find eval() Usage

Security: Detect dangerous dynamic code execution.

violation(X) :- node(X, "CALL"), attr(X, "name", "eval").

Find new Function() Usage

violation(X) :- node(X, "CALL"), attr(X, "name", "Function").

Find console.log Calls

violation(X) :- node(X, "CALL"), attr(X, "object", "console"), attr(X, "method", "log").

Find Database Queries

violation(X) :- node(X, "db:query").

Find HTTP Client Requests

violation(X) :- node(X, "http:request").

Find External Dependencies

violation(X) :- node(X, "MODULE"), attr(X, "external", "true").

Find Functions in a Specific File

violation(X) :- node(X, "FUNCTION"), attr(X, "file", "/path/to/file.js").

Find All Functions Called by a Specific Function

violation(Y) :-
  node(X, "FUNCTION"),
  attr(X, "name", "myFunction"),
  edge(X, C, "CONTAINS"),
  node(C, "CALL"),
  edge(C, Y, "CALLS").

Find Unused Functions (No Incoming CALLS)

violation(X) :- node(X, "FUNCTION"), \+ edge(_, X, "CALLS").

Warning: May include entry points and event handlers.

Find Files with Most Unresolved Calls

violation(F) :- node(C, "CALL"), attr(C, "file", F), \+ edge(C, _, "CALLS").

Groups results by file path.

Combining Conditions

AND (comma)

Find functions that are both exported AND have no callers:

violation(X) :-
  node(X, "FUNCTION"),
  attr(X, "exported", "true"),
  \+ edge(_, X, "CALLS").

OR (semicolon)

Find either eval or Function calls:

violation(X) :-
  node(X, "CALL"),
  (attr(X, "name", "eval") ; attr(X, "name", "Function")).

Understanding Results

Query results return node IDs. Use npx @grafema/cli node <id> to see full node details:

# Run query
npx @grafema/cli query 'violation(X) :- node(X, "CALL"), attr(X, "name", "eval").'

# Output: CALL:src/utils.js:42:eval
# Get details
npx @grafema/cli node "CALL:src/utils.js:42:eval"

Tips

  1. Start simple — Begin with single conditions, then add more
  2. Use negation carefully\+ can be slow on large graphs
  3. Check file paths — Use relative paths from project root
  4. Quote strings — All string values must be in double quotes

Common Node Types

TypeDescription
MODULEA JavaScript/TypeScript file
FUNCTIONFunction declaration or expression
CLASSClass declaration
METHODMethod in a class
VARIABLEVariable declaration
CALLFunction call
METHOD_CALLMethod call (obj.method())
http:routeHTTP endpoint (Express, etc.)
http:requestHTTP client request (fetch, axios)
db:queryDatabase query

Common Edge Types

TypeMeaning
CONTAINSParent contains child (module contains function)
CALLSFunction call resolves to target
DEPENDS_ONModule imports another module
ASSIGNED_FROMVariable gets value from expression
INTERACTS_WITHHTTP request connects to route

See Also