Saturn icon indicating copy to clipboard operation
Saturn copied to clipboard

Investigate GraphQL controllers

Open Krzysztof-Cieslak opened this issue 7 years ago • 4 comments

Investigate abstraction for creating GraphQL instead of REST-ish controllers.

Krzysztof-Cieslak avatar Jul 13 '18 22:07 Krzysztof-Cieslak

https://github.com/fsprojects/FSharp.Data.GraphQL may be of interest here.

rmunn avatar Jul 14 '18 09:07 rmunn

Also of interest: https://graphql.org/learn/serving-over-http/

Summary:

  • Single endpoint, usually /graphql, handles all requests
  • Handle either GET or POST
  • For GET, use query string. One parameter named query required, optional parameters can include variables (JSON-encoded string) and operationName (a single string).
  • For POST, request should use application/json content type. Body is JSON with up to three fields: query, variables, and operationName.
    • query is required, but might be specified in the URL query parameters rather than body; both should be accepted and handled.
      • Spec doesn't say what happens if URL has a query parameter named query and JSON body also has a query field. In such a case, I'd suggest going with the behavior of the express-graphql library, which uses let query = urlData.query || bodyData.query; so that URL parameters, if specified, override the body. (Same applies to variables and operationName.
    • variables only required if query contains variables.
    • operationName only required if query contains multiple operations.
  • If application/graphql content type is used instead, POST body should be considered the GraphQL query string (and variables and operationName, if presented, would have to be found in the URL query parameters)
  • Response should always be JSON, in format { "data": { ... }, "errors": [ ... ] }. errors field only present if there were errors, otherwise omitted. data omitted if error happened before query execution began. If query execution started but error prevented response, data should be present but contain null.

More formal spec: http://facebook.github.io/graphql/June2018/

rmunn avatar Jul 15 '18 08:07 rmunn

3 years later, what is the recommended way of doing GraphQL with Saturn now? Is there any? Is it possible (in a production quality/setup)?

AlexeyRaga avatar Aug 09 '21 13:08 AlexeyRaga

@AlexeyRaga Probably the best way right now is not to involve Saturn and use AspNetCore middleware from

  • graphql-dotnet pretty solid and pretty popular but a bit too low-level in my experience (using this in production)
  • hot chocolate newer solution than graphql-dotnet and removes a lot of boiler plate when mapping types (haven't tried it but it looks like it solves lots of issues that graphql-dotnet has)
  • FSharp.Data.GraphQL native F# implementation of a GraphQL server that might need a bit of glue to hook into Saturn but it is also a bit low-level like graphql-dotnet

You will need to experiment with these to see which one suits your needs the best

Zaid-Ajaj avatar Aug 09 '21 14:08 Zaid-Ajaj