Skip to content

Commit fd7d47b

Browse files
committed
PCX-1086 Edits for building a query in graphql
1 parent 9bba90e commit fd7d47b

12 files changed

+73
-35
lines changed

products/analytics/src/content/graphql-api/getting-started/compose-graphql-query.md

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,61 +5,96 @@ order: 50
55

66
# Create a query in a GraphQL client
77

8-
You can use a GraphQL client to build and execute queries to the GraphQL API endpoint. The example below uses the GraphiQL client.
8+
You can use a GraphQL client to build and execute queries to the GraphQL API endpoint. The example below uses the [GraphiQL](https://github.com/graphql/graphiql/tree/main/packages/graphiql#readme) client.
99

10-
Before you begin, configure the API endpoint and HTTP headers in the GraphQL client.
10+
## Prerequisites
1111

12-
Click on the editing pane of GraphiQL and add the following base query:
12+
This article assumes that you are already familiar with [_Querying basics_](/graphql-api/getting-started/querying-basics).
1313

14-
```json
15-
query {
16-
viewer {
17-
zones(filter: { zoneTag: "{zone-id}"}) {
14+
Before you begin, [configure the API endpoint and HTTP headers](/graphql-api/getting-started/authentication/graphql-client-headers) in the GraphiQL client.
1815

19-
}
20-
}
21-
}
22-
```
16+
<Aside type='tip' header='Tip'>
17+
18+
To explore the documentation for the data sets and fields in the Cloudflare GraphQL schema, click **Docs** to open the _Documentation Explorer_ pane.
19+
20+
For an introduction, see [Explore the GraphQL schema](/graphql-api/getting-started/explore-graphql-schema).
21+
22+
</Aside>
23+
24+
## Set up a query and choose a data set
25+
26+
Click on the editing pane of GraphiQL and add this base query, replacing `zone-id` with your Cloudflare zone ID:
2327

24-
To explore the documentation for the nodes and fields in the Cloudflare GraphQL schema, click **Docs** to open the _Documentation Explorer_ pane.
28+
![GraphiQL base query](../../static/images/graphiql-base-query.png)
2529

26-
The GraphiQL client has auto completion, to assist query building. Place your cursor on the line under "zones" and start typing a word: a popup window appears, listing all the possible fields that contain that word. For example, if you type `firewall`, the popup displays the fields that return firewall information:
30+
<Aside type='tip' header='Tip'>
2731

28-
![firewall nodes](../../static/images/create-query-fw-data-set.png)
32+
To find the ID for a zone, log in to your Cloudflare account and click the site for which you want to obtain the zone ID. In the Cloudflare dashboard **Overview** page, scroll to the **API** section in the right sidebar, which displays your zone ID and account ID.
33+
34+
</Aside>
35+
36+
To assist query building, the GraphiQL client has word completion. Place your cursor on the line below `zones` and start entering a value—a popup menu displays. For example, when you type `firewall`, the popup displays the data sets that return firewall information:
37+
38+
![firewall nodes](../../static/images/graphiql-word-completion.png)
2939

3040
The text at the bottom of the list displays a short description of the data that the node returns.
3141

32-
Select the field you want to use and insert it into your query: either click the item in the list, or scroll using arrow keys and hit return.
42+
Select the data set you want to query and insert it—either click the item in the list, or scroll using arrow keys and press <kbd>Return</kbd>. This example uses the `firewallEventsAdaptive` data set.
43+
44+
## Supply required parameters
45+
46+
Hover your mouse over a field to display a tooltip that describes the data set. In this example, hovering over the `firewallEventsAdaptive` node displays this description:
47+
48+
![GraphiQL parameters](../../static/images/graphiql-set-up-base-query.png)
3349

34-
Hover your mouse over a field to open a popup window showing the documentation for that field. The following popup window appears when you hover over the `firewallEventsAdaptive` field:
50+
To display information about the data set, including required parameters, click the data set name (blue text). The **Documentation Explorer** opens and displays details about the data set:
3551

36-
![Autocomplete popup](../../static/images/create-query-fw-data-set-description.png)
52+
![GraphiQL parameters](../../static/images/graphiql-parameters.png)
3753

38-
Click on the node name (blue text) to display information about the field, including the required parameters, in the _Documentation Explorer_ pane.
54+
Note that the `filter` and `limit` arguments are required, as indicated by the exclamation mark (`!`) after their type definitions (gold text). In this example, the `orderBy` argument is not required, though when used it requires a value of type `ZoneFirewallEventsAdaptiveOrderBy`.
3955

40-
Click on the type definition (gold text) to display information about the field type in the _Documentation Explorer_ pane.
56+
To browse a list of supported filter fields, click the filter type definition (gold text) in the Documentation Explorer. In this example the type is `ZoneFirewallEventsAdaptiveFilter_InputObject`:
4157

42-
Add the required parameters for the field in the editing pane: type the open parenthesis character, `(`, after the field name. GraphiQL inserts the closing parenthesis and opens a popup window with a list of required parameters:
58+
![GraphiQL filter fields](../../static/images/graphiql-filter-fields.png)
4359

44-
![GraphiQL filter popup](../../static/images/create-query-fw-data-set-filter.png)
60+
This example query shows the required `filter` and `limit` arguments for `firewallEventsAdaptive`:
4561

46-
Configure the required parameters.
62+
![GraphiQL filter values](../../static/images/graphiql-filter-values.png)
4763

48-
To add the data fields that you want to display, type the opening bracket `{` after the closing parenthesis containing parameters. GraphiQL automatically inserts the closing bracket. For readability, hit the return key between the brackets.
64+
## Define the fields to return with your query
4965

50-
Click the field type definition to display the list of fields in in the _Documentation Explorer_ pane:
66+
To browse the fields you can return with your query, hover your cursor over the dataset name in your query, and in the tooltip that displays, click the data type definition (gold text):
5167

52-
![List of fields](../../static/images/create-query-fw-data-set-field-type.png)
68+
![GraphiQL parameters](../../static/images/graphiql-set-up-base-query.png)
5369

54-
Start typing the name of a field that you want to fetch, to open an auto-complete list. Once you have entered all the fields you want, click the **Play** button to submit the query. The response pane contains the data fetched from the Cloudflare GraphQL API endpoint.
70+
**The Documentation Explorer** opens and displays a list of fields:
5571

56-
The GraphiQL client allows you to create variables to use in the query.
57-
In the following example, the _Query variables_ pane contains a variable that represents a zone ID:
72+
![List of fields](../../static/images/graphiql-return-fields.png)
73+
74+
To add the data fields that you want to display, type an opening brace (`{`) after the closing parenthesis for the parameters, then start typing the name of a field that you want to fetch. Use word completion to choose a field.
75+
76+
This example query returns the `action`, `datetime`, `clientRequestHTTPHost`, and `userAgent` fields:
77+
78+
![Example query with return fields](../../static/images/graphiql-query-return-field-values.png)
79+
80+
Once you have entered all the fields you want your query to return, you can click the **Play** button to submit the query. The response pane contains the data fetched from the Cloudflare GraphQL API endpoint.
81+
82+
![GraphiQL response](../../static/images/create-query-fw-data-set-play.png)
83+
84+
## Variable substitution
85+
86+
The GraphiQL client allows you to create variables for use in your queries.
87+
88+
To add a query variable, click the **Query Variables** pane and enter a JSON object that defines your variable.
89+
90+
This example JSON object defines a query variable for a zone ID:
5891

5992
```json
6093
{"zoneTag":"xxxxxxxxx"}
6194
```
6295

63-
Use `$zoneTag` to refer to the variable in a query.
96+
To use a variable in your query, prepend the `$` character to your variable name and use it to replace the desired value. When using a variable as a value in a query, do not wrap the variable in quotes as you would for a literal value.
6497

65-
![GraphiQL response](../../static/images/create-query-fw-data-set-play.png)
98+
This example query uses the zoneTag query variable to represent the zone ID:
99+
100+
![GraphiQL query variables](../../static/images/graphiql-query-variables.png)

products/analytics/src/content/graphql-api/getting-started/execute-graphql-query.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@ title: Use curl to query the GraphQL API
33
order: 60
44
---
55

6-
# Use CURL to query the GraphQL API
6+
# Use curl to query the GraphQL API
77

88
You can submit a query you built up in the GraphiQL client as a payload in the `data` field of a POST request to the GraphQL API.
99

10-
The advantage of executing the request as a CURL request is that you can redirect the response to a file, and execute other post processing methods.
10+
The advantage of executing the request with [curl](https://curl.se/) is that you can redirect the response to a file, and execute other post processing methods.
1111

12-
The GraphQL endpoint requires valid JSON, so you must pass the query as the _value_ part of a JSON _key/value_ pair with a key named "query".
12+
The GraphQL endpoint requires valid JSON, so you must pass the query as the _value_ part of a JSON _key:value_ pair with a key named "query".
1313

1414
Pass the list of variables in another JSON _key:value_ pair with a key named "variables".
1515

1616
The script below returns the firewall events in one zone over the last 24 hours:
1717

18-
```
18+
```bash
19+
---
20+
header: Example bash script that uses curl to query Analytics API
21+
---
1922
#!/bin/bash
2023
#
2124
# This script fetches the last 24 hours of firewall events for the ZoneID passed

products/analytics/src/content/graphql-api/getting-started/explore-graphql-schema.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ A list of **search nodes** displays, with a brief description of their behavior
6565

6666
![Search nodes](../../static/images/docs-fw-data-set.png)
6767

68-
Refer to [_Data Sets (tables)_](/graphql-api/features/data-sets) for details on nomenclature and behavior of these nodes.
68+
Refer to [_Data Sets (tables)_](/graphql-api/features/data-sets) for details on the nomenclature and behavior of these nodes.
6969

7070
To view the fields available for a particular data set, click on its type definition. If you click on the **ZoneFirewallEventsAdaptive** type definition in the **firewallEventsAdaptive** node, a list of fields is displayed:
7171

20.1 KB
Loading
55.3 KB
Loading
34.4 KB
Loading
31.3 KB
Loading
44 KB
Loading
49.8 KB
Loading
52.7 KB
Loading

0 commit comments

Comments
 (0)