1.0.1 • Published 5 years ago
@schematizer/schematizer v1.0.1
Schematizer
This is another library for make graphql schemas with hard type declaration on arguments, types and inputs
Installation
you can install it with yarn or npm
yarn add @schematizer/schematizer
or
npm install @schematizer/schematizer
docs
Example
You can see the quick start demo or see the next code examplte to show how this library works
import { Fn, Queries, schematize, Type } from '@schematizer/schematizer';
import { graphql } from 'graphql';
// make a type
class Product {
public id: number;
public name: string;
public price: number;
public salable: boolean;
}
const productType = new Type(Product).fields(() => ({
id: 'int',
name: 'string',
price: 'float',
salable: 'boolean',
}));
const products: Product[] = [
{ id: 1, name: 'foo', price: 5, salable: true },
{ id: 2, name: 'bar', price: 2, salable: true },
];
// make queries
const queries = new Queries({
products: Fn([Product], () => () => products),
});
// schematize
const schema = schematize(productType, queries);
const source = `
query {
products {
id
name
price
salable
}
}
`;
// you can use your favorite server like "graphql yoga" or "apollo server"
const { data, errors } = await graphql({ schema, source });
Type
The Type
class defines a graphql type, first we need define a class that will represent our type and we will create a Type
instance
import { Type } from '@schematizer/schematizer';
class Person {
public id: number;
public name: string;
}
const personType = new Type(Person)
.as('userType') // optional
.fields(() => ({
id: 'int',
name: 'string',
}));
Input
Similar to Type, we need define a class to represent an input
import { Input } from '@schematizer/schematizer';
class MovementInput {
public from: number;
public to: number;
public amount: number;
public description: string;
}
const movementInput = new Input(MovementInput).fields(() => ({
from: 'int',
to: 'int',
amount: 'float',
description: 'string',
}));
Query
To define graphql queries we need use Queries
class, we will define the queries in the constructor argument
import { Arg, Fn, Queries } from '@schematizer/schematizer';
import { Product } from '../types';
import { getProductsMagically, getProductMagically } from '../magic';
const queries = new Queries({
getProducts: Fn([Product], ({
sqlInject = Arg('string'), // do not do this
}) => () => {
return getProductsMagically(sqlInject);
}),
getProduct: Fn(Product, ({
id = Arg('int'),
}) => () => {
return getProductMagically(id);
}),
});
Mutation
The mutations works just like queries, but with the Mutations
class
import { Arg, Fn, Mutations } from '@schematizer/schematizer';
import { Product } from '../types';
import { ProductInput } from '../inputs';
import { addProductMagically, removeProductMagically } from '../magic';
const mutations = new Mutations({
addProduct: Fn(Product, ({
input = Arg(ProductInput),
}) => () => {
return addProductMagically(input);
}),
removeProduct: Fn(Product, ({
id = Arg('int'),
}) => () => {
return removeProductMagically(id);
}),
});
1.0.1
5 years ago
1.0.0
5 years ago
0.1.2
5 years ago
0.1.0
5 years ago
0.1.1
5 years ago
0.0.1
5 years ago
0.0.0-alpha.1
5 years ago
0.0.0-alpha.0
5 years ago