F# with Http.fs - not able to execute GraphQL APIs

128 views Asked by At

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/

1

There are 1 answers

2
Tomas Petricek On BEST ANSWER

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": "...."}.