Skip to content

Instantly share code, notes, and snippets.

@MichaelCurrin
Last active March 6, 2024 10:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MichaelCurrin/ae75545c451629b909a7dd7580a08add to your computer and use it in GitHub Desktop.
Save MichaelCurrin/ae75545c451629b909a7dd7580a08add to your computer and use it in GitHub Desktop.
Twitter GraphQL sample

Twitter GraphQL sample

Example of using a GraphQL service to explore the Twitter API

A great way to explore the Twitter API and returned data, without worrying about authorization or local setup. Just add a query in the explorer and hit run.

About

The GraphQL Hub website has an interactive GraphiQL service which allows access to Gitub, Twitter, Giphy and other APIs in the browser without authorization.

There are some convenient "playground" links to explore on the homepage:

Sample view

The example in this gist covers how to get Twitter data from the service. The example covers the functionality the service is capable of:

  • lookup a tweet by ID
  • lookup a user by ID or screen name
  • search tweets by query e.g. hashtag search.

Look at the other files in this gist or follow the link below:

https://www.graphqlhub.com/playground?query...

Extend

Ideas to improve on the query:

  • Repeat the user section or search section for additional lookups
  • Increase the count for the search
  • use variables to make the query more flexible then set the values using the parameters pane at the bottom. That needs to valid JSON.

Limitations

Note that the functionality on the service is very limited.

You cannot do paging for higher volume of search tweets. There are a lot of things in the full Twitter API which are not covered here. Also the fields are limited - you can get retweet count but not favorites for example.

