1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-06-30 22:01:44 +02:00

new tactic: pull in assets into GraphQL on build time

This commit is contained in:
Matthias Kretschmann 2020-07-07 09:27:05 +02:00
parent 7860aecd98
commit b653ed7990
Signed by: m
GPG Key ID: 606EEEF3C479A91F
6 changed files with 143 additions and 80 deletions

View File

@ -28,6 +28,7 @@ module.exports = {
path: `${__dirname}/node_modules/@oceanprotocol/art/` path: `${__dirname}/node_modules/@oceanprotocol/art/`
} }
}, },
'gatsby-source-ocean',
{ {
resolve: 'gatsby-plugin-sharp', resolve: 'gatsby-plugin-sharp',
options: { options: {

View File

@ -1,5 +1,4 @@
const path = require('path') const path = require('path')
const axios = require('axios')
// const { config } = require('./src/config/ocean') // const { config } = require('./src/config/ocean')
exports.onCreateWebpackConfig = ({ actions }) => { exports.onCreateWebpackConfig = ({ actions }) => {
@ -11,47 +10,70 @@ exports.onCreateWebpackConfig = ({ actions }) => {
}) })
} }
exports.createPages = async ({ actions, reporter }) => { exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions const { createPage } = actions
// Query for all assets to use in creating pages.
const result = await axios(
`https://aquarius.marketplace.oceanprotocol.com/api/v1/aquarius/assets`
)
const assets = result.data.ids
// Handle errors // Create pages for all assets
const assetDetailsTemplate = path.resolve(
'src/components/templates/AssetDetails/index.tsx'
)
const result = await graphql(`
query {
allAsset {
edges {
node {
did
main {
type
name
dateCreated
author
license
price
datePublished
files {
contentType
index
}
}
additionalInformation {
description
deliveryType
termsAndConditions
access
}
}
}
}
}
`)
if (result.errors) { if (result.errors) {
reporter.panicOnBuild(`Error while querying Aquarius for all assets.`) throw result.errors
return
} }
// Create pages for each DID await result.data.allAsset.edges.forEach(({ node }) => {
const assetDetailsTemplate = path.resolve( const path = `/asset/${node.did}`
`src/components/templates/AssetDetails/index.tsx`
)
await assets.forEach(async (did) => { createPage({
const path = `/asset/${did}`
await createPage({
path, path,
component: assetDetailsTemplate, component: assetDetailsTemplate,
context: { did } context: { did: node.did }
}) })
}) })
} }
exports.onCreatePage = async ({ page, actions }) => { // exports.onCreatePage = async ({ page, actions }) => {
const { createPage } = actions // const { createPage } = actions
// // page.matchPath is a special key that's used for matching pages
// // only on the client.
// const handleClientSideOnly = page.path.match(/^\/asset/)
// page.matchPath is a special key that's used for matching pages // if (handleClientSideOnly) {
// only on the client. // page.matchPath = '/asset/*'
const handleClientSideOnly = page.path.match(/^\/asset/)
if (handleClientSideOnly) { // // Update the page.
page.matchPath = '/asset/*' // createPage(page)
// }
// Update the page. // }
createPage(page)
}
}

View File

@ -0,0 +1,46 @@
const axios = require('axios')
exports.sourceNodes = async ({
actions,
createNodeId,
createContentDigest
}) => {
const { createNode } = actions
// Query for all assets to use in creating pages.
const result = await axios(
`https://aquarius.marketplace.oceanprotocol.com/api/v1/aquarius/assets`
)
for (let i = 0; i < result.data.ids.length; i++) {
const did = result.data.ids[i]
const metadataResult = await axios(
`https://aquarius.marketplace.oceanprotocol.com/api/v1/aquarius/assets/metadata/${did}`
)
const metadata = {
did,
...metadataResult.data.attributes
}
const nodeMeta = {
id: createNodeId(did),
parent: null,
children: [],
internal: {
type: 'Asset',
mediaType: 'application/json',
content: JSON.stringify(metadata),
contentDigest: createContentDigest(metadata)
}
}
const node = {
...metadata,
...nodeMeta
}
await createNode(node)
}
}

