Using a Data Source
Once you have created some data sources, you can use them to enrich the
data structures returned by the Cook's content retrieval queries. To do
this you use one of the content retrieval functions called
datasource()
or
extendedDatasource()
. If you go back to editing the
section page content retrieval query
(index-page.graphql
) at
http://localhost:8101/tomorrow-online/edit
, place
your cursor immediately above the headerMenu
entry
and press Ctrl-Space
, then you will see that the
displayed list includes a datasource
option. Select
it and add a name
parameter, specifying the name of
the data source you created:
datasource(name: "politicalContent")
(You don't actually need to place the data source call before the
headerMenu
entry, but by default it does have to be a
top-level entry in the query, at the same level as
resolution
, context
,
menu
and so on. You can, however, configure the Cook
to allow the use of the datasource()
function in
different locations, see
Changing The Data Source Context.)
The datasource
function returns
AtomEntry
objects that contain information about each
content item found by the data source query. One of the
AtomEntry
object's fields is
__typename
, which means that you can test the
returned content items' type using the same ... on
content-type technique used to test
the context object:
datasource(name: "politicalContent") { ... on Story { displayId fields { title } } }
Once you have determined the types of the returned content items in this way, you have access to all of their content and relations in exactly the same way as for content items retrieved directly from the Content Store.
You can optionally prefix the datasource
function
with a descriptive field name to make the output structure easier to
navigate:
politicalContent: datasource(name: "politicalContent") { ... on Story { displayId fields { title } } }
Executing the query now will produce the same output as before, but with
an additional politicalContent
field containing the
selected information about the content items returned by the datasource:
... "politicalContent": [ { "displayId": "29", "fields": { "title": "Cameron promises 'seven-day NHS'" } }, { "displayId": "26", "fields": { "title": "'£260m cost' if line not electrified" } }, ..etc... ] ...