Skip to content

Conversation

dwwoelfel
Copy link
Contributor

@dwwoelfel dwwoelfel commented Oct 11, 2018

I had a really hard time discovering the source of an error for a script that was making GraphQL queries to OneGraph. The error was "Invalid string", but I had multiple string arguments.

With this change, the error I get back is Argument `pos` of type `String` expected on field `updateCard`, found 1.

With that error, there is enough context for me to easily pinpoint the problem and fix it.

If you'd like an easy way to test, this change is live on OneGraph:

curl 'https://serve.onegraph.com/dynamic?app_id=0b33e830-7cde-4b90-ad7e-2a39c57c0e11'  \
    --data-binary '{"query":"{ trello { card { name }}}"}'

> {"data":null,"errors":[{"message":"Argument `id` of type `String!` expected on field `card`, but not provided."}]}

Copy link
Owner

@andreas andreas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! ❤️ Thanks a lot for the PR.

I've made some comments related to style.


let rec string_of_const_value: Graphql_parser.const_value -> string = function
| `Null -> "null"
| `Int i -> string_of_int(i)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd consider it more idiomatic to omit parenthesis when not required, e.g. string_of_int i here. Similarly elsewhere.

let obj ?doc name ~fields ~coerce =
Object { name; doc; fields; coerce }

let rec string_of_const_value: Graphql_parser.const_value -> string = function
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: space before the colon.

| `Bool b -> string_of_bool(b)
| `Enum e -> e
| `List l -> Printf.sprintf "[%s]" (String.concat ", " (List.map (fun i -> (string_of_const_value i)) l))
| `Assoc a -> Printf.sprintf "{%s}" (String.concat ", " (List.map (fun (k, v) -> Printf.sprintf "%s: %s" k (string_of_const_value v)) a))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly extract (List.map ...) to a let-expression (similarly above).

| Object(a) -> a.name
| Enum(a) -> a.name
| List(a) -> (Printf.sprintf "[%s]" (string_of_arg_typ a))
| NonNullable(a) -> (Printf.sprintf "%s!" (string_of_arg_typ a))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would omit parenthesis in the match cases (e.g. Scalar a) and around Printf.sprintf ....

field_name
(match value with
| Some(v) -> Printf.sprintf "found %s" (string_of_const_value v)
| None -> "but not provided")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would extract this to a let-expression.

with StringMap.Missing_key key -> Error (Format.sprintf "Missing variable `%s`" key)

and eval_arg : type a. variable_map -> a arg_typ -> Graphql_parser.const_value option -> (a, string) result = fun variable_map typ value ->
and eval_arg : type a. variable_map -> string -> string -> a arg_typ -> Graphql_parser.const_value option -> (a, string) result = fun variable_map field_name arg_name typ value ->
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd consider adding labels to avoid mixing up the arguments as they have the same type (i.e. is field name first or last on invocation).

| Ok coerced -> Ok (Some coerced)
| Error _ -> Error (eval_arg_error field_name arg_name typ (Some value))
in
result
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively:

begin match s.coerce value with
...
end

@dwwoelfel
Copy link
Contributor Author

Thanks @andreas! Updated per your comments.

Any chance we could use ocamlformat on this project? https://github.com/ocaml-ppx/ocamlformat

@andreas andreas merged commit 4367e7f into andreas:master Oct 12, 2018
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.

2 participants