-
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Vector's GraphQL API implements the GraphQL Cursor Connections Specification, to aid cursoring and pagination through potentially large datasets. As an example, consider this Vector config: [api]
enabled = true
[sources.one]
type = "generator"
format = "json"
[sources.two]
type = "generator"
format = "json"
[sources.three]
type = "generator"
format = "json"
[sinks.blackhole]
type = "blackhole"
inputs = ["one", "two", "three"] There are 3x sources, each feeding into a blackhole sink. If we wanted to return all the sources in this config, along with the name of the sink(s) they feed into, we could issue this query: {
sources {
pageInfo {
startCursor
endCursor
hasNextPage
hasPreviousPage
}
totalCount
edges {
node {
componentId
sinks {
componentId
}
}
}
}
} Which would return: {
"data": {
"sources": {
"edges": [
{
"node": {
"componentId": "one",
"sinks": [
{
"componentId": "blackhole"
}
]
}
},
{
"node": {
"componentId": "two",
"sinks": [
{
"componentId": "blackhole"
}
]
}
},
{
"node": {
"componentId": "three",
"sinks": [
{
"componentId": "blackhole"
}
]
}
}
],
"pageInfo": {
"endCursor": "Q3Vyc29yOjI",
"hasNextPage": false,
"hasPreviousPage": false,
"startCursor": "Q3Vyc29yOjA"
},
"totalCount": 3
}
}
} A few notes on some of these fields:
Both Now let's assume we only want the first source. We can add a {
sources(first:1) {
pageInfo {
startCursor
endCursor
hasNextPage
hasPreviousPage
}
totalCount
edges {
node {
componentId
sinks {
componentId
}
}
}
}
} Which will now return: {
"data": {
"sources": {
"edges": [
{
"node": {
"componentId": "one",
"sinks": [
{
"componentId": "blackhole"
}
]
}
}
],
"pageInfo": {
"endCursor": "Q3Vyc29yOjA",
"hasNextPage": true,
"hasPreviousPage": false,
"startCursor": "Q3Vyc29yOjA"
},
"totalCount": 3
}
}
} This is pretty much the same thing, but notice a few differences:
This time, let's use that {
sources(after:"Q3Vyc29yOjA") {
pageInfo {
startCursor
endCursor
hasNextPage
hasPreviousPage
}
totalCount
edges {
node {
componentId
sinks {
componentId
}
}
}
}
} Returning: {
"data": {
"sources": {
"edges": [
{
"node": {
"componentId": "two",
"sinks": [
{
"componentId": "blackhole"
}
]
}
},
{
"node": {
"componentId": "three",
"sinks": [
{
"componentId": "blackhole"
}
]
}
}
],
"pageInfo": {
"endCursor": "Q3Vyc29yOjI",
"hasNextPage": false,
"hasPreviousPage": true,
"startCursor": "Q3Vyc29yOjE"
},
"totalCount": 3
}
}
} Because we dropped the These options can be combined -- for example, skip the first result, and only return the last result of what remains: {
sources(after:"Q3Vyc29yOjA", last:1) {
pageInfo {
startCursor
endCursor
hasNextPage
hasPreviousPage
}
totalCount
edges {
node {
componentId
sinks {
componentId
}
}
}
}
} Which now gives us just the third (and final) source: {
"data": {
"sources": {
"edges": [
{
"node": {
"componentId": "three",
"sinks": [
{
"componentId": "blackhole"
}
]
}
}
],
"pageInfo": {
"endCursor": "Q3Vyc29yOjI",
"hasNextPage": false,
"hasPreviousPage": true,
"startCursor": "Q3Vyc29yOjI"
},
"totalCount": 3
}
}
} Now All components implement cursoring in the same way - including You may notice that component fields on Technically, there's nothing stopping us from implementing the cursor spec on all levels -- so you could, for example, retrieve the first 5 sources, and the first 2 sinks that each of them implement. We chose not to do, to keep the surface area simpler. Generally, users that have narrowed down their 'root' component scope and are querying, say, sources that feed into a sink, will want to know all sources that are going in. But this is something we may revisit in the future if there's a need for it. Finally, I'll leave you with one other scenario -- this allows you to query both sinks and sources, and filter against all of them: {
components(filter:{componentId:{contains:"o"}}) {
edges {
node {
...on Source {
componentId
sinks {
componentId
}
}
...on Sink {
componentId
}
}
}
}
} Which returns: {
"data": {
"components": {
"edges": [
{
"node": {
"componentId": "blackhole"
}
},
{
"node": {
"componentId": "one",
"sinks": [
{
"componentId": "blackhole"
}
]
}
},
{
"node": {
"componentId": "two",
"sinks": [
{
"componentId": "blackhole"
}
]
}
}
]
}
}
} A few points to make here:
Vector's GraphQL API is intended to be flexible for a range of use-cases. It's also used to power vector top (for component metrics) and vector tap (for event lines) - two of our built-in CLI tools. I'd encourage you to click "Docs" on the right side of the playground and explore the API further to get a better idea of what it's capable of. Let me know if you have any further questions or a particular use-case in mind, and I'd be happy to dive in further! |
Beta Was this translation helpful? Give feedback.
-
Regarding the difference in the publicly deployed playground vs. what you see locally: Cursoring and pagination is one of the more recent features added to our GraphQL API, so I suspect you're seeing an older version where this wasn't yet implemented. It's a little more boilerplate to issue an equivalent query, but it's also more flexible for limiting results and having the ability to search/filter components, so it's definitely the more useful of the two schemas. |
Beta Was this translation helpful? Give feedback.
-
@leebenson thanks so much for the detailed explanation. So far I only spent around 2 hours on learning GraphQL in order to use this API. I will look into BTW, I think we could include such information in the API documentation so that people can understand how this API can be used. I did some web searching previously, here are the two major sources of information I can find:
|
Beta Was this translation helpful? Give feedback.
Vector's GraphQL API implements the GraphQL Cursor Connections Specification, to aid cursoring and pagination through potentially large datasets.
As an example, consider this Vector config:
There are 3x sources, each feeding into a blackhole sink.
If we wanted to return all the sources in this config, along with the name of the sink(s) they feed into, we could issue this query: