andreas / ocaml-graphql-server

GraphQL servers in OCaml
MIT License
624 stars 60 forks source link

self recursive root query type not possible #164

Closed despairblue closed 5 years ago

despairblue commented 5 years ago

Hey I'm trying to add an ocaml implementation to some benchmark. The benchmark wants the server to implement a schema like that:

    schema {
        query: Query
    }

    interface Base {
        id: String
    }

    type Query implements Base {
        id: String
        string: String
        listOfStrings: [String]
        listOfObjects: [Query]
        listOfInterfaces: [Base]
    }

Since Schema.schema only takes a list of fields I cannot use the query type when creating the fields. The closest I got was this:

schema {
  query: query
}

interface Base {
  id: String
}

type query {
  query: Query
}

type Query {
  id: String
  string: String
  listOfStrings: [String]
  listOfObjects: [Query]
  listOfInterfaces: [Base]
}

I then tried using the query_name parameter of Schema.schema to change the root query's name to Query, but that just short circuits the schema:

type Query {
  query: Query
}

I'm currently looking into the code of the schema function, but I think I would need to change the execute function as well :/

Am I missing something and that is actually possible without changing the code? I mean I could also change the benchmark of course ^^

andreas commented 5 years ago

Hi @despairblue,

Interesting with the benchmark πŸ˜„ You're right that it's currently not possible to have a self-recursive Query type with ocaml-graphql-server. The current approach solves some typing-related issues, and it hasn't really come up as an issue for any real-world use case yet. If you want to hack it, you can update the function schema to accept a value of type ('ctx, unit option) typ as the query type (you will need to raise if this value is not an object).

Hope it helps. Would be curious to see the results πŸ˜„

despairblue commented 5 years ago

Thanks, the results are here: https://github.com/ruiaraujo/gbench/pull/1

I only run the benchmark for scalars, lists and the introspection one for now. You can check the server here if you have any suggestions, but it's in reason, I can convert it to ocaml though if you like.

I think rewriting the benchmark is actually the better option than making the implementation more complicated. I also don't see any real world use case for this. :smiley:

andreas commented 5 years ago

Thanks for sharing πŸ˜„ I'll close this for now then.