Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/whats-good/uniform-graphql
Code-first GraphQL apis in TypeScript with complete & robust end-to-end type safety.
https://github.com/whats-good/uniform-graphql
complex-types graphql graphql-api graphql-schemas graphql-types resolvers typescript
Last synced: 4 months ago
JSON representation
Code-first GraphQL apis in TypeScript with complete & robust end-to-end type safety.
- Host: GitHub
- URL: https://github.com/whats-good/uniform-graphql
- Owner: whats-good
- Created: 2020-12-13T06:01:26.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-08-08T12:35:23.000Z (over 2 years ago)
- Last Synced: 2024-04-29T15:21:41.512Z (9 months ago)
- Topics: complex-types, graphql, graphql-api, graphql-schemas, graphql-types, resolvers, typescript
- Language: TypeScript
- Homepage:
- Size: 32.3 MB
- Stars: 45
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# UniformGraphQL
> Code-first GraphQL apis in TypeScript with complete & robust end-to-end type safety.
π Docs: [https://uniform-graphql.whatsgood.dog](http://uniform-graphql.whatsgood.dog)
## Features
- π€ Uniform type system: write once in `TypeScript`, get `GraphQL` schema for free.
- π¨βπ» Code-first by default, but can be partially used as schema-first.
- π No code generation. Your code becomes instantly usable.
- π¬ Sophisticated type system adjusted to the complexities of `GraphQL`.
- π‘ Single source of truth for your api.
- π No manual typecasting, no decorators, no runtime type checking.β οΈ Disclaimer: This is a very young and unstable library. Weβre still at `v0`. We have a pretty robust core, but everything is subject to change.
## Install
```sh
npm install @whatsgood/uniform-graphql
```β οΈ `graphql` is a peer dependency
## Examples
Go to the [examples](https://github.com/whats-good/uniform-graphql/tree/master/packages/examples) directory to see a demo
## Quick Start
```ts
import { t, SchemaBuilder } from '@whatsgood/uniform-graphql';
import { ApolloServer } from 'apollo-server-express';
import express from 'express';const Membership = t.enum({
name: 'Membership',
values: {
free: null,
paid: null,
enterprise: null,
},
});const Animal = t.object({
name: 'Animal',
fields: {
id: t.id,
age: t.integer,
name: t.string,
},
});const User = t.object({
name: 'User',
fields: {
id: t.id,
fullName: t.string.nullable,
membership: Membership,
pets: t.list(Animal),
},
});const schemaBuilder = new SchemaBuilder();
schemaBuilder.query('user', {
type: User,
args: {
id: t.id,
},
resolve: async (_, args, context) => {
return {
id: args.id,
fullName: () => 'John Johnson',
membership: 'enterprise' as const,
pets: async () => [
{
name: 'Lulu',
id: 'cat-1',
age: 10,
},
],
};
},
});schemaBuilder.mutation('signup', {
type: User,
args: {
email: t.string,
},
resolve: (_, args, context) => {
return {
id: 'newly signedup user id',
fullName: 'newly signed up user name',
membership: 'free' as const,
pets: [],
};
},
});schemaBuilder.fieldResolvers(User, {
fullName: async (root) => {
return 'overriding fullname';
},
});const apolloServer = new ApolloServer({
schema: schemaBuilder.getSchema();
});const PORT = 4001;
const app = express();
apolloServer.applyMiddleware({ app });app.listen({ port: PORT }, () => {
console.log(
`π Server ready at http://localhost:${PORT}${apolloServer.graphqlPath}`,
);
});
```## Recommended TSConfig
```json
{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"lib": ["es2018", "esnext.asynciterable"],
"strict": true
}
}
```## Roadmap
- [ ] Stabilize the `t.scalar` type factory
- [ ] IOC & containers
- [x] Documentation website
- [ ] Write tests (There are none right now)
- [x] Design a logo (open to suggestions)
- [ ] Argument validation
- [ ] Remove lodash and become `0 dependency`
- [ ] Enable query building through the object syntax: `t.query({ currentUser: ..., todos: ...})` instead of `t.query('currentUser', ...)`
- [ ] Subscriptions support
- [ ] Enable schema-first features: mock an api without implementing it.## Acknowledgements
`uniform-graphql` stands on the shoulders of 2 giants:
1. [type-graphql](https://github.com/MichalLytek/type-graphql): This is arguably the strongest code-first GraphQL solution for TypeScript. The author is very friendly and helpful, and has managed to create and maintain a great community. I urge you to go check them out and say hi.
2. [io-ts](https://github.com/gcanti/io-ts): The techniques Iβve found in this library have truly opened my mind to the limitless potential of TypeScript. `io-ts` is the library that convinced me that this library was possible.
> This library is `type-graphql` in substance and `io-ts` in form.
## Author
π€ **Kerem Kazan**
- Twitter: [@MechanicalKazan](https://twitter.com/MechanicalKazan)
- Github: [@mechanical-turk](https://github.com/mechanical-turk)
- LinkedIn: [@keremkazan](https://linkedin.com/in/keremkazan)