Skip to content

Commit

Permalink
✨ Added reviews. menu, and photos to GraphQL
Browse files Browse the repository at this point in the history
  • Loading branch information
whoisryosuke committed Sep 22, 2018
1 parent b735ae5 commit cc9605d
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 8 deletions.
117 changes: 114 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The plugin creates an endpoint called `KushyPost` and `allKushyPost`. You query

All the parameters from the API are plugged into GraphQL, so if an API endpoint has a `avatar` property, you can query GraphQL for it.

Here is a sample query for a shop:
### Query your shop/brand

```graphql
{
Expand Down Expand Up @@ -67,14 +67,125 @@ Here is a sample query for a shop:
}
```

### Query your reviews

```graphql
{
allKushyReview {
edges {
node{
id
status
post_id
user_id
rating
useful
dank
funny
review
created_at {
date
}
updated_at {
date
}
includes {
user {
id
name
username
avatar
}
}
}
}
}
}
```

### Query your menu

```graphql
{
allKushyMenu {
edges {
node{
id
product {
id
name
slug
categories
avatar
featured_img
}
pricing {
pricing_type
list_price
sale_price
half_gram
one_gram
two_grams
eighth_ounce
quarter_ounce
half_ounce
ounce
quarter_pound
half_pound
pound
}
cannabinoids {
thc
cbd
cbn
}
}
}
}
}
```

### Query your photos

```graphql
{
allKushyPhoto {
edges {
node {
id
image
caption
user_id
post_id
featured
created_at{
date
timezone
}
updated_at{
date
timezone
}
}
}
}
}
```

## Todo

* [] - Allow users to add a config for section and ID to load a specific shop/brand.
* [] - Add endpoint for shop/brand menu (`KushyMenu` / `allKushyMenu`)
* [] - Add endpoint for shop/brand menu (`KushyMenu` / `allKushyMenu`)
* [] - Add endpoint for shop/brand photos (`KushyPhotos` / `allKushyPhotos`)
* [] - Add endpoint for shop/brand reviews (`KushyReviews` / `allKushyReviews`)
* [] - Add endpoint for shop/brand reviews (`KushyReviews` / `allKushyReviews`)

### Pending

* [] - Create async function to load all pages from API, currently only loads first (see: [getPages()](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-wordpress/src/fetch.js#L334-L348))
* [] - Create helper function to download all loaded media into cache (see: [downloadMediaFiles()](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-wordpress/src/normalize.js#L452))


## Credits

* [GatsbyJS](https://www.gatsbyjs.org/)
* [GatsbyJS Source Plugin tutorial](https://www.gatsbyjs.org/docs/source-plugin-tutorial/)
46 changes: 41 additions & 5 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,39 @@ exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {

// Gatsby adds a configOption that's not needed for this plugin, delete it
delete configOptions.plugins

const fetchData = (url, section, type) => {
fetch(url)
.then(response => response.json())
.then(data => {
data.data.forEach(post => {
console.log(section, post)
// Process data to create a GraphQL node matching the structure of the API
const nodeData = processPost(post, section, type)
// Use Gatsby's createNode helper to create a node from the node data
createNode(nodeData)
})
})
}

const fetchMenu = slug => {
const url = `http://localhost/api/v1/inventory/menu/${slug}`
fetchData(url, 'menu', 'KushyMenu')
}

const fetchPhotos = id => {
const url = `http://localhost/api/v1/photos/post/${id}`
fetchData(url, 'photo', 'KushyPhoto')
}

const fetchReviews = id => {
const url = `http://localhost/api/v1/reviews/post/${id}`
fetchData(url, 'review', 'KushyReview')
}

// Helper function that processes a photo to match Gatsby's node structure
const processPost = post => {
const nodeId = createNodeId(`kushy-post-${post.id}`)
const processPost = (post, section, type) => {
const nodeId = createNodeId(`kushy-${section}-${post.id}`)
console.log('post', post)
const nodeContent = JSON.stringify(post)
const nodeContentDigest = crypto
Expand All @@ -25,7 +54,7 @@ exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
parent: null,
children: [],
internal: {
type: `KushyPost`,
type,
content: nodeContent,
contentDigest: nodeContentDigest,
},
Expand All @@ -35,7 +64,7 @@ exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
}

// Convert the options object into a query string
const apiOptions = queryString.stringify(configOptions)
// const apiOptions = queryString.stringify(configOptions)

// Make sure user has added necessary config
// If not, give them 5 recent dispensaries
Expand All @@ -52,9 +81,16 @@ exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
.then(data => {
data.data.forEach(post => {
// Process data to create a GraphQL node matching the structure of the API
const nodeData = processPost(post)
const nodeData = processPost(post, 'post', 'KushyPost')
// Use Gatsby's createNode helper to create a node from the node data
createNode(nodeData)

// Grab reviews and generate nodes
const reviews = fetchReviews(post.id)
// Grab reviews and generate nodes
const photos = fetchPhotos(post.id)
// Grab menu and generate nodes
const menu = fetchMenu(post.slug)
})
})
)
Expand Down

0 comments on commit cc9605d

Please sign in to comment.