https://github.com/rricard/bs-graphql-bindings
BuckleScript binding for graphql-js
https://github.com/rricard/bs-graphql-bindings
apollo bucklescript graphql ocaml reasonml
Last synced: 12 months ago
JSON representation
BuckleScript binding for graphql-js
- Host: GitHub
- URL: https://github.com/rricard/bs-graphql-bindings
- Owner: rricard
- License: mit
- Created: 2017-08-19T20:12:49.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-18T02:04:59.000Z (over 8 years ago)
- Last Synced: 2025-04-06T21:46:45.422Z (12 months ago)
- Topics: apollo, bucklescript, graphql, ocaml, reasonml
- Language: OCaml
- Size: 64.5 KB
- Stars: 50
- Watchers: 4
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# BuckleScript GraphQL bindings
[](https://circleci.com/gh/rricard/bs-graphql-bindings)
This repository stores a collection of [BuckleScript](https://bucklescript.github.io/) bindings for various, widely used, [GraphQL](http://graphql.org/) projects.
## Packages
The following packages are contained in this repository and published on npm:
| Package | Binds to | NPM | Description |
|---------|----------|-----|-------------|
| [`bs-graphql`](./packages/bs-graphql) | [`graphql-js`](https://github.com/graphql/graphql-js/) | [](https://badge.fury.io/js/bs-graphql) | Core GraphQL bindings over the JS reference implementation |
| [`bs-graphql-tools`](./packages/bs-graphql-tools) | [`graphql-tools`](https://github.com/apollographql/graphql-tools/) | [](https://badge.fury.io/js/bs-graphql-tools) | Bindings for the Apollo GraphQL server tools |
| [`bs-apollo-server-express`](./packages/bs-apollo-server-express) | [`apollo-server-express`](https://github.com/apollographql/apollo-server/) | [](https://badge.fury.io/js/bs-apollo-server-express) | Bindings to mount an apollo server on express |
> **WARNING**: Most of those bindings are **partial** for now! Please consult each binding's readme for more information.
## Guides
### Your first Reason-GraphQL server
**TL;DR;**: This repo has it all with a few interesting things more (such as snapshot testing with jest): https://github.com/rricard/graphql-reason-server-example.
This short guide will show you how to setup a GraphQL server fast.
First, you will need a reason codebase:
```sh
npm install -g bs-platform
bsb -init my-graphql-server -theme basic-reason
cd my-graphql-server
yarn install || npm install
```
Now, you need to install the packages here and graphql as well as express:
```
yarn add graphql express body-parser \
bs-express bs-graphql bs-apollo-server-express bs-graphql-tools || \
npm install --save graphql express body-parser \
bs-express bs-graphql bs-apollo-server-express bs-graphql-tools
```
Add the bindings in `bsconfig.json`:
```json
{
"name": "my-graphql-server",
"sources": [
"src"
],
"bs-dependencies" : [
"bs-express",
"bs-graphql",
"bs-graphql-tools",
"bs-apollo-server-express"
]
}
```
We're starting the development, you should run a watch process: `yarn watch || npm run watch`.
Remove `demo.re` and add the following files:
First we'll define a Reason type alongside an associated GraphQL schema and resolver functions: `Episode.re`:
```reason
type t = {
name: string,
number: int
};
let hope = {name: "A New Hope", number: 4};
let empire = {name: "The Empire Strikes Back", number: 5};
let jedi = {name: "The Return of the Jedi", number: 6};
let getName e => e.name;
let getNumber e => e.number;
let typeDef = "type Episode { name: String!, number: Int! }";
let resolvers = {"name": getName, "number": getNumber};
```
Then, we'll define the base Query type similarly: `Query.re`:
```reason
let getEpisodes () => [|Episode.hope, Episode.empire, Episode.jedi|];
let getFavoriteEpisode () => Episode.empire;
let typeDef = "type Query { episodes: [Episode!]!, favoriteEpisode: Episode! }";
let resolvers = {"episodes": getEpisodes, "favoriteEpisode": getFavoriteEpisode};
```
We can now put together a schema: `Schema.re`:
```reason
let typeDefs = Query.typeDef ^ " " ^ Episode.typeDef;
let resolvers = {"Query": Query.resolvers, "Episode": Episode.resolvers};
let schema = GraphQLTools.makeExecutableSchema {"typeDefs": typeDefs, "resolvers": resolvers};
```
And finally, we can mount a server: `Server.js`:
```reason
let port = 8080;
external bodyParserJson : unit => Express.Middleware.t = "json" [@@bs.module "body-parser"];
let () = {
let app = Express.App.make ();
Express.App.use app (bodyParserJson ());
let graphqlMiddleware = ApolloServerExpress.createGraphQLExpressMiddleware Schema.schema;
let graphiqlMiddleware = ApolloServerExpress.createGraphiQLExpressMiddleware "/graphql";
Express.App.useOnPath app graphqlMiddleware path::"/graphql";
Express.App.useOnPath app graphiqlMiddleware path::"/graphiql";
Express.App.listen app ::port;
Js.log ("GraphQL Server listening on http://localhost:" ^ string_of_int port)
};
```
If your watcher worked so far, you can now run the server and run a query against it:
```sh
node ./lib/js/src/server.js
curl --request POST \
--url http://localhost:8080/graphql \
--header 'content-type: application/json' \
--data '{"query":"{ favoriteEpisode { name, number }}"}'
```
## Contributing
Please read the [CONTRIBUTING](./CONTRIBUTING.md) document if you want to help or report issues.