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

Error in GraphQL examples #373

Open
Ecsodikas opened this issue Dec 28, 2024 · 0 comments
Open

Error in GraphQL examples #373

Ecsodikas opened this issue Dec 28, 2024 · 0 comments

Comments

@Ecsodikas
Copy link

Hi everyone,

I was working through the graphql examples and I found out that the type system is not able to resolve all the types given in the example (https://github.com/aantron/dream/tree/master/example/i-graphql), so it does not compile.

open Graphql_lwt

type user = {id : int; name : string}

let hardcoded_users: user list = [
  {id = 1; name = "alice"};
  {id = 1; name = "bob"};
]

let user =
  Graphql_lwt.Schema.(obj "user"
    ~fields:[
      field "id"
        ~typ:(non_null int)
        ~args:Arg.[]
        ~resolve:(fun _info (user : user) -> user.id);
      field "name"
        ~typ:(non_null string)
        ~args:Arg.[]
        ~resolve:(fun _info (user : user) -> user.name);
    ])

let schema =
  Graphql_lwt.Schema.(schema [
    field "users"
      ~typ:(non_null (list (non_null user)))
      ~args:Arg.[arg "id" ~typ:int]
      ~resolve:(fun _info () id ->
        match id with
        | None -> hardcoded_users
        | Some id' ->
          match List.find_opt (fun {id; _} -> id = id') hardcoded_users with
          | None -> []
          | Some user -> [user]);
  ])

Produces the following error:

| let user =
         ^^^^
Error: The type of this expression,
       ('_weak1, user option) Graphql_lwt.Schema.typ,
       contains the non-generalizable type variable(s): '_weak1.
       (see manual section 6.1.2)

The solution is to make user and schema a function to solve the issue.

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

1 participant