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

Support input field directives #638

Closed
confuser opened this issue Feb 15, 2018 · 4 comments
Closed

Support input field directives #638

confuser opened this issue Feb 15, 2018 · 4 comments

Comments

@confuser
Copy link

confuser commented Feb 15, 2018

The current implementation #518 appears to only support query/object type level definitions.

    directive @length(max: Int!) on FIELD | FIELD_DEFINITION | INPUT_FIELD_DEFINITION

    type Query {
        books: [Book]
      }
      type Book {
        title: String
      }
      type Mutation {
        createBook(input: BookInput): Book
      }
      input BookInput {
        title: String! @length(max: 10)
      }

When executing a createBook mutation, the length directive resolver never executes. Use case would be to add more granular validation rules via directives. Can be done within the resolver itself, but personally, it feels cleaner to include these at the schema level.

It would be amazing if this was supported.

Happy to open a PR, but fairly new to Typescript, some pointers would be appreciated.

Seems to be due to https://github.com/apollographql/graphql-tools/blob/e1b346aad05f7e356d2cfcd2e619ab9904a81118/src/schemaGenerator.ts#L250-L259 not catering for GraphQLInputObjectType, but obviously this function is used in multiple places...

@benjamn
Copy link
Contributor

benjamn commented Feb 16, 2018

PR #640 will make this pretty easy:

import { SchemaDirectiveVisitor } from "graphql-tools";

SchemaDirectiveVisitor.visitSchemaDirectives(schema, {
  length: class extends SchemaDirectiveVisitor {
    public visitInputFieldDefinition(field: GraphQLInputField) {
      // This LimitedLengthType should be just like field.type except that the
      // serialize method enforces the length limit. For more information about
      // GraphQLScalar type serialization, see the graphql-js implementation:
      // https://github.com/graphql/graphql-js/blob/31ae8a8e8312494b858b69b2ab27b1837e2d8b1e/src/type/definition.js#L425-L446
      field.type = new LimitedLengthType(field.type, this.args.max);
    }
  }
});

I'm leaving the implementation of LimitedLengthType as an exercise for another time, but I hope the rest of this code demonstrates how to use the SchemaDirectiveVisitor abstraction.

@confuser
Copy link
Author

Thanks! Was able to implement this using the SchemaDirectiveVisitor feature, https://github.com/confuser/graphql-constraint-directive for reference

@stubailo
Copy link
Contributor

@confuser wow this is really neat - do you want to write a blog post about this package for the Apollo blog? I think a lot of people would be looking for something like this.

@stubailo
Copy link
Contributor

Also if you reply here I'm likely to miss it - if you are interested, perhaps DM me on twitter or email me at sashko@apollographql.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
Development

No branches or pull requests

3 participants