This may be a silly question, but how do I load a script to a store and then execute code onload? Something like the below if I were to add a script manually to the head of an html page:
Currently I am loading the script as follows:
// server.js
server.use(
createShopifyAuth({
afterAuth(ctx) {
...
const {shop, accessToken} = ctx.state.shopify
const mutation = JSON.stringify({
mutation: `mutation scriptTagCreate($input: ScriptTagInput!) {
scriptTagCreate(input: $input) {
scriptTag {
id
}
userErrors {
field
message
}
}
}`,
variables: {
input: {
cache: true,
displayScope: 'ONLINE_STORE',
src: 'route_to_my_script'
}
}
})
fetch(`https://${shop}/admin/api/2021-04/graphql.json`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': accessToken
},
body: mutation
})
.then(console.log)
.catch(console.error)
}
})
)
So, where do I provide an option to call the my_init function once the script has been loaded?