@armix/server-schema v1.2.2
@armix/server-schema
model → adapter → schema
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-schemaUsage
Define your models
const User = {
name: "User",
fields: {
name: {
type: "String",
nullable: false,
},
},
};Import your adapters
- Use our core adapters like SchemaGraphQL or SchemaKnex,
- Or even create your own!
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
- Use with other packages like Apollo Server and enjoy!
import { ApolloServer } from "apollo-server";
const apollo = new ApolloServer({ typeDefs, resolvers });
apollo.listen();Motivation?
- Read here.
Development
First clone this project to your machine:
$ git clone git@github.com:armix-io/armix-server.gitOnce in the project root, you can run these scripts with yarn:
build— builds typescript./srcinto./libdev— starts typescripttsc(watching./src) andnodemon(watching./lib, executingyalc 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
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago