0.2.0-alpha.0 • Published 4 years ago

@sayjava/scaffold-server v0.2.0-alpha.0

Weekly downloads
-
License
MIt
Repository
github
Last release
4 years ago

What is Scaffold?

  • :blush: Automatic CRUD operations from type definitions

  • Automatic Graphql resolvers to interact with configurable datasources

  • Write your own reusable datasources

  • Source date from multiple datasources with a simple API. e.g (csv, json, googlesheets, sql e.t.c)

  • Declarative relationship between types across datasources using the @relation directive

  • Type and field mapping to datasources using the @named directives. This is very useful for legacy datasources.

  • Zero Config ( but configurable) scaffold server. Powerful for quick prototyping with the built-in Sample Datasource

  • Bring your own server (works with any graphql server)

  • Pluggable into an existing code base

:rocket: Quick Prototyping

Create a file called schema.graphql with these type definitions.

See Sample Datasource docs

type User {
  id: ID # IDs will be auto-generated
  username: String @named(as: "name_firstName") # Sample first names
  lastname: String @named(as: "name_lastName") # Sample last names
  videos: [Video] @relation # create a one-to-many relationship between users and videos
}

type Video {
  id: ID # IDs will be auto-generated
  title: String @named(as: "name_title") # Sample title
  created_at: String @named(as: "date_recent") # Sample dates
  owner: User @relation # create a back one-to-one relationship between videos and a user
}

Run this command in the same folder as the schema.graphql file

 npx scaffold-server

Navigate to http://localhost:8080/graphql and run the following query

query {
  findUsers(limit: 2) {
    firstname
    lastname

    videos {
      title
      created_at
    }
  }
}

:black_nib: As a library

import { scaffold, ScaffoldConfig } from "@sayjava/scaffold-schema";
import { graphql } from "graphql";
import csv from "@sayjava/datasource-csv";

import myDatasource from "./custom_datasource";

const typeDefs = `
  type Album @datasource(name: "albums") {
    id: ID @named(as: "AlbumId")
    title: String @named(as: "Title")

    artist: Artist @relation @named(as: "ArtistId")
  }

  type Artist @datasource(name: "artists") {
    id: ID @named(as: "ArtistId")
    name: String @named(as: "Name")
    date: String

    albums: [Album] @relation
  }
`;

const { datasource: artist } = await myDatasource();

const { datasource: albums } = await csv({ filepath: "path/to/albums.csv" });
const sources = { artists, albums };

// scaffold the schema
const { schema } = await scaffold({ typeDefs, sources });

const query = `
    query {
        findArtists(where: { name: { eq: "U2" } }) {
            id
            name
            albums (orderBy: { date: asc }) {
                title
            }
        }
    }
`;

await graphql(schema, query);

Scaffold (Schema-first API) Benefits

  • Declarative and versioned API via type definition.

  • Change the schema, change the API. No new code to be written

  • Only expose a portion of the data as needed

  • Keep your existing datasource, works well with legacy sources

  • Expose and link old datasources with new ones via graphql

Workflow

Using a schema first approach.

Schema API Docs

Full API Docs Scaffold API Docs

Scaffold Datasources

TODOs

[] Graphql alias