0.1.1 • Published 4 years ago

@skimah/ds-faker v0.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

A true schema-first approach to building GraphQL APIs. Map your GraphQL types directly to your datasources and query away.

Features

  • Auto-generated CRUD and Field-filtering API from the schema definition (also supports readonly types)
  • Expose only the records you want to your clients via your schema definition
  • Datasource agnostic using the Skimah Datasource interface. Datasources like sql, csv, faker e.t.c. can be easily crafted
  • Schema based relationships across types from different datasources
  • Legacy friendly, field and type names can be remapped from legacy datasources to GraphQL field names for a cleaner API.
  • Bring your own GraphQL Server.

Motivation

  • To harness the power of graphql as an API description language
  • Different APIs for different clients based on need to know basis
  • Reusable resolvers/datasources by standardizing data access patterns across datasources
  • Create relationships across multiple datasources transparently
  • A good prototyping tool for fleshing application ideas with minimum server commitment

Example

import { generate } from "@skimah/api";
import CsvSource from "@skimah/ds-csv";
import JsonSource from "@skimah/ds-json";
import { graphql } from "graphql";

const users = new CsvSource({
  records: `id,user_name
  1,james
    `,
});

const tasks = new JsonSource({
  records: [
    {
      id: 1,
      title: "compile",
      done: false,
      owner: 1,
    },
    {
      id: 2,
      title: "deploy",
      done: true,
      owner: 1,
    },
  ],
});

const sources = {
  users,
  tasks,
};

const typeDefs = `
    type User @datasource(name: "users") {
        id: ID
        userName: String @named(as: "user_name")
        tasks: [Task] @relation
    }

    type Task @datasource(name: "tasks") {
        id: ID
        title: String
        done: Boolean
        owner: User @relation
    }
`;

const query = `{
  findUsers(limit: 1) {
    userName
    tasks(where: { done: { eq: true } }) {
      title
    }
  }
}`;

(async () => {

  //  Generate executable schema
  const { schema } = await generate({
    typeDefs,
    sources,
  });

  // use whatever graphql server that suits you
  const result = await graphql({
    schema,
    source: query,
  });

  console.log(result.data.findUsers);
})();

You can now run queries like the on below against the executable schema. Skimah will transparently fetch the data across the json and csv datasources

{
  findUsers(limit: 2) {
    username
    tasks(where: { done: { eq: true } }) {
      title
    }
  }
}

Skimah Directives

Skimah ships with a collection of directives that makes it easy to craft an API from a schema definition.

DirectiveDescriptionLocation
@readonlyOnly generate read APIs for the typeObject
@datasourceAssociates the type to a defined datasource. Datasources are reusableObject
@namedRename a type, field or relationship from datasource to GraphQL namesField | Object
@relationCreates a relationship between typesField

Skimah directives

Datasources

Datasources are the backbone of Skimah and they are quite simple to implement. They are simply a reusable CRUD interface to be implemented and are called by by the generated type resolvers for the corresponding GraphQL operations. Datasource documentation

A simple empty datasource implementation is as follows

const myCustomSource = {
  select: async (selection: QueryModel) => {
    return [];
  },

  create: async (models: MutationModel[]) => {
    return { affected: [] };
  },

  update: async (criteria: Criteria, changes: MutationModel) => {
    return { affected: [] };
  },

  delete: async (criteria: Criteria, model: Model) => {
    return { affected: [] };
  }
};

Packaged Datasources

DatasourceDescription
@skimah/jsonjson datasource can read records from json array records or file
@skimah/csvcsv datasource can read records from a csv text or file
@skimah/fakerfaker datasource uses fakerjs to generate records from type definition

Other datasources like Postgres, Mysql, SQLite, Dynamodb e.t.c are coming soon.

GraphQL API

Skimah's GraphQL API is heavily influenced by Hasura's and MongoDB API

DatasourceDescription
findA find query is generated for each type. e.g findUser for type called User
createCreate operation is generated for each type e.g createUsers for type called User
updateUpdate operation is generated for each type e.g updateUsers for type called User
deleteDelete operation is generated for each type e.g deleteUsers for type called User

Skimah Studio

Experience the power of a schema-first approach to GraphQL API development with an online IDE for quickly generating GraphQL APIs and endpoints.

  • Generate Skimah API on the fly
  • Use your own datasources to try out your API
  • Get a unique API endpoint to use in your application

Give it a try Skimah Studio Screenshot

Coming Soon

  • Support for Dataloader-ish caching
  • Support for Graphql interfaces
  • Support for Graphql query field alias
  • Support for Graphql Unions
  • Nested mutation across datasources

Bug tracker

If you have any suggestions, bug reports, or annoyances please report them to our issue tracker at https://github.com/skimah/skimah/issues

Links

⚠️ Skimah is not production ready yet but it is fabulous for quickly prototyping APIs for projects.

License

Released under MIT License.

Skimah is authored and maintained by @sayjava.