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 Directives on Input Types #511

Closed
sdalezman opened this issue Jan 24, 2019 · 11 comments
Closed

Support Directives on Input Types #511

sdalezman opened this issue Jan 24, 2019 · 11 comments
Labels
enhancement New feature or request needs docs stale v0.10 For release in v0.10

Comments

@sdalezman
Copy link

sdalezman commented Jan 24, 2019

Expected Behaviour

When defining a graphql schema you should be able to define a directive on an entire input object or an input field

Actual Behavior

Can't define any directive on a input field

Minimal graphql.schema and models to reproduce

directive @hello on INPUT_OBJECT

type Query {
   repeatLogName(n: Name!): String
}

input Name {
   name: String!
}

extend input Name @hello

where the directive evaluates to the input from the logging example:

func InputLogging(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
	rc := graphql.GetResolverContext(ctx)
	log.Printf("input object logging: %v, %s, %T, %+v", rc.Path(), rc.Field.Name, obj, obj)
	return next(ctx)
}

Input logging will never be called. I think this has to do with how FieldMiddleware calls directives, but this is only from a cursory glance at the generated code

@mathewbyrne
Copy link
Contributor

I do not believe that we currently support directives on Input types, partly because I think it's a little unclear how/when they would be invoked.

As you suggest they could definitely be invoked whenever an input is used, but even then it's not 100% clear (e.g. would a variable input directive be invoked at runtime just once, or each time the variable is used).

But I could also imagine use-cases for code-gen time invocation, e.g. mapping an input type to a Go type.

We do note this in the directive docs but we could be clearer about what specific locations we support directives and what happens if you implement them.

@mathewbyrne mathewbyrne added the enhancement New feature or request label Jan 28, 2019
@mathewbyrne mathewbyrne changed the title Input Directive never called Support Directives on Input Types Jan 28, 2019
@sdalezman
Copy link
Author

@mathewbyrne the reason I thought it was supported was because in the example of the type system extension there's an INPUT_OBJECT: https://github.com/99designs/gqlgen/blob/master/example/type-system-extension/schemas/input-object-extension.graphql

Searching through the generated code it didn't seem to ever be called, but thought it might've been my lack of understanding the generated code entirely

I understand your comments about when/how it should be called. For us, adding in some sort of input property/input object directive would be great. It would allow us to not have to add function calls to ensure that a given user has permission to filter/query by various fields (from a security perspective that adds onus to a developer to ensure they remember to add those checks as well as the reviewer to ensure that they're obvious- we do that and have tests for those flows, but reducing the mental burden of rapidly iterating on api's is always a plus in my book).

That way we can have a handful of types and know that they can always be used securely and reduce the exposure of surface areas

@vektah vektah added the v0.9 Fixed in 0.9.0 label Feb 7, 2019
@sdalezman
Copy link
Author

sdalezman commented Feb 14, 2019

@vektah @mathewbyrne our team would be happy to tackle this issue. If you can provide a bit more design guidance so we don't go down the wrong rabbit hole. We were thinking that you can have two types of input directives:

  1. Input directive that's called before any resolvers are called
  2. Input directive that's called after resolvers are called

Which I think would cover 80% of use cases directives for inputs. If your team agrees on that we'd be happy to start working on this (and save us a tremendous headache at the moment)

@mathewbyrne
Copy link
Contributor

Hey @sdalezman, sorry it took a bit to get back to you. We'd been keen to support Directive Middlewares on Input objects. I think it would work more or less the same as fields, where when the argument is resolved for a given input type, the middleware stack is run.

Obviously these directives will have the same interface as other directives, and be supplied through the generated directive root.

We'd be happy to give feedback on a proposal issue if you think the scope of the change warrants. Please be sure to work against the next branch for this change.

@steebchen
Copy link
Contributor

steebchen commented Mar 12, 2019

Somehow, my directive works on INPUT_FIELD_DEFINITION, but I'm not able to get its value or name:

directive @can(action: Action!) on INPUT_FIELD_DEFINITION

type Mutation {
	register(payload: RegisterPayload!): AuthResult!
}

input RegisterPayload {
	email: String!
	password: String!
	referrer: String @can(action: Create)
}

allows me implement this directive function:

func can(
	ctx context.Context,
	obj interface{},
	next graphql.Resolver,
	action gqlgen.Action,
) (interface{}, error) {
	rc := graphql.GetResolverContext(ctx)
	log.Printf("field %s has value %#v", rc.Field.Name, obj)
  return next(ctx)
}

prints field register has value gqlgen.RegisterPayload{Email:"customer", Password:"", Name:(*string)(nil)}

I would have expected to get field referrer has value <nil> (provided that referrer was set to "").

Is there currently a way to do that?

@vektah vektah added v0.10 For release in v0.10 and removed v0.9 Fixed in 0.9.0 labels May 15, 2019
@stale
Copy link

stale bot commented Aug 28, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Aug 28, 2019
@stale stale bot closed this as completed Sep 4, 2019
@swkumar
Copy link

swkumar commented Jan 16, 2020

hi @steebchen: were you able to get the value or name for the INPUT_FIELD_DEFINITION directive?

@steebchen
Copy link
Contributor

steebchen commented Jan 16, 2020

@swkumar I'm not working on the project anymore, but I don't think I was. I worked around it. @vektah Can you please re-open this and mark as not stale?

@swkumar
Copy link

swkumar commented Jan 16, 2020

@steebchen : thanks for the response. May I ask what was your workaround for this? Thanks!

@steebchen
Copy link
Contributor

Sorry, that was unclear: I just didn't use directives for my use case anymore :P

@swkumar
Copy link

swkumar commented Mar 9, 2020

Looks like this will be worked on here: #1084

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs docs stale v0.10 For release in v0.10
Projects
None yet
Development

No branches or pull requests

5 participants