View File

@ -0,0 +1,3 @@
{
"name": "gatsby-source-ocean"
}

View File

@ -1,53 +1,41 @@
import React, { useState, ReactElement, useEffect } from 'react' import React, { ReactElement } from 'react'
import { Aquarius, Logger } from '@oceanprotocol/squid' import { PageProps, graphql } from 'gatsby'
import { PageProps } from 'gatsby'
import { config } from '../../../config/ocean'
import Layout from '../../../components/Layout' import Layout from '../../../components/Layout'
import { MetaDataMarket, ServiceMetaDataMarket } from '../../../@types/MetaData'
import { Alert } from '../../atoms/Alert'
import AssetContent from './AssetContent' import AssetContent from './AssetContent'
export default function AssetDetailsTemplate(props: PageProps): ReactElement { export default function AssetDetailsTemplate(props: PageProps): ReactElement {
const [metadata, setMetadata] = useState<MetaDataMarket>() const { asset } = props.data as any
const [title, setTitle] = useState<string>()
const [error, setError] = useState<string>()
const { did } = props.pageContext as { did: string } return (
<Layout title={asset.main.name} uri={props.path}>
<AssetContent did={asset.did} metadata={asset} />
</Layout>
)
}
useEffect(() => { export const templateQuery = graphql`
async function init() { query AssetByDid($did: String!) {
try { asset(did: { eq: $did }) {
const aquarius = new Aquarius(config.aquariusUri, Logger) did
const ddo = await aquarius.retrieveDDO(did) main {
type
if (!ddo) { name
setTitle('Could not retrieve asset') dateCreated
setError('The DDO was not found in Aquarius.') author
return license
price
datePublished
files {
index
contentType
} }
}
const { attributes }: ServiceMetaDataMarket = ddo.findServiceByType( additionalInformation {
'metadata' description
) deliveryType
termsAndConditions
setTitle(attributes.main.name) access
console.log(attributes)
setMetadata(attributes)
} catch (error) {
setTitle('Error retrieving asset')
setError(error.message)
} }
} }
init() }
}, []) `
return error ? (
<Layout title={title} noPageHeader uri={props.path}>
<Alert title={title} text={error} state="error" />
</Layout>
) : did && metadata ? (
<Layout title={title} uri={props.path}>
<AssetContent did={did} metadata={metadata} />
</Layout>
) : null
}

View File

@ -13,7 +13,7 @@ export default function AssetRoute(props: PageProps): ReactElement {
const [title, setTitle] = useState<string>() const [title, setTitle] = useState<string>()
const [error, setError] = useState<string>() const [error, setError] = useState<string>()
const { did } = props.pageContext as { did: string } const did = props.location.pathname.split('/')[2]
useEffect(() => { useEffect(() => {
async function init() { async function init() {
@ -32,7 +32,6 @@ export default function AssetRoute(props: PageProps): ReactElement {
) )
setTitle(attributes.main.name) setTitle(attributes.main.name)
console.log(attributes)
setMetadata(attributes) setMetadata(attributes)
} catch (error) { } catch (error) {
setTitle('Error retrieving asset') setTitle('Error retrieving asset')
@ -43,14 +42,18 @@ export default function AssetRoute(props: PageProps): ReactElement {
}, []) }, [])
return error ? ( return error ? (
<Layout title={title} noPageHeader uri={props.path}> <Layout title={title} noPageHeader uri={props.location.pathname}>
<Alert title={title} text={error} state="error" /> <Alert title={title} text={error} state="error" />
</Layout> </Layout>
) : did && metadata ? ( ) : did && metadata ? (
<Layout title={title} uri={props.path}> <Layout title={title} uri={props.location.pathname}>
<Router basepath="/asset"> <Router basepath="/asset">
<AssetContent did={did} metadata={metadata} path="/asset/:did" /> <AssetContent did={did} metadata={metadata} path="/asset/:did" />
</Router> </Router>
</Layout> </Layout>
) : null ) : (
<Layout title="Loading..." uri={props.location.pathname}>
Loading...
</Layout>
)
} }