Skip to content

Commit

Permalink
successful query of speckle stream data w gql
Browse files Browse the repository at this point in the history
  • Loading branch information
cdriesler committed Jul 12, 2023
1 parent 042974e commit cd0efe6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
52 changes: 47 additions & 5 deletions apps/nodepen-client/components/NodesAppContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import type React from 'react'
import { useCallback, useState } from 'react'
import { print } from 'graphql'
import gql from 'graphql-tag'
import type * as NodePen from '@nodepen/core'
import { NodesApp, DocumentView, SpeckleModelView } from '@nodepen/nodes'
import type { NodesAppState, NodesAppCallbacks } from '@nodepen/nodes'
Expand Down Expand Up @@ -46,7 +48,7 @@ const NodesAppContainer = ({ document: initialDocument, templates }: NodesAppCon
const handleExpireSolution = useCallback((state: NodesAppState): void => {
const solutionId = state.solution.id

const fetchSolution = async (): Promise<string> => {
const requestSolution = async (): Promise<string> => {
const response = await fetch('http://localhost:6500/grasshopper/id/solution', {
method: 'POST',
body: JSON.stringify({ solutionId, document: state.document }),
Expand All @@ -57,10 +59,50 @@ const NodesAppContainer = ({ document: initialDocument, templates }: NodesAppCon
return data
}

fetchSolution()
.then((data) => {
console.log(`🟢 Solution object id: ${data}`)
// setSolution(sanitize(data))
const fetchObjects = async (objectId: string): Promise<void> => {
const query = gql`
query GetObjects($streamId: String!, $objectId: String!) {
stream(id: $streamId) {
object(id: $objectId) {
data
children {
objects {
data
}
}
}
}
}
`

const response = await fetch(`${stream.url}/graphql`, {
method: 'POST',
headers: {
Authorization: `Bearer ${stream.token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: print(query),
operationName: 'GetObjects',
variables: {
streamId: stream.id,
objectId: objectId,
},
}),
})

const data = await response.json()

console.log(data)
}

requestSolution()
.then((rootObjectId) => {
console.log(`🟢 Solution object id: ${rootObjectId}`)
return fetchObjects(rootObjectId)
})
.then(() => {
// Do nothing
})
.catch((e) => {
console.log(e)
Expand Down
2 changes: 2 additions & 0 deletions apps/nodepen-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"@nodepen/core": "^2.0.0",
"@nodepen/nodes": "^2.0.0",
"@tanstack/react-query": "^4.29.19",
"graphql": "^16.7.1",
"graphql-tag": "^2.12.6",
"next": "13.4.9",
"next-images": "^1.8.5",
"react": "^18.0",
Expand Down
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cd0efe6

Please sign in to comment.