how to pass date variable to graphQL call in svelte?

how to pass date variable to graphQL call in svelte?


1

I have a graphql file with a date in ISO format. I would like to pass a variable instead of hard-coding the date. I would like to use Date.toISOstring() or some get current date method.

GRAPHQL FILE

let today = Date.toISOString() //or DateNow()

query guide {
        tv {
            guide(date: "2022-08-10T00:00:00Z <-replace--${today}") {
                entries {
                    channel {
                        show{
                            ......
                    }

                }
            }
        }
    }
}

Is this possible?

1 Answer
1


3

Use a GraphQL variable and pass it to your query. Here are the adjustment you have to make to the query. I am guessing the name of the date scalar here (DateTime), it might as well simply be String. Check the documentation of the API to get the correct name.

query guide($date: DateTime!) {
    tv {
        guide(date: $date) {
            entries {
                channel {
                    show{
                        ......
                    }
                }
            }
        }
    }
}

If you use Svelte Apollo for example, you can pass the variables like this:

const guide = query(GUIDE_QUERY, {
  variables: { date: new Date().toIsoString() },
});

3

  • what if I use urql instead of apollo? I am unsure where to put the second part..

    – lache

    Aug 11, 2022 at 13:51

  • Their documentation is not that long… formidable.com/open-source/urql/docs/basics/svelte/#variables

    – Herku

    Aug 15, 2022 at 9:44

  • thanks! i figured it out i was just struggling with finding where they put the query

    – lache

    Aug 19, 2022 at 15:21



Leave a Reply

Your email address will not be published. Required fields are marked *