profusion/apollo-validation-directives

Use with graphql-tools mocking lib

craigmulligan opened this issue ยท 3 comments

Hey ๐Ÿ‘‹ thanks for the great library.

I'd like to get apollo-validation-directives working with graphql-tools mocking lib .

I thought I'd be able to call addValidationResolversToSchema after adding mocks to the schema and therefore have both real validationResolvers with mock application resolvers. But it doesn't appear to work.

Is there a way to achieve this?

I've created a minimal reproduction to illustrate the use case: https://github.com/hobochild/-apollo-validation-directives-repro

Thanks again

Hello @hobochild,

After some investigation we concluded that you can use the SchemaDirectiveVisitor from graphql-tools/utils to revisit the whole schema after it was mocked. Doing this, it will repopulate the needed resolvers and everything will work as expected.
I'm attaching a diff in this comment, which "fixes" the example that you provided to us.
Please, let me know if you need help.

diff --git a/package.json b/package.json
index 8376271..eb29d7d 100644
--- a/package.json
+++ b/package.json
@@ -5,6 +5,7 @@
   "main": "schema.js",
   "dependencies": {
     "@graphql-tools/mock": "^6.2.4",
+    "@graphql-tools/utils": "^6.2.4",
     "@profusion/apollo-validation-directives": "^2.0.3",
     "apollo-server-express": "^2.18.2"
   },
diff --git a/schema.js b/schema.js
index 1c79ac5..5de09c0 100644
--- a/schema.js
+++ b/schema.js
@@ -18,6 +18,10 @@ const resolvers = {
   }
 };

+export const schemaDirectives = {
+  pattern,
+}
+
 const schema = makeExecutableSchema({
   typeDefs: [
     typeDefs,
@@ -25,9 +29,7 @@ const schema = makeExecutableSchema({
     ...pattern.getTypeDefs()
   ],
   resolvers,
-  schemaDirectives: {
-    pattern: pattern
-  }
+  schemaDirectives,
 });

 export default schema;
diff --git a/schema.test.js b/schema.test.js
index 39962d1..5b92244 100644
--- a/schema.test.js
+++ b/schema.test.js
@@ -1,5 +1,6 @@
 import { addMocksToSchema } from "@graphql-tools/mock";
-import schema from "./schema";
+import { SchemaDirectiveVisitor } from '@graphql-tools/utils';
+import schema, { schemaDirectives } from "./schema";
 import { graphql } from "graphql";
 import { ValidateDirectiveVisitor } from "@profusion/apollo-validation-directives";

@@ -14,6 +15,7 @@ beforeAll(() => {
     preserveResolvers: false
   });

+  SchemaDirectiveVisitor.visitSchemaDirectives(schemaWithMocks, schemaDirectives);
   const schemaWithValidators = ValidateDirectiveVisitor.addValidationResolversToSchema(
     schemaWithMocks
   );

Amazing thanks @cabelitos will try it out and report back.

Can confirm this is working as expected. Thanks for the help ๐Ÿฅ‡