And it feels buggy:

  • I found url was not set in search when it was fine in user.
  • Adding retweet-related fields reduced the number of tweets.
  • It did not return a tweet which a known ID while another tweet ID was fine.
{
graphQLHub
twitter {
user(identifier: name, identity: "ThePSF") {
name
screen_name
followers_count
url
profile_image_url
tweets_count
description
tweets(limit: 3) {
id
created_at
text
retweet_count
}
}
tweet(id: "1260993677213736961" ) {
id
text
user {
screen_name
}
created_at
}
search(q: "#python", count: 10) {
id
user {
screen_name
followers_count
profile_image_url
}
text
retweet_count
created_at
}
}
}
{
"data": {
"graphQLHub": "Use GraphQLHub to explore popular APIs with GraphQL! Created by Clay Allsopp @clayallsopp",
"twitter": {
"user": {
"name": "Python Software Foundation",
"screen_name": "ThePSF",
"followers_count": 320094,
"url": "http://t.co/KdOzhmst4U",
"profile_image_url": "http://pbs.twimg.com/profile_images/439154912719413248/pUBY5pVj_normal.png",
"tweets_count": 5096,
"description": "The Python Software Foundation. For help with Python code, see comp.lang.python.",
"tweets": [
{
"id": "1261008477411061800",
"created_at": "Thu May 14 19:00:24 +0000 2020",
"text": "Need help covering travel/registration funds due to an #opensource conference being cancelled? Apply for assistance… https://t.co/fTx9KsfN7o",
"retweet_count": 6
},
{
"id": "1260993677213737000",
"created_at": "Thu May 14 18:01:35 +0000 2020",
"text": "Lightning Talks Part 2 - Python Language Summit 2020 https://t.co/7ZGtczGa9y",
"retweet_count": 5
},
{
"id": "1260946433311727600",
"created_at": "Thu May 14 14:53:51 +0000 2020",
"text": "Salesforce Engineering puts Python to work across many areas of their business. See how they use it in machine lear… https://t.co/JFyqw6CHzd",
"retweet_count": 8
}
]
},
"tweet": {
"id": "1260993677213737000",
"text": "Lightning Talks Part 2 - Python Language Summit 2020 https://t.co/7ZGtczGa9y",
"user": {
"screen_name": "ThePSF"
},
"created_at": "Thu May 14 18:01:35 +0000 2020"
},
"search": [
{
"id": "1261336574324813800",
"user": {
"screen_name": "TheDeveloperBot",
"followers_count": 298,
"profile_image_url": "http://pbs.twimg.com/profile_images/1250454907871580162/Al-vkUN1_normal.jpg"
},
"text": "RT @AyoubKhan558: Check out my Gig on Fiverr:\nMake frontend for web apps using angular, react or vuejs\nhttps://t.co/eOdXqJmVb3\n\n\n\n\n\n#j…",
"retweet_count": 3,
"created_at": "Fri May 15 16:44:08 +0000 2020"
},
{
"id": "1261336551369240600",
"user": {
"screen_name": "AyoubKhan558",
"followers_count": 56,
"profile_image_url": "http://pbs.twimg.com/profile_images/1250374076813783040/8RjLhrzG_normal.jpg"
},
"text": "Check out my Gig on Fiverr:\nMake frontend for web apps using angular, react or vuejs\nhttps://t.co/eOdXqJmVb3\n\n\n•… https://t.co/KxOohFl7QV",
"retweet_count": 3,
"created_at": "Fri May 15 16:44:03 +0000 2020"
},
{
"id": "1261336509854204000",
"user": {
"screen_name": "StephaniePaser1",
"followers_count": 191,
"profile_image_url": "http://pbs.twimg.com/profile_images/1087743226591760384/4WTYhyIV_normal.jpg"
},
"text": "RT @realpython: 🐍📰 Python 3 Installation & Setup Guide \n\nIn this Python installation guide you'll see step by step how to get a working Pyt…",
"retweet_count": 3,
"created_at": "Fri May 15 16:43:53 +0000 2020"
},
{
"id": "1261336351284355000",
"user": {
"screen_name": "butecopensource",
"followers_count": 430,
"profile_image_url": "http://pbs.twimg.com/profile_images/555010522521681921/CliGvM8y_normal.png"
},
"text": "AWS S3 em Python https://t.co/MwvWRpmyFV #python #aws #s3",
"retweet_count": 0,
"created_at": "Fri May 15 16:43:15 +0000 2020"
},
{
"id": "1261336327783682000",
"user": {
"screen_name": "ML_Tweet_Bot",
"followers_count": 340,
"profile_image_url": "http://pbs.twimg.com/profile_images/1257171060950253568/oouTFT0u_normal.png"
},
"text": "RT @fionnfuchs: A while ago I wrote a small Medium article on generating game assets using unsupervised #MachineLearning with a small amoun…",
"retweet_count": 1,
"created_at": "Fri May 15 16:43:09 +0000 2020"
},
{
"id": "1261336305172131800",
"user": {
"screen_name": "fionnfuchs",
"followers_count": 118,
"profile_image_url": "http://pbs.twimg.com/profile_images/1255940259663286289/PGJfzsxI_normal.jpg"
},
"text": "A while ago I wrote a small Medium article on generating game assets using unsupervised #MachineLearning with a sma… https://t.co/mWl24Rcyjr",
"retweet_count": 1,
"created_at": "Fri May 15 16:43:04 +0000 2020"
},
{
"id": "1261336247693385700",
"user": {
"screen_name": "merit_bakare",
"followers_count": 230,
"profile_image_url": "http://pbs.twimg.com/profile_images/1142732425426067456/8bK0mZ1l_normal.jpg"
},
"text": "RT @gp_pulipaka: Free Springer 65 #DataScience #Books During #COVID19. #BigData #Analytics #IoT #IIoT #Python #RStats #TensorFlow #JavaScri…",
"retweet_count": 61,
"created_at": "Fri May 15 16:42:50 +0000 2020"
},
{
"id": "1261336199194488800",
"user": {
"screen_name": "DubasiSridhar",
"followers_count": 836,
"profile_image_url": "http://pbs.twimg.com/profile_images/1245623536170893315/dnGZxyHK_normal.jpg"
},
"text": "RT @justmarkham: 🤖⚡ scikit-learn tip #35:\n\nThere's no need to use \".values\" when passing a DataFrame or Series to scikit-learn... it knows…",
"retweet_count": 8,
"created_at": "Fri May 15 16:42:39 +0000 2020"
},
{
"id": "1261336140122128400",
"user": {
"screen_name": "DNAed_tech",
"followers_count": 5257,
"profile_image_url": "http://pbs.twimg.com/profile_images/749364509190524928/UITNLp3U_normal.jpg"
},
"text": "RT @coding_monkeys: me and my project🤣🤣🤣⁣\n\n#AI #ML #vuejs #BigData #cloud #IoT #HPC #robotics #Programmer #enjoy #RStats #javascript #creat…",
"retweet_count": 54,
"created_at": "Fri May 15 16:42:24 +0000 2020"
}
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment