Is there any way to get content type's data (dropdown data)? - contentful

I'm working on contentful to create blog posts.
I have created a field named with category with dropdown data, like the below image.
I have created more blogs for each categories (Ex: game has 5 blogs, Tour has 10 blogs and etc).
I want to show the list of all categories with content counts,
Is there any possible to get all of the categories with content count? ( I can get it by getting all blogs using this query
const res = ContentfulService.getEntries({ content_type: 'blog'})
then I grouped with category, but to get the category only, I don't want to get all of the blogs.)
Please let me know if there is a solution.
Thanks

The only way to do this through the API would be to make a request for each category and look at the total property of the response and that would be less efficient than what you're already suggesting.
https://cdn.contentful.com/spaces/{space_id}/environments/{environment_id}/entries?access_token={access_token}&content_type={content_type}&fields.category[in]={categoryValue}

I have achieved that using graphQL,
const query =
{
categoryCollection
{
items{
linkedFrom {
blogCollection {
total
}
}
name:category
sys {
id
}
}
}
}
let categories = await fetch(https://graphql.contentful.com/content/v1/spaces/${spaceId}, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
// Authenticate the request
Authorization: Bearer ${accessToken},
},
// send the GraphQL query
body: JSON.stringify({ query }),
})

Related

How to find accounts with similar profile picture in twitter?

I am basically analyzing Twitter search results i.e activities on tweets (like, retweet, replies) to abstract unique interactors/users and comparing their profile picture with the expected PFP image. If the user profile image matches the comparison threshold, It is added to a collection.
Twitter search:
const searchResult: any = await axios.get(twitterBaseUrl + `/tweets/search/recent`, {
params: {
'query': searchKey,
'max_results': '10',
'tweet.fields': 'author_id,public_metrics,conversation_id'
},
headers: {
'Authorization': 'Bearer ' + twitterBearerToken,
}
});
find unique users interacting on tweets:
const uniqueUsers: any = [...new Map(users.map((user: any) => [user["id"], user])).values()];
Compare images:
for (const user of uniqueUsers) {
const compatibility = await commons.twitterService.compareImage(
pfpImage,
user.profile_image_url.replace('_normal', '')
);
// Compatibility threshold = 90%
if (compatibility > 90) {
usersWithDBPFP.push(user)
}
}
The problem with this approach is, it is hitting API rate limits and the accumulated result is way less than expected also I don't think this is a scalable solution.
In the meantime, I have found this awesome tool nftinspect which was quickly picking up and collecting Twitter profiles, I want to achieve a similar result, yet not clear in which direction I should go. Can you guys help me with this or suggest me any alternative ways, if you are comfortable? I would really appreciate that. Thank you :)

How to fetch all variants of a product from shopify graphql api?

I want to fetch all variants of a product suing shopify graphql api, but it seems like we have to define the number of variants or any other thing we want by using first argument after the query. I want to get all variants of the product instead of defining a number of variants, how can this be possible?
const myproducts = await axios({
url: `https://${req.session.shop}/admin/api/2022-01/graphql.json`,
method: 'post',
headers: {
"X-Shopify-Access-Token": req.session.token,
},
data: {
query: `
{
product(id: "gid://shopify/Product/6829796196490") {
title
variants(first: 5) {
edges {
cursor
node {
selectedOptions {
name
value
}
media(first: 5) {
edges {
node {
alt
mediaContentType
status
__typename
... on MediaImage {
id
preview {
image {
originalSrc
}
}
__typename
}
}
}
}
}
}
pageInfo {
hasNextPage
}
}
}
}
`,
}
});
A product can't have more than 100 variants, that is a set limit by Shopify.
So in order to get ALL variants you just have to say first: 100 as long as your request cost doesn't exceed the allowed bucket cost for the query you will get them.
As for what you are asking, there is no way to get all variants without using first or last, this is a requirement when you are trying to get an array of edges.

Salesforce Rest API - Inserting values into Lookup fields

