• This repository has been archived on 28/Jul/2020
  • Stars
    star
    191
  • Rank 202,877 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Create GraphQL schema from JSON files and APIs

DEPRECATED

-- This project is no longer maintained and has fallen severely out of sync with the
-- GraphQL community. Don't use it at all.

json-to-graphql

Generates a GraphQL schema file based on any JSON data

Overview

This package can take in almost* any kind of JSON data and generate a valid GraphQL schema file, including custom types, lists, nullable and non-nullable fields, and deeply nested children.

If passed an array of JSON data it will use that as "training" data to check type consistency and identify if fields are nullable or not.


  • This project is still in pre-release stage and corner cases are sure to arise

Install

$ npm install --save json-to-graphql

Usage

generateSchema(data: json | Array<json>): string

Takes in JSON data (either a singular instance or array) and returns a string containing the schema definitions. You'll likely want to just write this to a file using fs.

import generateSchema from 'json-to-graphql'
import data from './data.json'

const schema = generateSchema(data)
fs.writeFile('schema.js', schema, callback)

example

Say you have the following JSON response from an API

{
  "name": "brandon",
  "id": 1,
  "favorite_color": "teal",
  "job": {
    "type": "web developer",
    "years": 1
  },
  "dogs": ["minnie", "navi"]
}

It includes strings, ints, nested objects, and arrays. Passing this to generateSchema would output the following schema:


const {
    GraphQLBoolean,
    GraphQLString,
    GraphQLInt,
    GraphQLFloat,
    GraphQLObjectType,
    GraphQLSchema,
    GraphQLID,
    GraphQLNonNull
} = require('graphql')


const JobType = new GraphQLObjectType({
    name: 'job',
    fields: {
        type: {
            description: 'enter your description',
            type: new GraphQLNonNull(GraphQLString),
            // TODO: Implement resolver for type
            resolve: () => null,
        },
        years: {
            description: 'enter your description',
            type: new GraphQLNonNull(GraphQLInt),
            // TODO: Implement resolver for years
            resolve: () => null,
        }
    },
});


module.exports = new GraphQLSchema({
    query: new GraphQLObjectType({
        name: 'RootQueryType',
        fields: () => ({
            name: {
                description: 'enter your description',
                type: new GraphQLNonNull(GraphQLString),
                // TODO: Implement resolver for name
                resolve: () => null,
            },
            id: {
                description: 'enter your description',
                type: new GraphQLNonNull(GraphQLID),
                // TODO: Implement resolver for id
                resolve: () => null,
            },
            favorite_color: {
                description: 'enter your description',
                type: new GraphQLNonNull(GraphQLString),
                // TODO: Implement resolver for favorite_color
                resolve: () => null,
            },
            job: {
                description: 'enter your description',
                type: new GraphQLNonNull(JobType),
                // TODO: Implement resolver for job
                resolve: () => null,
            },
            dogs: {
                description: 'enter your description',
                type: new GraphQLNonNull(new GraphQLList(GraphQLString)),
                // TODO: Implement resolver for dogs
                resolve: () => null,
            }
        })
    })
})

The only thing the user has to do is hook up the schema to an actual resolve function so that it knows how to query different values. In the future this project may include a CLI utility that can generate a resolve utility that wraps any JSON API.

Try it out

The __tests__ folder currently just contains a small example that you can edit and run. You can change values in api.js and see how the generated schema (which is outputed to the console) changes.

More Repositories

1

react-copy-write

✍️ Immutable state with a mutable API
JavaScript
1,785
star
2

tinytime

⏰ A straightforward date and time formatter in <1kb
JavaScript
1,337
star
3

react-perimeter

🚧 Create an invisible perimeter around an element and respond when its breached.
JavaScript
1,048
star
4

alder

A minimal implementation of the UNIX tree command with colors!
JavaScript
225
star
5

ln.js

TypeScript
175
star
6

critical-reasonmling-slides

OCaml
66
star
7

twitter-required-alt-text

