-
-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Closed
Description
Bug report
Required System information
- Node.js version: v16.15.1
- NPM version: 8.11.0
- Strapi version: v4.1.12
- Database: sqlite
- Operating system: Mac OS Monterey 12.1
Describe the bug
While I'm working on a Google Cloud Functions to manage the publication status of my content, I wanted to make it more automatic. I wrote a function that let me get all the content that have a publicationState: PREVIEW with a specific date, in my case today. I work with GraphQL on this project and I tried my queries on Postman to get a grasp of it.
What I tried to achieve is to update my publications to be updated from PREVIEW to LIVE without any human interaction. I avoid any plugin to do so as I can't run any cron on my app engine in production.
- When I run the query on Postman, I get the content I need, I have no problem.
- When I run the
CURLsnipper from postman on my terminal, I have no problem. - When I run the Node.js code on my production environment, I have no problem
- When I run the Node.js code on my local environment, I get an error.
Expected behavior
Be able to query http://localhost:1337/graphql on my node.js app locally.
Screenshots
Postman
Curl
curl --location --request POST 'http://localhost:1337/graphql' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"query getSnacks($date: Date) {\n snacks(publicationState: PREVIEW, filters: { publish_at: { eq: $date } }) {\n data {\n id\n }\n }\n }","variables":{"date":"2022-07-03"}}'
Node.js
https://jsonhero.io/j/5C1RgIvUu6TS
Code snippets
Function
'use strict';
const functions = require('@google-cloud/functions-framework');
const axios = require('axios');
require('dotenv').config()
functions.http('updatePublication', (req, res) => {
const today = new Date();
const dd = String(today.getDate()).padStart(2, "0");
const mm = String(today.getMonth() + 1).padStart(2, "0"); //January is 0!
const yyyy = today.getFullYear();
const getPublication = JSON.stringify({
query: `query getSnacks($date: Date) {
snacks(publicationState: PREVIEW, filters: { publish_at: { eq: $date } }) {
data {
id
}
}
}`,
variables: {"date":`${yyyy}-${mm}-${dd}`}
});
const configGetPublication = {
method: 'post',
url: `${process.env.GRAPHQL_API_URL}`,
headers: {
'Content-Type': 'application/json'
},
data : getPublication
};
const sendGetRequest = async () => {
try {
const resp = await axios(configGetPublication);
console.log(JSON.stringify(resp.data));
res.send(JSON.stringify(resp.data));
} catch (err) {
console.error(JSON.stringify(err));
res.send(JSON.stringify(err));
}
};
sendGetRequest();
});
Middleware
module.exports = [
"strapi::errors",
{
name: "strapi::security",
config: {
contentSecurityPolicy: {
useDefaults: true,
directives: {
"connect-src": ["'self'", "https:"],
"img-src": ["'self'", "data:", "blob:"],
"media-src": [
"'self'",
"data:",
"blob:",
],
upgradeInsecureRequests: null,
},
},
},
},
"strapi::cors",
"strapi::poweredBy",
"strapi::logger",
"strapi::query",
"strapi::body",
"strapi::session",
"strapi::favicon",
"strapi::public",
];
Metadata
Metadata
Assignees
Labels
No labels


