Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

graphql-query-complexity causing schema introspection error in apollo graphql #99

Open
@arijit-itobuz

Description

import 'reflect-metadata';
import http from 'node:http';
import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
import cors from 'cors';
import express, { type NextFunction, type Request, type Response } from 'express';
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import { config } from './config/config.js';
// import { graphqlQueryComplexity } from './graphql/lib/graphqlQueryComplexity.lib.js';
import { typegraphqlSchema } from './graphql/typegraphqlSchema.js';
import { restRouter } from './rest/routes/route.js';

async function main() {
try {
const port = config.app.APP_PORT;
const app = express();
const http_server = http.createServer(app);

/* apollo server */
const apolloServer = new ApolloServer({
 schema: typegraphqlSchema,
 introspection: config.app.APP_ENV === 'dev',
 plugins: [
 ApolloServerPluginDrainHttpServer({ httpServer: http_server }),
 // graphqlQueryComplexity. // commenting fixes the error
 ],
});
await apolloServer.start();
/* middlewares */
app.use(cors());
app.use(express.json({ limit: '50mb', type: 'application/json' }));
app.use(express.urlencoded({ limit: '50mb', extended: true, type: 'application/x-www-form-urlencoded' }));
app.use(graphqlUploadExpress({ maxFileSize: 1 * 1024 * 1024, maxFiles: 5 }));
/* api */
app.get('/', (req: Request, res: Response, next: NextFunction) => res.status(200).send('root'));
app.use('/rest', restRouter);
app.use(
 '/graphql',
 expressMiddleware(apolloServer, { context: async ({ req }) => ({ req: req, api: 'graphql' }) })
);
app.use((req: Request, res: Response, next: NextFunction) => res.status(404).send('Page Not Found'));
/* start */
await new Promise<void>((resolve) => http_server.listen({ port: port }, resolve));
console.log(`[rest]: http://localhost:${port}/rest/`);
console.log(`[graphql]: http://localhost:${port}/graphql/`);

} catch (error) {
console.log('[Error]:', error);
}
}

main();


import type { GraphQLRequest } from '@apollo/server';
import type { DocumentNode } from 'graphql';
import { fieldExtensionsEstimator, getComplexity, simpleEstimator } from 'graphql-query-complexity';
import { config } from '../../config/config.js';
import { typegraphqlSchema } from '../typegraphqlSchema.js';

export const graphqlQueryComplexity = {
requestDidStart: async () => ({
async didResolveOperation({ request, document }: { request: GraphQLRequest; document: DocumentNode }) {
const complexity = getComplexity({
schema: typegraphqlSchema,
operationName: request.operationName,
query: document,
variables: request.variables,
estimators: [fieldExtensionsEstimator(), simpleEstimator({ defaultComplexity: 1 })],
});

 if (complexity > config.graphql.GRAPHQL_QUERY_COMPLEXITY) {
 throw new Error(
 `Sorry, too complicated query! ${complexity} exceeded the maximum allowed complexity of ${config.graphql.GRAPHQL_QUERY_COMPLEXITY}`
 );
 }
 console.log('[Graphql Query Complexity]:', { Operation: request.operationName, Complexity: complexity });
},

}),
};


error

Schema Introspection Failure

Introspection is disabled on this endpoint. Enable introspection to populate your schema.
Unable to reach server

We were unable to introspect your endpoint. Please enable introspection on your server. (You might still be able to submit operations!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /