1.2.2 • Published 3 years ago

@armix/server-schema v1.2.2

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

@armix/server-schema

modeladapterschema

Quickly generate extensible schemas for your backend, with first-class support for GraphQL and Knex; generate reusable TypeDefs and Resolvers to use with packages like Apollo Server, and concise table-building functions for Knex-supported databases!

Install

yarn add @armix/server-schema

Usage

Define your models

const User = {
  name: "User",
  fields: {
    name: {
      type: "String",
      nullable: false,
    },
  },
};

Import your adapters

import { SchemaGraphQL } from "@armix/server-schema";

const graphql = SchemaGraphQL({ ... });

Generate your schemas

import SchemaManager from "@armix/server-schema";

const schemas = SchemaManager([User], { graphql });

Explore your schemas

const { User } = schemas;

> User.graphql.typeDefs.Root /*
type User { name: String! }
type UserManyResult {
  items: [User!]!
  total: Int
  cursor: String
}
input UserManyFilters {
  name_eq: String
  name_ne: String
  name_gt: String
  name_gte: String
  name_lt: String
  name_lte: String
}
input UserInput { name: String }
input UserInputID { id: ID! name: String } */

> User.graphql.typeDefs.Query /*
User(id: ID!): User!
User_many(
  cursor: String
  order: String
  filters: UserManyFilters
  limit: Int
): UserManyResult! */

> User.graphql.typeDefs.Mutation /*
User_create(data: [UserInput!]!): [User!]!
User_update(data: [UserInputID!]!): [User!]!
User_delete(ids: [ID!]!): [ID!]! */

> User.graphql.resolvers /*
{
  Root: {
    User: {
      name: [AsyncFunction: resolver],
    }
  },
  Query: {
    User: [AsyncFunction: resolverQuery],
    User_many: [AsyncFunction: resolverQueryMany]
  },
  Mutation: {
    User_create: [AsyncFunction: resolverMutationCreate],
    User_update: [AsyncFunction: resolverMutationUpdate],
    User_delete: [AsyncFunction: resolverMutationDelete]
  }
} */

Wrangle your schemas

  • Use built-in helpers to wrangle all your schemas.
import { mergeTypeDefs, mergeResolvers } from "@armix/server-schema";

const schemas = [User, Post, Comment];
const allTypeDefs = schemas.map((schema) => schema.graphql.typeDefs);
const allResolvers = schemas.map((schema) => schema.graphql.resolvers);

const typeDefs = gql`
  ${mergeTypeDefs(allTypeDefs)}
  type Query {
    Foo: String!;
    ${mergeTypeDefs(allTypeDefs, "Query")}
  }
  type Mutation {
    Faa: String!;
    ${mergeTypeDefs(allTypeDefs, "Mutation")}
  }
`;

const resolvers = {
  ...mergeResolvers(allResolvers),
  Query: {
    Foo: (...) => "Bar",
    ...mergeResolvers(allResolvers, "Query"),
  },
  Mutation: {
    Faa: (...) => "Bor",
    ...mergeResolvers(allResolvers, "Mutation"),
  },
}

Use your schemas

import { ApolloServer } from "apollo-server";

const apollo = new ApolloServer({ typeDefs, resolvers });
apollo.listen();

Motivation?

Development

First clone this project to your machine:

$ git clone git@github.com:armix-io/armix-server.git

Once in the project root, you can run these scripts with yarn:

  • build — builds typescript ./src into ./lib
  • dev — starts typescript tsc (watching ./src) and nodemon (watching ./lib, executing yalc push) to automatically publish changes to local subscribed projects, see development with yalc.

Development with yalc

The develop script requires yalc to be installed globally.

Once installed you need to run yalc publish in this package directory, and then run yalc add @armix/server-graphql in your target project to subscribe to future local changes. Back in this package directory, you can automatically build and push changes with yarn develop, or manually with yalc push.

Maintainers

1.2.0

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago