Querying basics
GraphQL structures data as a graph. GraphQL uses a schema to define the objects and their hierarchy in your data graph. You can explore the edges of the graph by using queries to get the needed data. These queries must respect the structure of the schema.
A node, followed by its fields, is at the core of a GraphQL query. A node is an object of a specific type; the type specifies the fields that make up the object.
A field can be another node where the appropriate query would contain nested elements. Some nodes look like functions that can take on arguments to limit the scope of what they can act on. You can apply filters at each node.
A typical query against the Cloudflare GraphQL schema is made up of four main components:
viewer
- is the root node,zones
oraccounts
- indicate the scope of the query, that is the domain area or account you want to query. Theviewer
can access onezones
oraccounts
, or both,- data node or dataset - represent the data you want to query.
zones
oraccounts
may contain one or more datasets. To find out more about discovering nodes, please refer to introspection, - fieldset - a set of fields or nested fields of the dataset.
The query to Cloudflare GraphQL API must be sent over HTTP POST request with payload in JSON format that consists of these fields:
From the above structure, the query
field must contain a GraphQL query
formatted as a single line string (meaning all newline symbols should be
stripped / escaped), when variables
is an object that contains all values of
used placeholders in the query itself.
In the following example, the GraphQL query fetches a datetime
, action
, and
client request HTTP host as host
field of 2 WAF events from zone-scoped
firewallEventsAdaptive
dataset.
In the query above, we have variable placeholders: $tag, $start, and $end. We
provide values for those placeholders alongside the query by placing them into
variables
field of the payload.
There are multiple ways to send your query to Cloudflare GraphQL API. You can use you favourite GraphQL client or CLI to send a request via curl. We have a how-to guide about using GraphiQL client, also check a guide on how to execute a query with a curl here.
As previously mentioned, a query might contain one or multiple nodes (datasets). At the API level, the data extraction would be done simultaneously, but the response would be delayed until all dataset queries got their results. If any fails during the execution, the entire query will be terminated, and the error will be returned.
Here are some helpful articles about working with the Cloudflare Analytics API and GraphQL.