Tagging GraphQL Responses
Let's send the following GraphQL query to our SpaceX demo service and see how this is handled by GraphCDN.
query {
launchesPast(limit: 2) {
id
mission_name
launch_date_utc
}
}
Internally GraphCDN extends the query being sent to your backend by asking for __typename
fields for any types you have included in your query. The query on your backend would look like the following:
query {
launchesPast(limit: 2) {
__typename
id
mission_name
launch_date_utc
}
}
The response to the above query would look like the following JSON document. (Your response might show different values, depending on when you run the query, but the structure will be the same.)
{
"data": {
"launchesPast": [
{
"__typename": "Launch",
"id": "109",
"mission_name": "Starlink-15 (v1.0)",
"launch_date_utc": "2020-10-24T15:31:00.000Z"
},
{
"__typename": "Launch",
"id": "108",
"mission_name": "Sentinel-6 Michael Freilich",
"launch_date_utc": "2020-11-21T17:17:00.000Z"
}
]
}
}
GraphCDN then takes a look at the response and figure out which types and instances of those types are included. This information informs which tags are added to the response when it is stored in the cache.
Based on the above query, those tags will look like the following:
The actual format that GraphCDN uses is slightly different; it is shown here in a simplified state. GraphCDN includes additional required metadata for each tag, including the internal service ID.
app:XYZ
: an internal ID to identify records of a single GraphCDN servicequery:launchesPast
: the root field usedtype:Launch
: the type (or types) returned by the querykey:Launch:id:109
the combination of type, key field, and value of that key field for a specific instance returned by the querykey:Launch:id:108
Key Fields
If you have additional key fields defined for your types, GraphCDN tags documents with tags referencing those additional key fields as well.
For example, if the Launch
type referenced above also has a field called launch_name
that uniquely identifies a single launch, you could add this field to your list of key fields. Any query that fetches an entity of type Launch
and also requests the launch_name
key field is then also tagged with the appropriate tag:
key:Launch:launch_name:Buttercup Moon
By default GraphCDN treats any id
, _id
and key
fields on your types as key fields and tags responses with those. You can however configure this according to your requirements.
Nested Types
Similarly, if we extend our original query and ask for additional information available in nested types, GraphCDN would add tags for those types as well. Asking for more information about the rocket, for example,
query {
launchesPast(limit: 2) {
__typename
id
mission_name
launch_date_utc
rocket {
__typename
rocket {
__typename
id
}
}
}
}
would return this response
{
"data": {
"launchesPast": [
{
"__typename": "Launch",
"id": "109",
"mission_name": "Starlink-15 (v1.0)",
"launch_date_utc": "2020-10-24T15:31:00.000Z",
"rocket": {
"__typename": "LaunchRocket",
"rocket": {
"__typename": "Rocket",
"id": "falcon9"
}
}
},
{
"__typename": "Launch",
"id": "108",
"mission_name": "Sentinel-6 Michael Freilich",
"launch_date_utc": "2020-11-21T17:17:00.000Z",
"rocket": {
"__typename": "LaunchRocket",
"rocket": {
"__typename": "Rocket",
"id": "falcon9"
}
}
}
]
}
}
and would store it in GraphCDNs edge cached tagged with the following tags:
app:XYZ
query:launchesPast
type:Launch
type:LaunchRocket
type:Rocket
key:Launch:id:109
key:Launch:id:108
key:Rocket:falcon9
Updated 3 months ago