graphql-tools icon indicating copy to clipboard operation
graphql-tools copied to clipboard

Circular dependency in @graphql-tools/load-files

Open EngHans opened this issue 3 years ago • 1 comments

The Bug

Using load-files causes circular dependencies warnings to be triggered.

To Reproduce

Consider having a project with the following structure:

project/
├── app.js
├── resolvers/
│   ├── resolver1.js
│   ├── resolver2.js
│   └── index.js
.
.
.

app.js:

var express = require('express');
var { graphqlHTTP } = require('express-graphql');
var { buildSchema, GraphQLScalarType, Kind } = require('graphql');

var resolvers = require('./resolvers')

var schema = buildSchema(`
.
.
.
`);

var app = express();
app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: resolvers,
  graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at localhost:4000/graphql');

resolvers/index.js:

const path = require('path')
const { mergeResolvers } = require('@graphql-tools/merge')
const { loadFilesSync } = require('@graphql-tools/load-files')

const resolversArray = loadFilesSync([path.join(__dirname, './*.js')])
module.exports = mergeResolvers(resolversArray)

Expected behavior image

Current Output (node:3601) Warning: Accessing non-existent property 'default' of module exports inside circular dependency
(Use node --trace-warnings ... to show where the warning was created)
(node:3601) Warning: Accessing non-existent property 'schema' of module exports inside circular dependency
(node:3601) Warning: Accessing non-existent property 'typeDef' of module exports inside circular dependency
(node:3601) Warning: Accessing non-existent property 'typeDefs' of module exports inside circular dependency
(node:3601) Warning: Accessing non-existent property 'resolver' of module exports inside circular dependency
(node:3601) Warning: Accessing non-existent property 'resolvers' of module exports inside circular dependency

Environment:

  • OS: Kubuntu
  • @graphql-tools/load-files:
  • NodeJS: 14.15.3

Additional context

I got rid of the circular dependencies using the package merge-graphql-schemas, as follows:

resolvers/index.js:

const path = require('path');
const { mergeResolvers, fileLoader } = require('merge-graphql-schemas');

const resolversArray = fileLoader(path.join(__dirname, './*.js'));
const resolvers = mergeResolvers(resolversArray);

module.exports = resolvers;

EngHans avatar Mar 15 '22 22:03 EngHans

loadFiles is using nodejs module system. If it cannot handle circular dependency, we have nothing to do with that as far as I know. If someone has a solution, PRs are welcome

ardatan avatar Mar 15 '22 23:03 ardatan

Closing this for now since the main cause is the module system of the environment itself.

ardatan avatar Mar 29 '23 05:03 ardatan