Giter VIP home page Giter VIP logo

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 object
     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(~clicks=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 dependabot[bot] avatar matthiaskern avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

graphql-to-reason's Issues

Types compatible with Reason native (ie. sans BuckleScript)

Feature Request

Is your feature request related to a problem? Please describe.
I'm trying to use graphql-to-reason to generate types from a schema SDL file which can be used by my server (ocaml-graphql based native project) and my frontend project (ReasonReact + reason-apollo).

Kudos for a light-weight way to get going. Generating types from the SDL file was simple to get going. However the currently generated types rely on BuckleScript, so I'm unable to use them on my backend project without manually patching them.

Describe the solution you'd like
A clear and concise description of what you want to happen. Add any considered drawbacks.

BuckleScript makes the most sense for frontend JS applications. For native though, you'd want to use record syntax rather than Js.t.

The simplest way I can think of to trigger between the two syntaxes is either to have a --native flag or a --syntax=[bs|native] option. The second option is more future proof, but presently I can't think of a possible third syntax option.

I'll report back with what changes I've needed to make to get a simple example server working when I get a simple example server working.

Describe alternatives you've considered
None, though bs-native seems like an interesting option. That was my first attempt when spinning up this project but esy + bs-native is much harder to get going than esy + pesy + dune from my personal experience with one project to the point where I gave up on it. It'd be nice to support non-bucklescript projects any way, in my opinion.

Teachability, Documentation, Adoption, Migration Strategy

This would be a secondary generation method which is completely opt-in. It would not affect current users.

Running example.

Version
Package version
latest
Describe the bug
A clear and concise description of what the bug is.

Building an running the example, here https://github.com/Coobaha/graphql-to-reason/tree/master/examples throw the following error:

> bsb -make-world -w

>>>> Start compiling
[7/7] Building src/SchemaTypes_builder.mlast.d
[2/4] Building src/SchemaTypes-GraphqlToReasonBasicExample.cmj

  We've found a bug for you!
  /Users/prisc_000/Downloads/Coobaha-examples_basic/src/SchemaTypes.re 1:40-6:1

  1 │ include SchemaTypes_builder.MakeSchema({
  2 │   module Scalars = {};
  . │ ...
  5 │   type directiveResolver('payload);
  6 │ });

  Signature mismatch:
  ...
  Type declarations do not match:
    type resolver('parent, 'args, 'fieldType, 'result) =
        ('parent, 'args) => Js.Promise.t('result)
  is not included in
    type resolver('payload, 'fieldType, 'result)
  File "/Users/prisc_000/Downloads/Coobaha-examples_basic/src/SchemaTypes.re", line 3, characters 3-101:
    Actual declaration
  They have different arities.

>>>> Finish compiling(exit: 1)

reproduction here.https://github.com/idkjs/coobaha-repro

Expected behaviour
A clear and concise description of what you expected to happen.

Expected behaviour was for project to compile.

Trying to run clicks example from readme.

Version
Package version

    "graphql-to-reason": "^1.0.0-alpha.0",

Describe the bug
A clear and concise description of what the bug is.
I added SchemaTypes.re

include SchemaTypes_builder.MakeSchema({
  module Scalars = {type click;};
  type resolver('parent, 'payload, 'fieldType, 'result) =
    ('parent) => Js.Promise.t('result);
  type directiveResolver('payload);
});

I added Clicks.re to which i had to curry the resolve function for it to compile.

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^);
  });

I added Resolvers.re

let mutationResolvers =
  SchemaTypes.Mutation.t(~clicksCount=Clicks.resolver, ());

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

Which produces this error:

Error: The function applied to this argument has type
         (~click: GraphqlToReasonBasicExample.SchemaTypes.rootResolver(
                  {. "payload": int},
                   GraphqlToReasonBasicExample.SchemaTypes.click,
                   GraphqlToReasonBasicExample.SchemaTypes.click)=?) =>
         GraphqlToReasonBasicExample.SchemaTypes.Mutation.t
This argument cannot be applied with label ~clicksCount

Screenshot 2019-09-21 at 12 40 39 PM

How would these errors be resolved? Thanks.

Reproduction https://github.com/idkjs/graphql-to-reason-examples/tree/master/clicks-example

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.