import * as gqlPackage from '@tinacms/graphql' ; export default async function feedback ( req , res ) { class InMemoryStore extends gqlPackage . MemoryStore { public supportsSeeding ( ) { return true ; } public supportsIndexing ( ) { return false ; } } class InMemoryBridge { public rootPath : string ; private mockFileSystem : { [ filepath ] : string ; } | undefined ; private content : string ; constructor ( rootPath , content ) { this . rootPath = rootPath ; this . mockFileSystem = mockFileSystem ( content ) ; this . content = content ; } public glob = async ( pattern ) => { return Object . keys ( this . mockFileSystem ) . filter ( key => key . startsWith ( pattern ) ) ; } ; public get = async ( filepath ) => { 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 , data ) => { const mockData = await this . getMockData ( ) ; this . mockFileSystem = { ... mockData , [ filepath ] : data } ; } ; public getMockData = async ( ) => { return this . mockFileSystem ; } ; public async putConfig ( filepath , data ) { await this . put ( filepath , data ) ; } public supportsBuilding ( ) { return true ; } } const mockFileSystem = ( content ) => ( { '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---` , } ) ; const schema = JSON . parse ( req . query . schema ) ; const database = await gqlPackage . createDatabase ( { bridge : new InMemoryBridge ( '' , req . query . content ) , store : new InMemoryStore ( '' ) , } ) ; const query = req . query . query ; const variables = req . query . variables ? 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 } ] } ) ; } }