import { Commun , ConfigManager } from '@commun/core' ; import { Express } from 'express' ; import ExpressGraphQL from 'express-graphql' ; import { createGraphQLSchema } from './SchemaBuilder' ; import { printSchema } from 'graphql' ; export const GraphQLModule = { async setup ( ) { Commun . registerLogsToken ( 'graphql-operation' , req => req . body ?. operationName ) ; Commun . registerPlugin ( 'graphql' , { onExpressAppCreated : this . setupGraphql . bind ( this ) , afterServerStart : ( ) => { console . log ( `    👌 GraphQL endpoint: ${ Commun . getOptions ( ) . endpoint }/graphql` ) ; console . log ( ) ; } , } ) ; } , async setupGraphql ( app ) { const schema = createGraphQLSchema ( ) ; app . use ( '/graphql' , ExpressGraphQL ( { schema , graphiql : process . env . NODE_ENV === 'development' , } ) ) ; if ( process . env . NODE_ENV === 'development' ) { const schemaStr = '# auto-generated by Commun (https://commun.dev)\n\n' + printSchema ( schema ) ; await ConfigManager . writeGeneratedFile ( 'schema.graphql' , schemaStr , true ) ; } } , } ;