Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disabling any resolver on a Mongoose model fails to generate the schema #6989

Closed
jstebenne opened this issue May 17, 2024 · 3 comments · Fixed by #7034
Closed

Disabling any resolver on a Mongoose model fails to generate the schema #6989

jstebenne opened this issue May 17, 2024 · 3 comments · Fixed by #7034

Comments

@jstebenne
Copy link
Contributor

Hello!

We’ve stumbled upon this error while trying to disable resolvers on one of our Mongoose models:

 Failed to generate the schema Error: Type MyModel does not have resolver with name 'count'
    at ObjectTypeComposer.getResolver (/app/node_modules/graphql-compose/src/ObjectTypeComposer.ts:1306:13)
    at /app/node_modules/@graphql-mesh/mongoose/cjs/index.js:80:76
    at Array.map (<anonymous>)
    at /app/node_modules/@graphql-mesh/mongoose/cjs/index.js:78:44
    at async Promise.all (index 4)
    at async Promise.all (index 0)
    at async MongooseHandler.getMeshSource (/app/node_modules/@graphql-mesh/mongoose/cjs/index.js:55:9)
    at async /app/node_modules/@graphql-mesh/runtime/cjs/get-mesh.js:90:28
    at async Promise.allSettled (index 0)
    at async getMesh (/app/node_modules/@graphql-mesh/runtime/cjs/get-mesh.js:85:5)

To trigger this error, we used the following configuration in .meshrc:

sources:
  - name: [redacted]
    handler:
      mongoose:
        connectionString: '{env.MONGO_DB_CONNECTION_STRING}'
        models:
          - name: MyModel
            path: ./src/models/my-model.ts#MyModel
            options:
              resolvers:
                count: false

Our package versions:

  "dependencies": {
    "@graphql-mesh/cli": "^0.88.7",
    "@graphql-mesh/mongoose": "^0.96.6",
    "@graphql-mesh/transform-filter-schema": "^0.96.6",
    "@graphql-mesh/transform-naming-convention": "^0.96.6",
    "@graphql-mesh/transform-rename": "^0.96.6",
    "graphql": "^16.8.1",
    "mongoose": "^8.3.1",
    "tsx": "4.7.2"
  },

We've managed to make a patch on the compiled code and are still testing it, but the problem seems to be gone, and we are able to move forward with our project.

Here is the diff that solved the problem:

diff --git a/node_modules/@graphql-mesh/mongoose/cjs/index.js b/node_modules/@graphql-mesh/mongoose/cjs/index.js
index 21a5e1e..7263909 100644
--- a/node_modules/@graphql-mesh/mongoose/cjs/index.js
+++ b/node_modules/@graphql-mesh/mongoose/cjs/index.js
@@ -63,13 +63,31 @@ class MongooseHandler {
                     throw new Error(`Model ${modelConfig.name} cannot be imported ${modelConfig.path}!`);
                 }
                 const modelTC = (0, graphql_compose_mongoose_1.composeWithMongoose)(model, modelConfig.options);
+
+                const resolversOption = modelConfig.options?.resolvers;
+                function isResolverEnabled(operation) {
+                  return !resolversOption?.hasOwnProperty(operation) || resolversOption[operation] !== false;
+                }
+
+                const enabledQueryOperations = modelQueryOperations.filter(isResolverEnabled);
+                const enabledMuationOperations = modelMutationOperations.filter(isResolverEnabled);
+
                 await Promise.all([
-                    Promise.all(modelQueryOperations.map(async (queryOperation) => schemaComposer.Query.addFields({
+                  Promise.all(
+                    enabledQueryOperations.map(async queryOperation =>
+                      schemaComposer.Query.addFields({
                         [`${modelConfig.name}_${queryOperation}`]: modelTC.getResolver(queryOperation),
-                    }))),
-                    Promise.all(modelMutationOperations.map(async (mutationOperation) => schemaComposer.Mutation.addFields({
-                        [`${modelConfig.name}_${mutationOperation}`]: modelTC.getResolver(mutationOperation),
-                    }))),
+                      }),
+                    ),
+                  ),
+                  Promise.all(
+                    enabledMuationOperations.map(async mutationOperation =>
+                      schemaComposer.Mutation.addFields({
+                        [`${modelConfig.name}_${mutationOperation}`]:
+                          modelTC.getResolver(mutationOperation),
+                      }),
+                    ),
+                  ),
                 ]);
                 const typeName = modelTC.getTypeName();
                 typeMergingOptions[typeName] = {

(CC: @jpboily-axceta)

This issue body was partially generated by patch-package.

@jstebenne
Copy link
Contributor Author

If you want, we can open a PR with this fix after we have completed our testing with the rest of our models/projects

@jpboily-axceta
Copy link
Contributor

So far this is working great! It even gave us the possibility of creating in advance types for our schemas that are embedded in more than one model. Previously, the first model that was using the schema create a type for it in the format ModelNameSchemaName.

With this fix in place, we can have models were all the resolvers are disabled to create types for our reusable schemas.

@ardatan
Copy link
Owner

ardatan commented May 20, 2024

We'd love to accept a PR :)

jstebenne added a commit to jstebenne/graphql-mesh that referenced this issue May 27, 2024
When disabling resolvers in the .meshrc configuration files, the Mongoose handler would still try to generate the schema, then fail.

Co-authored-by: Jean-Philippe Boily <jp.boily@axceta.com>
jstebenne added a commit to jstebenne/graphql-mesh that referenced this issue May 27, 2024
When disabling resolvers in the .meshrc configuration files, the Mongoose handler would still try to generate the schema, then fail.

Co-authored-by: Jean-Philippe Boily <jp.boily@axceta.com>
jstebenne added a commit to jstebenne/graphql-mesh that referenced this issue May 28, 2024
When disabling resolvers in the .meshrc configuration files, the Mongoose handler would still try to generate the schema, then fail.

Co-authored-by: Jean-Philippe Boily <jean-philippe.boily@axceta.com>
ardatan added a commit that referenced this issue Sep 4, 2024
…ng schema (#6989) (#7034)

* Fix: filter out disabled operations in Mongoose handler (#6989)

When disabling resolvers in the .meshrc configuration files, the Mongoose handler would still try to generate the schema, then fail.

Co-authored-by: Jean-Philippe Boily <jean-philippe.boily@axceta.com>

* ..

---------

Co-authored-by: Jean-Philippe Boily <jean-philippe.boily@axceta.com>
Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants