All Projects → sikanhe → Gqtx

sikanhe / Gqtx

Code-first type-safe GraphQL Server without codegen or metaprogramming

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Gqtx

Graphql Stack
A visual explanation of how the various tools in the GraphQL ecosystem fit together.
Stars: ✭ 117 (-32.37%)
Mutual labels:  graphql, graphql-server
Graphql Serverless
GraphQL (incl. a GraphiQL interface) middleware for the webfunc serverless web framework.
Stars: ✭ 124 (-28.32%)
Mutual labels:  graphql, graphql-server
Searchobjectgraphql
GraphQL plugin for SearchObject gem
Stars: ✭ 118 (-31.79%)
Mutual labels:  graphql, graphql-server
Firebase Functions Graphql Example
GraphQL server running on Cloud Functions for Firebase
Stars: ✭ 107 (-38.15%)
Mutual labels:  graphql, graphql-server
Hangzhou Graphql Party
杭州 GraphQLParty 往期记录(slide,照片,预告,视频等)
Stars: ✭ 142 (-17.92%)
Mutual labels:  graphql, graphql-server
Qlens
QLens is an electron app which dynamically generates GraphQL Schemas and Mongo Schema visualization. QLens significantly cuts development time by automating the formation of their GraphQL schemas based on information fetched from their non-relational database.
Stars: ✭ 110 (-36.42%)
Mutual labels:  graphql, graphql-server
Graphql Directive Auth
GraphQL directive for handling auth
Stars: ✭ 120 (-30.64%)
Mutual labels:  graphql, graphql-server
Ariadne
Ariadne is a Python library for implementing GraphQL servers using schema-first approach.
Stars: ✭ 1,274 (+636.42%)
Mutual labels:  graphql, graphql-server
Graphql Api For Wp
[READ ONLY] GraphQL API for WordPress
Stars: ✭ 136 (-21.39%)
Mutual labels:  graphql, graphql-server
Graphql Compose Examples
Live examples of schemas builded with graphql-compose
Stars: ✭ 134 (-22.54%)
Mutual labels:  graphql, graphql-server
Json Graphql Server
Get a full fake GraphQL API with zero coding in less than 30 seconds.
Stars: ✭ 1,369 (+691.33%)
Mutual labels:  graphql, graphql-server
Graphql Jpa
JPA Implementation of GraphQL (builds on graphql-java)
Stars: ✭ 156 (-9.83%)
Mutual labels:  graphql, graphql-server
Graphql Log
Add logging to your GraphQL resolvers so you know what's going on in your app.
Stars: ✭ 94 (-45.66%)
Mutual labels:  graphql, graphql-server
Pragma
Build GraphQL APIs In No Time
Stars: ✭ 111 (-35.84%)
Mutual labels:  graphql, graphql-server
Qlkube
A GraphQL api for Kubernetes
Stars: ✭ 89 (-48.55%)
Mutual labels:  graphql, graphql-server
Server
Framework NodeJS for GraphQl
Stars: ✭ 118 (-31.79%)
Mutual labels:  graphql, graphql-server
Graphql Transform Schema
Transform, filter & alias resolvers of a GraphQL schema
Stars: ✭ 84 (-51.45%)
Mutual labels:  graphql, graphql-server
Graphjin
GraphJin - Build APIs in 5 minutes with GraphQL. An instant GraphQL to SQL compiler.
Stars: ✭ 1,264 (+630.64%)
Mutual labels:  graphql, graphql-server
Go Proto Gql
Protobuff plugins for generating graphql schema and golang to graphql bindings. Also supports a graphql gateway (Alpha)
Stars: ✭ 127 (-26.59%)
Mutual labels:  graphql, graphql-server
Apollo Server
🌍  Spec-compliant and production ready JavaScript GraphQL server that lets you develop in a schema-first way. Built for Express, Connect, Hapi, Koa, and more.
Stars: ✭ 12,145 (+6920.23%)
Mutual labels:  graphql, graphql-server

Why another GraphqQL Server?

Getting Started

yarn add gqtx

Type-safety without manual work

gqtx is a thin layer on top of graphql-js for writing a type-safe GraphQL server in TypeScript. It provides you with a set of helper functions to create an intermediate representation of a GraphQL schema, and then converts that schema to a raw graphql-js schema. So you get to use everything from the reference implementation of GraphQL, but with way more type safety.

If a schema compiles, the following holds:

  • The type of a field agrees with the return type of the resolver.
  • The arguments of a field agrees with the accepted arguments of the resolver.
  • The source of a field agrees with the type of the object to which it belongs.
  • The return type of the resolver will not be input types (InputObject)
  • The arguments of a field will not be abstract types (Interface, Union)
  • The context argument for all resolver functions in a schema agree.

Most importantly, we achieve all this without having to:

  • Set up code generation tools
  • Write SDL and having your schema partially defined in code and in a DSL file
  • Require special compiler magic such as reflect-metadata and decorators

What does it look like?

import { createTypesFactory, buildGraphQLSchema } from 'gqtx';

enum Role {
  Admin,
  User,
}

type User = {
  id: string;
  role: Role;
  name: string;
};

const users: User[] = [
  { id: "1", role: Role.Admin, name: 'Sikan' },
  { id: "2", role: Role.User, name: 'Nicole' },
];

type AppContext = {
  viewerId: 1,
  users: User[]
}

// We can set the app context type once, and it will
// be automatically inferred for all our resolvers! :)
const t = createTypesFactory<AppContext>();

const RoleEnum = t.enumType({
  name: 'Role',
  description: 'A user role',
  values: [{ name: 'Admin', value: Role.Admin }, { name: 'User', value: Role.User }],
});

const UserType = t.objectType<User>({
  name: 'User',
  description: 'A User',
  fields: () => [
    t.defaultField('id', t.NonNull(t.ID)),
    t.defaultField('role', t.NonNull(RoleEnum)),
    // `defaultField` is the safe version of a default resolver
    // field. In this case, field 'name' must exist on `User`
    // and its type must be `string`
    t.defaultField('name', t.NonNull(t.String)),
  ],
});

const Query = t.queryType({
  fields: [
    t.field('userById', {
      type: UserType,
      args: {
        id: t.arg(t.NonNullInput(t.ID)),
      },
      resolve: (_, args, ctx) => {
        // `args` is automatically inferred as { id: string }
        // `ctx` is also automatically inferred as AppContext
        //  All with no extra work!
        const user = ctx.users.find(u => u.id === args.id);
        // Also ensures we return an `User | null` type :)
        return user || null;
      },
    })
  ],
});

const schema = buildGraphQLSchema({
  query: Query,
});

Use your favorite server option to serve the schema!

import express from 'express';
import graphqlHTTP from 'express-graphql';

const app = express();

app.use(
  '/graphql',
  graphqlHTTP({
    schema,
    graphiql: true,
  })
);

app.listen(4000);

To Recap

  • We created an intermediate representation of a GraphQL schema via the helper functions exported by this library.
  • Then, we converted the schema to a real graphql-js schema by calling buildGraphQLSchema at server startup time.
  • Used existing express middleware express-graphql to server our schema with graphiql explorer
  • That's it! We get a fully type-safe server with almost zero type annotation needed
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].