Inserting 200 records at a time (JSON array) into a custom object using the Salesforce REST API. Example with one record:
{
"records": [
{
"attributes" : {"type" : "Timecard__c"},
"Project__c": "a9S1F0000004LHDUA2",
"Milestone__c": "a9F1F00000007GOUAY",
"Resource__c": "0031F00000TApKqQAL",
"Date__c": "2020-08-16",
"Hours__c": 7,
"Notes__c": "Did some work"
},
]
}
The first three fields are lookups to other objects. The data I'm given to insert has names for the lookup fields (eg Project__c = "Canoe reconstruction", Milestone__c = "Rebuild gunwales", Resource__c = "John Smith".
My current plan is to generate arrays of Projects, Milestones, and Resources containing the Ids and Names then patch the JSON I have to load.
Does the Salesforce REST API offer a way to set the values of the Lookups to the text name such that it would find the Id on its own or is my current approach the most efficient way to handle this?
Here's the code I'm using for the processed data load...
const submitTimecards = async() => {
const token = await getAccessToken()
const data = JSON.parse(fs.readFileSync('timecards.json', 'utf-8'))
const response = await axios({
method: 'post',
url: `${salesforceUrl}/composite/sobjects`,
data,
headers: {
'Authorization': `OAuth ${token}`,
'Content-Type': 'application/json'
}
})
return response
}
By Name it's bit tricky. SF "natural" way would be to specify a helper field marked as external id (ideally it'd be marked unique too) and then you can use your references. "Dear Salesforce, I don't care what's your internal primary key of that Account record I need to link to, on my end it's 12345, go do your magic, look it up yourself".
It's in https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_upsert.htm?search_text=patch, look for example that says "Upserting Records and Associating with an External ID". It might not be very clear but if you have SF admin in team he/she should know what can be done with "upsert" operation in Data Loader, same principles would apply. I have an example that upserts multiple objects in one go, it'll be bit too crazy but try to read it: https://salesforce.stackexchange.com/a/274696/799
Or you could batch multiple requests into one all-or-none API call. It'll be like series of instructions to SF, not multiple round trips to you and having to cache results somewhere. In that call you could run queries and then use their temporary results in your final request. It'll look bit like https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_composite_record_manipulation.htm or https://developer.salesforce.com/blogs/tech-pubs/2017/01/simplify-your-api-code-with-new-composite-resources.html (scroll to the "A Simple Example, Now Using Composite!" part)
{
"compositeRequest" : [{
"method" : "POST",
"url" : "/services/data/v38.0/sobjects/Account",
"referenceId" : "refAccount",
"body" : {
"Name" : "My New Account"
}
},{
"method" : "GET",
"url" : "/services/data/v38.0/query/?q=select+id+from+contact+where+name='Howard+Jones'",
"referenceId" : "refContact"
},{
"method" : "PATCH",
"url" : "/services/data/v38.0/sobjects/Contact/#{refContact.records[0].Id}",
"referenceId" : "refContactUpdated",
"body" : {
"AccountId" : "#{refAccount.id}"
}
}]
}
The downside is that with composite you won't be able to do all 200 in 1 go.
You can have up to 25 subrequests in a single call. Up to 5 of these
subrequests can be sObject Collections or query operations, including
Query and QueryAll requests.

Add Existing Tag/Segment To Contacts

Lately I have found mailchimp-api-v3 to be quite useful for managing our .1k list. Currently, I use the following to (1) create new tags, and (2) add the tag to contacts:
const MC = require('mailchimp-api-v3');
const mailchimp = new MC('<apiKey>');
mailchimp.batch([{
method: 'POST',
path: '/lists/<list_id>/segments',
body: {
name: '<tag1>',
static_segment: [<contact_list1>]
}
}, {
method: 'POST',
path: '/lists/<list_id>/segments',
body: {
name: '<tag2>',
static_segment: [<contact_list2>]
}
}])
.then(results => {
console.log( results );
})
.catch(errs => {
console.log( errs );
});
Sometimes there's need to add an existing tag to contacts. Whenever I try to use the above code, as expected, I get tag already exists error and contacts are not tagged with this existing tag.
How do I get a list of all existing tags? And how would one add an existing tag to contacts?
I finally figured out how to do that. The just updated documentation is much easier to navigate that before. It's now evident that there are two ways of adding a tag/segment to (a) member(s): you can add a single tag to a member or you can add a single tag to many members:
Bulk add with members_to_add: array
{
method: 'POST',
path: '/lists/{list_id}/segments/{segment_id}',
body: {members_to_add: [<email-addresses>]}
}
This is indeed what I needed and I have now been using it for a while. Just today I found out the other method:
Single add with email_address: string
{
method: 'POST',
path: '/lists/{list_id}/segments/{segment_id}/members',
email_address: {email_address}
}

Using an arbitrary number of query params to filter results in mongoose

I'm building an API using node express and mongodb, with mongoose.
I have a post resource that handles user posts, and would like to be able to perform various queries on the post resource.
For instance I have a functions as that returns all posts as follows:
// Gets a list of Posts
exports.index = function(req, res) {
console.log(req.query);
Post.findAsync()
.then(mUtil.responseWithResult(res))
.catch(mUtil.handleError(res));
};
I looking for a good way of processing any additional query params that might come with the request.
/posts will return all posts, but /posts?user=12 will return posts by user with id 12 and /posts?likes=12 will return posts with 12 or more likes.
How can I check for and apply the these query params to filter and return the results since they may or may not be present.
Thanks ;)
If user=12 means "users with id 12", how does likes=12 mean "likes greater than 12"? You need to be more descriptive with your queries. You can do that by passing an array of objects. Send your query in a way that can be interpreted like this:
var filters = [
{
param: "likes",
type: "greater"
value: 12
},
{
param: "user",
type: "equal",
value: "12"
}]
var query = Post.find();
filters.forEach(function(filter) {
if (filter.type === "equal") {
query.where(filter.param).equals(filter.value);
}
else if (filter.type === "greater") {
query.where(filter.param).gt(filter.value);
}
// etc,,,
})
query.exec(callback);

Resources