Skip to content

Commit

Permalink
Revert "➖ Old Dependencies – Removing @tinacms/graphql-old and /datal…
Browse files Browse the repository at this point in the history
…ayer-old…"

This reverts commit 97c2257.
  • Loading branch information
isaaclombardssw authored Sep 27, 2024
1 parent 74d239f commit 9f312b9
Show file tree
Hide file tree
Showing 3 changed files with 1,242 additions and 64 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"@react-three/fiber": "^8.16.8",
"@tailwindcss/aspect-ratio": "^0.4.2",
"@tinacms/auth": "1.0.6",
"@tinacms/datalayer-old": "npm:@tinacms/[email protected]",
"@tinacms/graphql-old": "npm:@tinacms/[email protected]",
"@typescript-eslint/eslint-plugin": "^5.42.0",
"@typescript-eslint/parser": "^5.42.0",
"@vercel/postgres": "^0.5.1",
Expand Down
89 changes: 89 additions & 0 deletions pages/api/graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//TODO – check if this is still needed, the graphiql playgrounds have already been replaced, see https://github.com/tinacms/tina.io/issues/2001
import * as datalayerPackage from '@tinacms/datalayer-old'
import * as gqlPackage from '@tinacms/graphql-old'

export default async function feedback(req, res) {
class InMemoryStore extends datalayerPackage.LevelStore {
public supportsSeeding() {
return true
}
public supportsIndexing() {
return false
}
}
class InMemoryBridge {
public rootPath: string
private mockFileSystem: { [filepath: string]: string } | undefined
private content: string
constructor(rootPath: string, content: string) {
this.rootPath = rootPath
this.mockFileSystem = mockFileSystem(content)
this.content = content
}
public glob = async (pattern: string) => {
return Object.keys(this.mockFileSystem).filter((key) =>
key.startsWith(pattern)
)
}

public delete = async (filepath: string) => {
const mockData = await this.getMockData()
delete mockData[filepath]
}

public get = async (filepath: string) => {
const mockData = await this.getMockData()
const value = mockData[filepath]
if (!value) {
throw new Error(`Unable to find record for ${filepath}`)
}
return value
}
public put = async (filepath: string, data: string) => {
const mockData = await this.getMockData()
this.mockFileSystem = { ...mockData, [filepath]: data }
}

public getMockData = async () => {
return this.mockFileSystem
}
public async putConfig(filepath: string, data: string) {
await this.put(filepath, data)
}
public supportsBuilding() {
return true
}
}

const mockFileSystem = (content: string) => ({
'posts/hello-world.md': content,
'authors/pedro.md': `---\nname: Pedro\navatar: https://images.unsplash.com/photo-1555959910-80920d0698a4?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&auto=format&fit=crop&w=1301&q=80\n---`,
'authors/napolean.md': `---\nname: Napolean\navatar: https://images.unsplash.com/photo-1606721977440-13e6c3a3505a?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=344&q=80\n---`,
})
// @ts-ignore
const schema = JSON.parse(req.query.schema)
const database = await gqlPackage.createDatabase({
// @ts-ignore
bridge: new InMemoryBridge('', req.query.content),
// @ts-ignore
store: new InMemoryStore('', true),
})

const query = req.query.query
const variables = req.query.variables
? // as string since this can be an array of strings (we're not using it that way)
JSON.parse(req.query.variables as string)
: {}
try {
await gqlPackage?.indexDB({ database, config: schema, buildSDK: false })
const result = await gqlPackage.resolve({
database,
query,
variables,
})
return res.json(result)
} catch (e) {
res.status(500)
return res.json({ errors: [{ message: e.message }] })
}
}
Loading

0 comments on commit 9f312b9

Please sign in to comment.