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

add execute argument and input directive #460

Merged
merged 19 commits into from
Jan 5, 2019
Merged

add execute argument and input directive #460

merged 19 commits into from
Jan 5, 2019

Conversation

andrey1s
Copy link
Contributor

@andrey1s andrey1s commented Dec 1, 2018

added execution of system directives for ARGUMENT_DEFINITION and INPUT_FIELD_DEFINITION

generating code for arguments for example when adding a Directive instead of

	if tmp, ok := rawArgs["Id"]; ok {
		var err error
		arg0, err = graphql.UnmarshalID(tmp)
		if err != nil {
			return nil, err
		}
	}

will be

	if tmp, ok := rawArgs["Id"]; ok {

		argm0, err := chainFieldMiddleware([]graphql.FieldMiddleware{
			func(ctx context.Context, n graphql.Resolver) (res interface{}, err error) {
				min := "0"
				return e.directives.AssertRange(ctx, tmp, n, &min, nil, nil)
			},
		}...)(ctx, func(ctx2 context.Context) (args0 interface{}, err error) {
			args0, err = graphql.UnmarshalID(tmp)
			if err != nil {
				return nil, err
			}
			return
		})
		if err != nil {
			return nil, err
		}
		if data, ok := argm0.(string); ok {
			arg0 = data
		} else {
			return nil, errors.New("expect string")
		}

	}

if the argument is type input, the code will be added in addition to the previous code and the previous code will be added (for example)

	if tmp, ok := rawArgs["input"]; ok {

		var err error
		arg0, err = UnmarshalDataInput(tmp)
		if err != nil {
			return nil, err
		}

		arg0, err := e.DataInputMiddleware(ctx, arg0)
		if err != nil {
			return nil, err
		}
	}

and added input middleware(for example) or empty middleware

func (e *executableSchema) DataInputMiddleware(ctx context.Context, obj *models.DataInput) (*models.DataInput, error) {

	if obj.InternalData != nil {
		var err error
		obj.InternalData, err = e.CoordinatesInputMiddleware(ctx, obj.InternalData)
		if err != nil {
			return nil, err
		}
	}

	return obj, nil
}

@mathewbyrne
Copy link
Contributor

Thanks for the PR, I'll try to review it as I get time. It's a pretty big change though, a bit more description would be appreciated as to how you've implemented this and how it affects the current directives implementation.

@andrey1s
Copy link
Contributor Author

andrey1s commented Dec 3, 2018

update description
the current implementation (master) only supports directives for FIELD_DEFINITION and Executable directives, and does not support arguments. I left them unchanged, I hope we'll share them via the Location Directive in the future

@vektah
Copy link
Collaborator

vektah commented Dec 8, 2018

This is a large PR to drop without any discussion beforehand :( We have been thinking to cover this use case (validation) via a codegen plugin rather than getting the user to wire them up for each graph.

I'm getting lots of compile errors if I add a directive to some args in starwars:

directive @length(min: Int!, max: Int, message: String!) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION

# The query type, represents all of the entry points into our object graph
type Query {
    hero(episode: Episode = NEWHOPE @length(min: 2, max: 5)): Character
    reviews(episode: Episode! @length(min: 2, max: 5), since: Time): [Review!]!
# ...
type Starship {
    # The ID of the starship
    id: ID!
    # The name of the starship
    name: String! @length(min: 2, max: 5)
go generate ./example/starwars/
warning: running gqlgen from this binary is deprecated and may be removed in a future release. See https://github.com/99designs/gqlgen/issues/415
/home/adam/go/src/github.com/99designs/gqlgen/example/starwars/generated.go:308:19: invalid operation: (&args0) (value of type *interface{}) has no field or method UnmarshalGQL
/home/adam/go/src/github.com/99designs/gqlgen/example/starwars/generated.go:305:54: cannot convert nil (untyped nil value) to string
/home/adam/go/src/github.com/99designs/gqlgen/example/starwars/generated.go:268:54: cannot convert nil (untyped nil value) to string

@vektah vektah changed the base branch from master to next December 8, 2018 00:25
@andrey1s
Copy link
Contributor Author

andrey1s commented Dec 8, 2018

cannot convert nil (untyped nil value) to string

invalid schema
directive @length(min: Int!, max: Int, message: String!)
message MUST NOT be null
@length(min: 2, max: 5, message:"not valid")
and if not set ARGUMENT_DEFINITION schema invalid, but there is no error

and for example if use @hasRole from gqlgen/example/todo/schema.graphql without role MUST be error when generate go files, but its working as if you were the owner

invalid operation: (&args0) (value of type *interface{}) has no field or method UnmarshalGQL

updated generate args

@vektah vektah merged commit f9ee6ce into 99designs:next Jan 5, 2019
@vektah vektah mentioned this pull request Mar 4, 2019
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

Successfully merging this pull request may close these issues.

3 participants