-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from baransu/feature/better-args-validation
Validate required arguments on fields
- Loading branch information
Showing
8 changed files
with
142 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Visitor: Traversal_utils.VisitorSig = { | ||
open Traversal_utils; | ||
open Source_pos; | ||
open Graphql_ast; | ||
open Schema; | ||
|
||
include AbstractVisitor; | ||
|
||
let enter_field = (_self, ctx, def) => { | ||
let field_meta = | ||
Context.parent_type(ctx) | ||
|> Option.flat_map(t => Schema.lookup_field(t, def.item.fd_name.item)); | ||
|
||
let provided_args = | ||
def.item.fd_arguments | ||
|> Option.map(span => span.item) | ||
|> Option.get_or_else([]) | ||
|> List.map(arg => (arg |> fst).item); | ||
|
||
let expected_args = | ||
field_meta | ||
|> Option.map(fm => fm.fm_arguments) | ||
|> Option.get_or_else([]) | ||
|> List.filter(arg => | ||
switch(arg.am_arg_type) { | ||
| NonNull(_) => true | ||
| _ => false | ||
}); | ||
|
||
expected_args | ||
|> List.iter(arg => { | ||
let provided = provided_args |> List.exists(arg_name => arg_name == arg.am_name); | ||
if(!provided) { | ||
let message = | ||
Printf.sprintf("Argument \"%s\" on field \"%s\" not provided", arg.am_name, def.item.fd_name.item); | ||
|
||
Context.push_error(ctx, def.span, message); | ||
} | ||
}); | ||
}; | ||
|
||
type t = unit; | ||
let make_self = () => (); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
open Jest; | ||
open Expect; | ||
|
||
module MyQuery = [%graphql {| | ||
mutation MyMutation($required: String!) { | ||
optionalInputArgs(required: $required, anotherRequired: "val") | ||
} | ||
|}]; | ||
|
||
describe("Mutation with args", () => | ||
test("Printed query is a mutation", () => | ||
MyQuery.query |> Js.String.indexOf("mutation") |> expect |> toBe(0) | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters