0

I don't see any good documentation about how to execute GraphQL APIs using F# with Http.fs

Kindly share if you have the correct syntax available or point to the correct documentation for the same. I was trying with the Star Wars API given here: https://www.rithmschool.com/blog/an-introduction-to-graphql-queries

URL: https://swapi.graph.cool
Header: 'Content-Type': 'application/json' 
JSON Body: 
query {
  Film (title:"A New Hope" ) {
    director
    characters {
      name
    }
  }
}

Expected Response same as: https://swapi.graph.cool/

  • Have you looked at the source code? I recently used Http.fs for API requests quite successfully. Here's an example of a POST request: https://github.com/haf/Http.fs/blob/master/HttpFs.SamplePostApplication/Program.fs – Piotr Rodak Jun 16 '20 at 21:04
  • I am also using Http.fs for my API test and it works like a charm. My query was mainly related to GraphQL where we are sending a request in the form of Query. Please do share if you have a working example of Http.fs with GraphQL. – Sanjay Kumar Jun 17 '20 at 04:51

1 Answers1

1

I'm not familiar with Http.fs, but here is a small working example of calling the API using the F# Data Http utility:

Http.RequestString
  ( "https://swapi.graph.cool", 
    httpMethod="POST", headers=[ HttpRequestHeaders.ContentType("application/json") ],
    body=TextRequest("{\"query\": \"{ allFilms { title } }\"}") )

The main thing is that the body needs to be a JSON value where the actual query is a string stored in a record with a field named "query", i.e. {"query": "...."}.

adelarsq
  • 3,718
  • 4
  • 37
  • 47
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • This helps for sure but I am more inclined towards using Http.fs as compared to FSharp.Data as I have written my other API tests using Http.fs. If I don't get anything for Http.fs than I'll have no other option but to use FSharp.Data for all my GraphQL Apis. – Sanjay Kumar Jun 16 '20 at 13:51
  • 1
    I think you should be able to adapt this - it's just a matter of setting the body and headers... – Tomas Petricek Jun 16 '20 at 19:11