olavea / Lillian-and-Ola-Vea-com

Lillian-and-Ola-Vea-com
1 stars 0 forks source link

add Xata #67

Open olavea opened 1 year ago

olavea commented 1 year ago

branch stream/avatars

const { getXataClient } = require("./xata"); const { Client } = require("twitter-api-sdk"); // const { getXataClient } = require("./xata"); // const { Client } = require("twitter-api-sdk");

const createUserAvatarNodes = async (gatsbyUtils) => { // 1. Get the list of users from Xata ✅

const xata = getXataClient();

const records = await xata.db.accounts.getAll();

console.log(records);

// 2. Get info about users from Twitter ✅

const twitterClient = new Client(process.env.TWITTER_BEARER_TOKEN);

for (const record of records) { const { data: account } = await twitterClient.users.findUserById( record.id, { "user.fields": ["profile_image_url"], } );

// 3. Create Gatsby Data Node ✅ const { actions, createNodeId, createContentDigest } = gatsbyUtils; const { createNode } = actions;

  createNode({
    id: createNodeId(account.username),
    avatarUrl: account.profile_image_url,
    username: account.username,
    internal: {
      type: "UserAvatar",
      contentDigest: createContentDigest(account),
    },
  })

// createNode({ // id: createNodeId(account.username), // avatarUrl: account.profile_image_url, // username: account.username, // internal: { // type: "UserAvatar", // contentDigest: createContentDigest(account), // }, // });

}

};

// 4. Gatsby Plugin Remote Image ✅

exports.sourceNodes = async (gatsbyUtils) => { const { reporter } = gatsbyUtils;

reporter.verbose("Sourcing user avatars - START"); await createUserAvatarNodes(gatsbyUtils); reporter.verbose("Sourcing user avatars - DONE"); };

exports.createSchemaCustomization = ({ actions }) => { const { createTypes } = actions; const typeDefs = type UserAvatar implements Node { username: String! avatarUrl: String! } ; createTypes(typeDefs); };

https://twitter.com/jonnyshare/status/1594746513430925313