How to paging with GraphQL resolvers for Azure Cosmos DB in Azure API Management

sn796 25 Reputation points
2023-07-02T05:38:56.27+00:00

I am evaluating the Cosmos DB data source for a GraphQL API in Azure API Management.

To perform pagination evaluation, I'm trying to add a paging section to the resolver, but I'm unsure how to retrieve the value of continuation-token from the request.

Here is my configuration (attempting to retrieve from the continuationToken argument):

Schema

type Query {
  getUsers(continuationToken: String): UsersResult!
}

type User {
  id: ID!
  name: String!
  age: Int!
}

type UsersResult {
  items: [User!]!
  hasNextPage: Boolean!
  endCursor: String
}

Resolver

<cosmosdb-data-source>
	<connection-info>
		<connection-string use-managed-identity="true">
            AccountEndpoint=https://my-cosmos-db-name.documents.azure.com:443/;
        </connection-string>
		<database-name>demo-db</database-name>
		<container-name>users</container-name>
	</connection-info>
	<query-request>
		<sql-statement>
            SELECT * FROM c
        </sql-statement>
		<paging>
			<max-item-count>2</max-item-count>
			<continuation-token>
                @(context.GraphQL.Arguments["continuationToken"])
            </continuation-token>
		</paging>
	</query-request>
</cosmosdb-data-source>

When I execute the evaluation with the above configuration, I receive the following response. Is there an error in the specified method?

{
  "statusCode": 400,
  "message": "Resolvers are not defined and a service URL is not configured",
  "activityId": "5e3272d6-1741-4ac8-9fa1-718709c87920"
}

By the way, it worked correctly when I specified a fixed value like this:

<continuation-token>
    W3sidG9rZW4iOiItUklEOn5aejhHQU5aQzI0c0NBQUFBQUFBQUFBPT0jUlQ6MSNUUkM6MiNJU1Y6MiNJRU86NjU1NjcjUUNGOjgiLCJyYW5nZSI6eyJtaW4iOiIiLCJtYXgiOiJGRiJ9fV0=
</continuation-token>

Also, the documentation states that the continuation-token property can have a template attribute.

https://learn.microsoft.com/en-gb/azure/api-management/cosmosdb-data-source-policy

        <paging> 
            <max-item-count template="liquid" > 
                Maximum number of items returned by the query
            </max-item-count> 
            <continuation-token template="liquid"> 
                Continuation token for paging 
            </continuation-token> 
        </paging>

However, when I actually specify the template attribute, I receive the following error message and cannot save it:

Error in element 'cosmosdb-data-source' on line 1, column 2: The 'template' attribute is not declared.
  • resource
    • sku: Consumption, Development
    • location: Japan East
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,447 questions
{count} votes

1 answer

Sort by: Most helpful
  1. sn796 25 Reputation points
    2023-08-19T09:19:05.48+00:00

    @navba-MSFT

    Hi.

    I would like to inform you that the resolver was changed as follows and it operated as expected.

    <continuation-token>
      @(context.GraphQL.Arguments["continuationToken"])
    </continuation-token>
    

    <continuation-token>
      @(context.GraphQL.Arguments["continuationToken"].ToString())
    </continuation-token>
    

    I added a call to ToString().

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.