Skip to content

Instantly share code, notes, and snippets.

@idkjs
Last active February 25, 2018 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idkjs/6e3fa8b163f2f6e5e33b8e5f4bac46a3 to your computer and use it in GitHub Desktop.
Save idkjs/6e3fa8b163f2f6e5e33b8e5f4bac46a3 to your computer and use it in GitHub Desktop.
must provide query string error
[%%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