JCMais/graphql-yup-middleware

Support newer versions of graphql-middleware

Closed this issue · 7 comments

Hi,

I tried to use this package with graphql-middleware@4.x (latest) and I was getting an error, something like schema.validate is not a function. Then I saw on peerDependencies that you are supporting version below 2, which dates from 2 years ago.

Could we upgrade to newer versions of graphql-middleware? I can help with that.

Actually it is trying to execute a validate here, but in runtime my schema looks like this:

{
  input: ObjectSchema {
    _deps: [],
    _conditions: [],
    _options: { abortEarly: true, recursive: true },
    _exclusive: {},
    _whitelist: RefSet { list: Set {}, refs: Map {} },
    _blacklist: RefSet { list: Set {}, refs: Map {} },
    tests: [],
    transforms: [ [Function: coerce] ],
    _mutate: false,
    _typeError: [Function: validate] {
      TEST_NAME: 'typeError',
      TEST_FN: [Function: test],
      TEST: [Object]
    },
    _defaultDefault: [Function: _default],
    _type: 'object',
    fields: {
      name: [StringSchema],
      password: [StringSchema],
      email: [StringSchema],
      username: [StringSchema]
    },
    _nodes: [ 'username', 'email', 'password', 'name' ],
    _excludedEdges: []
  }
}

Could you post how are you defining the validation schema?

I already figured it out and it is working for me.

import * as Yup from 'yup';
import { GraphQLString, GraphQLNonNull } from 'graphql';
import { mutationWithClientMutationId } from 'graphql-relay';

// <some imports>

import { mutationValidationSchema } from '../../../common/validation';
import type { ValidationSchema } from '../../../common/validation';

import UserValidation from '../UserValidation';

export interface UserAddArgs {
  name: string;
  email: string;
  password: string;
  username: string;
}

const inputValidationSchema: ValidationSchema<UserAddArgs> = {
  name: Yup.string(),
  email: Yup.string().matches(UserValidation.email.regex, UserValidation.email.message),
  password: Yup.string().matches(UserValidation.password.regex, UserValidation.password.message),
  username: Yup.string().matches(UserValidation.username.regex, UserValidation.username.message),
};

const GraphQLStringNonNull = GraphQLNonNull(GraphQLString);

const mutation = mutationWithClientMutationId({
  name: 'UserAdd',
  inputFields: {
    name: { type: GraphQLStringNonNull },
    email: { type: GraphQLStringNonNull },
    password: { type: GraphQLStringNonNull },
    username: { type: GraphQLStringNonNull },
  },
  mutateAndGetPayload: async (args: UserAddArgs, _: GraphQLContext) => {
    // ...
  },
  outputFields: {
    // ...
  },
});

export default {
  ...mutation,
  validationSchema: mutationValidationSchema(inputValidationSchema),
};

I was saying maybe we could bump peerDeps, as graphql v15 is not supported (yet), etc...

I will look into that this weekend. Thanks for letting me know about the peer dep.

gajus commented

Any plans on executing this?

@gajus this is already available at graphql-yup-middleware@next, I have not released it yet.

graphql-yup-middleware@1.0.0 has been released with support to (only) GraphQL v15 using the extensions field + the most recent version of graphql-middleware.