Created
March 17, 2021 20:11
-
-
Save trevorblades/d1ef1f34fc222643a44e0852e8c3fa24 to your computer and use it in GitHub Desktop.
Querying for something when a Gatsby node is created
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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