json2graphql: From a JSON file to postgres-backed GraphQL
json2graphql is a tool that imports a JSON file to initialise schema and data in Postgres and then allows you to start querying it with GraphQL.
Hasura is used to expose a realtime GraphQL API on Postgres. Once your schema and data is imported, you can instantly start running powerful queries with filters, pagination, sorting, fetching relations, insert/update/delete mutations and subscriptions too.
Use-cases:
Bootstrapping a GraphQL backend: Try out this example of initialising a GraphQL chat backend using a messages/groups/users chat JSON file. Try it out
Play with a mongo dataset in Postgres & GraphQL: Export a mongo JSON dump, import it to Postgres and start querying it with GraphQL. Try it out
Query existing JSON datasets over GraphQL: Pick up a JSON dataset, import to your new or existing Hasura/Postgres instance and start querying it. Try using jdorfman/awesome-json-datasets.
Demo
In the GIF above, we are importing a schema and data from a JSON database. The Hasura GraphQL Engine is running at https://j2gtest.herokuapp.com
Run Hasura + Postgres: Run the Hasura GraphQL Engine and Postgres on Heroku's free tier by clicking this button:
Note the URL. It will be of the form: https://<app-name>.herokuapp.com. Let's say it's j2gtest.herokuapp.com.
For instructions on how to deploy Hasura in other environments, head to the docs.
json2graphql: We import schema, data and create Hasura configuration in one command:
Run GraphQL queries: You can query the data in Postgres tables over GraphQL using Hasura GraphQL Engine. You can make complicated queries like:
query {
user {
postsByUserId {
idtitlecommentsByPostId {
bodyid
}
}
id
}
}
Behind the scenes: The following schema is created in Postgres::
user (
id integernot nullprimary key,
name text
)
post (
id integernot nullprimary key,
title text,
views integer,
user_id integerforeign keyreferences user(id)
)
comment (
id integernot nullprimary key,
body text,
post_id integerforeign keyreferences post(id),
user_id integerforeign keyreferences user(id)
)
Installation
## Install globally
npm install -g json2graphql
## Or run as a one-off command
npx json2graphql <hasura-url> -d ./path/to/db.json
CLI Usage
# Running against a hasura without an admin secret
json2graphql https://j2gtest.herokuapp.com -d ./path/to/db.json
# Running against a hasura with an admin secret
json2graphql https://j2gtest.herokuapp.com -s <admin-secret> -d ./path/to/db.json
# Reset configuration, schema & data and import# Useful for updating schema structure or working against an existing Hasura setup# WARNING: This will remove all existing schema/data before applying
json2graphql https://j2gtest.herokuapp.com --overwrite -d ./path/to/db.json
Command
json2graphql URL [flags]
Args
URL: The URL where Hasura GraphQL Engine is running
Options
-d --db: path to the JS file that exports your sample JSON database
-o --overwrite: DANGER: Overwrite tables if they already exist in database
-v --version: show CLI version
-h, --help: show CLI help
JSON structure
The top level of your JSON database should be a JSON object with keys being the name of entities and values being list of entities. For example:
The JSON structure is a "normalised" set of objects
Top level objects are mapped to tables in postgres and root fields in the GraphQL schema
Keys in the objects are mapped to columns of the tables in postgres, and as fields in the GraphQL schema
Keys in the object with the column name of the form <ENTITY_NAME>_id, are considered to indicate foreign-key constraints on postgres, and connections in the GraphQL schema
The types of the columns/fields are inferred from the data in the columns
json2graphql treats top-level objects as tables, and their keys as columns. If it encounters a column name of the form <ENTITY_NAME>_id, json2graphql will consider it a foreign key to the entity with name <ENTITY_NAME>.
JavaScript type (constructor.name)
Postgres column type
GraphQL field type
Example data
Number
numeric
numeric
12.34 or 1223
String
text
String
Hello world
Boolean
bool
Boolean
true
Date
timestamptz
timestamptz
new Date("Jan 24, 2010 00:00:00")
Object or Array
jsonb
jsonb
{ ... }
Generating data - importing with .js files
You can also use Javascript .js files. This allows you to:
You should wrap it in an array and make the array a value of a top level key of your choice, say, country. You should also field name _id to id because the CLI expects an id field. It should look something like this:
请发表评论