Skip to content

Instantly share code, notes, and snippets.

@trevorblades
Created March 17, 2021 20:11
Show Gist options
  • Save trevorblades/d1ef1f34fc222643a44e0852e8c3fa24 to your computer and use it in GitHub Desktop.
Save trevorblades/d1ef1f34fc222643a44e0852e8c3fa24 to your computer and use it in GitHub Desktop.
Querying for something when a Gatsby node is created
import {GraphQLClient, gql} from 'graphql-request';
const client = new GraphQLClient('https://api.shopify.com/graphql', {
headers: {
authorization: 'Bearer MY_TOKEN' // environment variables here
}
});
const GET_PRODUCTS = gql`
query GetProducts($query: String!) {
products(first: 5, query: $query) {
edges {
node {
id
title
image
}
}
}
}
`;
exports.onCreateNode = async ({node, actions}) => {
if (node.internal.type === 'ContentfulVideo') {
console.log(node);
// graphql-request library
const response = await client.request(GET_PRODUCTS, {
query: `name:contains:${node.tags[0]}`
});
actions.createNodeField({
node,
name: 'products',
value: response.products
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment