import { connectionPlugin , fieldAuthorizePlugin , makeSchema , nullabilityGuardPlugin , queryComplexityPlugin , } from 'nexus' ; import { ApolloServer } from 'apollo-server' ; import { separateOperations } from 'graphql' ; import { fieldExtensionsEstimator , getComplexity , simpleEstimator } from 'graphql-query-complexity' ; import path from 'path' ; import { logMutationTimePlugin , NodePlugin } from './example-plugins' ; import * as types from './kitchen-sink-definitions' ; const DEBUGGING_CURSOR = false ; let fn = DEBUGGING_CURSOR ? ( i ) => i : undefined ; const schema = makeSchema ( { types , outputs : { schema : path . join ( __dirname , '../kitchen-sink-schema.graphql' ) , typegen : { outputPath : path . join ( __dirname , './kitchen-sink.gen.ts' ) , globalsPath : path . join ( __dirname , './kitchen-sink-globals.gen.ts' ) , declareInputs : true , } , } , plugins : [ NodePlugin , connectionPlugin ( { encodeCursor : fn , decodeCursor : fn , } ) , queryComplexityPlugin ( ) , logMutationTimePlugin , fieldAuthorizePlugin ( ) , nullabilityGuardPlugin ( { shouldGuard : true , fallbackValues : { ID : ( ) => 'MISSING_ID' , Int : ( ) => - 1 , Date : ( ) => new Date ( 0 ) , Boolean : ( ) => false , String : ( ) => '' , } , } ) , ] , prettierConfig : require . resolve ( '../../../.prettierrc' ) , features : { abstractTypeStrategies : { __typename : true , resolveType : true , } , } , } ) ; const server = new ApolloServer ( { schema , plugins : [ { requestDidStart : ( ) => ( { didResolveOperation ( { request , document } ) { const complexity = getComplexity ( { schema , query : request . operationName ? separateOperations ( document ) [ request . operationName ] : document , variables : request . variables , estimators : [ fieldExtensionsEstimator ( ) , simpleEstimator ( { defaultComplexity : 1 } ) , ] , } ) ; if ( complexity >= 100 ) { throw new Error ( `Sorry, too complicated query! ${ complexity } is over 100 that is the max allowed complexity.` ) ; } console . log ( 'Used query complexity points:' , complexity ) ; } , } ) , } , ] , } ) ; const port = process . env . PORT || 4000 ; server . listen ( { port } , ( ) => console . log ( `🚀 Server ready at http://localhost:${ port }${ server . graphqlPath }` ) ) ;