Search code examples
crongraphqlscheduled-tasks

Call a public GraphQL API once a month


I would like to make a monthly call to a public GraphQL API with some minor business logic.

I read about Posthook but it is unclear how I can use it to call a GraphQL API. Posthook appears to only support REST.

What is an easy and reliable way to do this? Would it make sense to use an AWS Lambda or is there a simpler way?


Solution

  • GraphQL services typically interact with clients over HTTP with POST requests. Clients send JSON in the request and receive JSON in the response.

    Some use cases benefit from a GraphQL client library like Apollo Client, but plain old cURL works fine:

    # request
    curl --location --request POST 'https://swapi-graphql.netlify.app/.netlify/functions/index' \
    --header 'Content-Type: application/json' \
    --data-raw '{"query":"query Query($filmId: ID) {\n  film(filmID: $filmId) {\n    title\n }\n}","variables":{"filmId":1}}'
    
    # response
    {"data":{"film":{"title":"A New Hope"}}}
    

    An AWS Lambda (AWS's serverless function service) would be a fine way to implement a scheduled GraphQL api call and apply business logic. The Lambda service integrates well with cron-scheduled triggering and results notification. It's easy to get started. You can set up the service by pointing-and-clicking in the AWS console. Or use a infrastructure-as-code library like AWS's SAM (i.e. define your infra in YAML) or CDK (i.e. define your infra in JS/Python/etc). Or in a squillion more ways.

    The other cloud providers have similar offerings, take your pick.