Last active
February 25, 2018 14:30
-
-
Save idkjs/6e3fa8b163f2f6e5e33b8e5f4bac46a3 to your computer and use it in GitHub Desktop.
must provide query string error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[%%raw {|require('isomorphic-fetch')|}]; | |
open Js.Promise; | |
module FilmQuery = [%graphql {| | |
{ | |
allFilms { | |
films { | |
id | |
title | |
releaseDate | |
} | |
} | |
} | |
|}]; | |
exception Graphql_error(string); | |
/* Construct a "packaged" query; FilmQuery takes no arguments: */ | |
let filmQuery = FilmQuery.make(); | |
/* Send this query string to the server */ | |
let query = filmQuery##query; | |
let sendQuery = (q) => | |
Fetch.( | |
fetchWithInit( | |
"http://swapi.apis.guru/", | |
RequestInit.make( | |
~method_=Post, | |
~body= | |
Js.Dict.fromList([("query", Js.Json.string(q##query)), ("variables", q##variables)]) | |
|> Js.Json.object_ | |
|> Js.Json.stringify | |
|> BodyInit.make, | |
~credentials=Include, | |
~headers=HeadersInit.makeWithArray([|("content-type", "application/json")|]), | |
() | |
) | |
) | |
|> Js.Promise.then_(json => Response.json(json) |> resolve) | |
|> Js.Promise.then_(data => Js.log(data) |> resolve) | |
|> Js.Promise.catch(err => resolve(Js.log(err))) | |
); | |
Js.log(query); | |
sendQuery(query); | |
/* | |
- error | |
[bucklescript] | |
This has type: | |
string | |
But somewhere wanted: | |
Js.t({.. query : string, variables : Js.Json.t }) | |
string | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment