npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-to-reason

v1.0.0-alpha.0

Published

Converts Graphql schema to ReasonML types

Downloads

6

Readme

graphql-to-reason

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

Build Status npm

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