Giter Site home page Giter Site logo

idkjs / graphql-to-reason Goto Github PK

View Code? Open in Web Editor NEW

This project forked from coobaha/graphql-to-reason

0.0 0.0 0.0 384 KB

transform GraphQL schema to ReasonML types

License: MIT License

Makefile 0.03% OCaml 99.67% JavaScript 0.27% Shell 0.03%

graphql-to-reason's Introduction

graphql-to-reason

This tool will transform existing GraphQL schema to ReasonML types to be used on server side.

Build Status npm

Examples

Installation

First, add this package as a dependency to your package.json

yarn add --dev graphql-to-reason
# or, if you use npm:
npm install -D graphql-to-reason

graphql-to-reason requires json variant (aka introspection query) of schema.graphql.

schema.json can be generated with npx graphql-to-reason-schema schema.graphql schema.json.

Integration with Bucklescript can be done via generators

a) With already introspected schema

{
  "generators": [
    {
      "name": "generate-schematypes",
      "command": "npx graphql-to-reason $in $out"
    }
  ],
  "sources": [
    {
      "dir": "src",
      "generators": [
        {
          "name": "generate-schematypes",
          "edge": [ "SchemaTypes_builder.re", ":", "schema.json"]
        }
      ]
    }
  ]
}

b) From .graphql

{
  "generators": [
    {
      "name": "generate-schematypes",
      "command": "npx graphql-to-reason-schema $in $in.json  && npx graphql-to-reason $in.json $out"
    }
  ],
  "sources": [
    {
      "dir": "src",
      "generators": [
        {
          "name": "generate-schematypes",
          "edge": ["SchemaTypes_builder.re", "schema.graphql.json", ":", "schema.graphql"]
        }
      ]
    }
  ]
}

Examples

Simple schema.graphql

scalar Click

type Query {
    clicks: Click!
}

type Mutation {
    click(payload: String!): Click!
}

Next we generate ReasonML code from it: npx graphql-to-reason schema.json SchemaTypes_builder.re

It will output SchemaTypes_builder.re to use it in other modules:

include SchemaTypes_builder.MakeSchema({
  /* we need to configure our server types:
      - all scalar types
      - resolver type
      - custom directive resolver type  */
  module Scalars = {
    type click = int;
  };

  /* args - our arguments Array
     fieldType - original field type
     result - resoved value (for example Js.Nullable.t(fieldType)) */
  type resolver('args, 'fieldType, 'result) =
    (
      unit,
      'args,
      /*context args depend on your graphql setup*/
      ServerContext.t,
      ServerContext.ResolveInfo.t,
      ServerContext.FieldInfo.t('fieldType)
    ) =>
    Js.Promise.t('result);
  type directiveResolver('payload);
});


module Clicks = {
  let count = ref(0);
  /* No need to explicitly type resolver, it will infer correct type later */
  let resolver = (_node, _args, _context, _resolveInfo, _fieldInfo) =>
    Js.Promise.make((~resolve, ~reject) => {
      count := count^ + 1;
      resolve(count^);
    });
};


/* Clicks.resolver now infers SchemaTypes.Mutation.clicksCount type */
let mutationResolvers =
  SchemaTypes.Mutation.t(~clicksCount=Clicks.resolver, ());

let resolvers = SchemaTypes.t(~mutation, ());

createMyGraphqlServer(resolvers);

Developing

Install esy:

npm install -g esy@latest

Install dependencies:

make install

To build executables:

make build

To run tests:

make test

graphql-to-reason's People

Contributors

coobaha avatar matthiaskern avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.