JCMais/graphql-yup-middleware

Graphql-yup-middleware doesn't run to check Yup errors

Closed this issue · 4 comments

Hi. I am trying to wire in graphql-yup-middleware to my project but I can't seem to make it work

I'm using Graphql ^v15.0.0

If a book title entered is less than 5 characters length, it should throw a Yup error. In my GraphQL playground, I have entered a book title only 2 characters long but no Yup error was thrown. My app is still working just that it is unable to validate using graphql-yup-middleware

I am also using graphql-resolvers as a middleware to check if a user is authenticated and I'm not sure if that could be an issue too. Will it play nicely with. graphql-yup-middleware?

Have i missed out anything in the setup in order to use graphql-yup-middleware in my project? Thanks

index.js

  const schema = makeExecutableSchema({
    typeDefs,
    resolvers,
  });

  const schemaWithYupMiddleware = applyMiddleware(schema, yupMiddleware());

  const server = new ApolloServer({
    validationRules: [depthLimit(7)],
    schema: schemaWithYupMiddleware,
    context: async ({ req, res }) => ({
      req,
      res,
    }),
  });

GraphQL Playground

mutation AddValidatedBook {
  addValidatedBook(title: "AI") // title of book should be 5 characters length so should throw validation error
}

bookTypeDefs.js

  extend type Mutation {
    addValidatedBook(title: String!): Boolean!
  }

bookResolver.js

const validateBookSchema = Yup.object().shape({
  title: Yup.string().required().min(5),
});

    addValidatedBook: {
      extensions: {
        yupMiddleware: {
          validationSchema: validateBookSchema,
        },
      },
      resolve: async (_, args) => {
        return true;
      },
    },

which version of graphql-yup-middleware are you using? Support for GraphQL v15 was added on the pre release version available at graphql-yup-middleware@next.

Thanks for your prompt response. I used v0.01. I upgraded the version to graphql-yup-middleware@next and it is able to work. On another note, if there is more than 1 error, the error field states "2 errors occurred". Is there any way for graphql playground to display the errors?

You need to use the MutationValidationError error object as return type for the error field instead of a single string.

Thanks @JCMais . It works! Tq