A Chrome extension that will not let you tweet images until you've added alt text.
JavaScript
49
star
8

react-is-deprecated

Adds an optional isDeprecated method to React.PropTypes
JavaScript
44
star
9

enzyme-adapter-preact

JavaScript
39
star
10

reason-chat-app

A web-based chat application built with ReasonML
OCaml
36
star
11

ssr-benchmarks

Simple benchmark testing time taken to render on the server
JavaScript
31
star
12

reason-vue

OCaml
28
star
13

rst-selector-parser

A CSS selector-based parser for React Standard Tree (RST) traversal.
JavaScript
19
star
14

algorithms-and-data-structures

JavaScript
17
star
15

react-mood

render components based on the mood of the user
16
star
16

preact-flow-example

Minimal example showing how to use Preact with Flow
JavaScript
15
star
17

unnamed-web-lang

Rust
13
star
18

mentorship-program

A working draft of a mentorship program for underrepresented groups in tech.
11
star
19

blog

TypeScript
10
star
20

react-tester

A testing utility for modern React applications
JavaScript
10
star
21

reason-react-dom

OCaml
9
star
22

react-relay-redux

Redux integrations for Relay
JavaScript
8
star
23

gist.scss

gist.scss lets users restyle embedded gists easily with Sass variablles to better match their site.
CSS
8
star
24

babel-rust

A source-to-source JavaScript compiler written in Rust. Not for production use.
7
star
25

ReasonCharts

Rust
6
star
26

together

JavaScript
6
star
27

accessible-wordle

TypeScript
6
star
28

microbe.js

A small, easy to use Node.js framework written in ES6
JavaScript
6
star
29

preact-blaze

JavaScript
5
star
30

compiler

Rust
5
star
31

reaction-diffusion

JavaScript
4
star
32

unique-random-at-depth

JavaScript
4
star
33

gradient-animation.scss

Sass mixin that lets anyone easily implement an animated background gradient.
CSS
4
star
34

pymessage

Send SMS or iMessages directly from the command line
Python
4
star
35

reactive

JavaScript
3
star
36

cached-validations

Create validation functions that cache results
JavaScript
3
star
37

graphql-client

JavaScript
3
star
38

set-state-middleware

JavaScript
3
star
39

defined-keys-only

JavaScript
3
star
40

bst

JavaScript
3
star
41

assert-report

A barebones test reporter in ~1.5kb with zero dependencies
JavaScript
3
star
42

saveourshelter

source code for the upcoming website, saveourshelter.info
HTML
3
star
43

AccessibilityNodeInfoRepoProject

Kotlin
2
star
44

scroll.js

JavaScript
2
star
45

git-branch

A small script to create a new git branch and push it to origin
Shell
2
star
46

linked-list

JavaScript
2
star
47

except

blacklist object properties
JavaScript
2
star
48

googlefontloader

A web interface that allows for quick and easy downloads of the Google Web Fonts. It uses the Github API to access Google's public repository.
JavaScript
2
star
49

grasp.js

general purpose front-end utility for DOM manipulation and templating
JavaScript
2
star
50

duxly

A redux helper/pattern to simplify the mapping of action constants and reducers
JavaScript
2
star
51

react-spectre

A React component library for spectre.css
JavaScript
2
star
52

mkdirps-brackets

Allows for bracket expansions to be used in paths for folder creation
JavaScript
2
star
53

old-coding-challenge

Code challenge that must be completed by developer candidates
JavaScript
2
star
54

www

HTML
1
star
55

remix-remark-slate-resolve-bug

TypeScript
1
star
56

aweary

1
star
57

assets

CSS
1
star
58

chrome-user-timings-bug

HTML
1
star
59

exercism

My solutions to some exercism.io problems
Rust
1
star
60

ngGeometry

Angular directive for a simple Three.JS scene
JavaScript
1
star
61

immutable-state

helpers for managing immutable state
JavaScript
1
star
62

enzyme-test-repo

A repo to clone/fork when you need to provide a repo of an issue with enzyme
JavaScript
1
star