@@ -10,8 +10,11 @@ import {
10
10
buildSchema ,
11
11
UseMiddleware ,
12
12
} from "type-graphql" ;
13
- import { ApolloServer } from "apollo-server" ;
13
+ import { ApolloServer } from "@apollo/server" ;
14
+ import express from "express" ;
14
15
import { prisma } from "./db.server" ;
16
+ import { expressMiddleware } from '@apollo/server/express4' ;
17
+ import { storeOffchainAttestation } from "./utils" ;
15
18
16
19
const PORT = process . env . GRAPH_PORT || 4000 ;
17
20
@@ -138,13 +141,53 @@ export async function startGraph() {
138
141
authChecker : customAuthChecker ,
139
142
} ) ;
140
143
144
+ const app = express ( ) ;
145
+ app . use ( express . json ( ) ) ;
146
+
147
+ app . post ( "/offchain/store" , async ( req , res ) => {
148
+ try {
149
+
150
+ if ( ! req . body . textJson ) {
151
+ return res . status ( 400 ) . json ( { error : "textJson is required" } ) ;
152
+ }
153
+
154
+ let parsedJson ;
155
+ try {
156
+ parsedJson = typeof req . body . textJson === "string"
157
+ ? JSON . parse ( req . body . textJson )
158
+ : req . body . textJson ;
159
+ } catch ( parseError ) {
160
+ return res . status ( 400 ) . json ( { error : "Invalid JSON in textJson" } ) ;
161
+ }
162
+ const attestation = await storeOffchainAttestation ( parsedJson ) ;
163
+
164
+ res . json ( { status : "ok" , attestation } ) ;
165
+ }
166
+ catch ( error ) {
167
+ const sanitizedError = typeof error === "string" ? error . replace ( / \n | \r / g, "" ) : JSON . stringify ( error ) . replace ( / \n | \r / g, "" ) ;
168
+ console . error ( "Error storing offchain attestation:" , sanitizedError ) ;
169
+ res . status ( 500 ) . json ( { error : "Internal server error" } ) ;
170
+ }
171
+ } ) ;
172
+
141
173
const server = new ApolloServer ( {
142
174
schema : schema ,
143
175
cache : "bounded" ,
144
176
introspection : true ,
145
- context : ( ) => ( { prisma } ) ,
146
177
} ) ;
147
178
148
- const { url } = await server . listen ( PORT ) ;
149
- console . log ( `Server is running, GraphQL Playground available at ${ url } ` ) ;
179
+ await server . start ( ) ;
180
+ app . use (
181
+ '/graphql' ,
182
+ express . json ( { limit : '100kb' } ) ,
183
+ expressMiddleware ( server , {
184
+ context : async ( ) => ( { prisma } ) ,
185
+ } )
186
+ ) ;
187
+
188
+ app . listen ( PORT , ( ) => {
189
+ console . log ( `GraphQL en http://localhost:${ PORT } /graphql` ) ;
190
+ console . log ( `REST en http://localhost:${ PORT } /offchain/store` ) ;
191
+ } ) ;
150
192
}
193
+
0 commit comments