All Projects → prismake → Typegql

prismake / Typegql

Licence: mit
Create GraphQL schema with TypeScript classes.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Typegql

Graphql2rest
GraphQL to REST converter: automatically generate a RESTful API from your existing GraphQL API
Stars: ✭ 181 (-56.39%)
Mutual labels:  api, graphql, graphql-server, graphql-api, graphql-js, graphql-schema
Type Graphql
Create GraphQL schema and resolvers with TypeScript, using classes and decorators!
Stars: ✭ 6,864 (+1553.98%)
Mutual labels:  api, graphql, graphql-js, schema, graphql-schema, decorators
Postgraphile
GraphQL is a new way of communicating with your server. It eliminates the problems of over- and under-fetching, incorporates strong data types, has built-in introspection, documentation and deprecation capabilities, and is implemented in many programming languages. This all leads to gloriously low-latency user experiences, better developer experiences, and much increased productivity. Because of all this, GraphQL is typically used as a replacement for (or companion to) RESTful API services.
Stars: ✭ 10,967 (+2542.65%)
Mutual labels:  api, graphql, graphql-api, graphql-js, schema
Storefront Api
Storefront GraphQL API Gateway. Modular architecture. ElasticSearch included. Works great with Magento1, Magento2, Spree, OpenCart, Pimcore and custom backends
Stars: ✭ 180 (-56.63%)
Mutual labels:  api, graphql, graphql-server, graphql-api
Countries
🌎 Public GraphQL API for information about countries
Stars: ✭ 156 (-62.41%)
Mutual labels:  api, graphql, graphql-api, schema
Pop
Monorepo of the PoP project, including: a server-side component model in PHP, a GraphQL server, a GraphQL API plugin for WordPress, and a website builder
Stars: ✭ 160 (-61.45%)
Mutual labels:  api, graphql, graphql-server, graphql-api
graphql-express-nodejs
A Simple GraphQL Server implementation using Express and Node. See post here: https://t.co/Cm6GitZaBL
Stars: ✭ 24 (-94.22%)
Mutual labels:  graphql-server, graphql-schema, graphql-js, graphql-api
Graphql Tools
🔧 Build, mock, and stitch a GraphQL schema using the schema language
Stars: ✭ 4,556 (+997.83%)
Mutual labels:  graphql, graphql-api, graphql-js, graphql-schema
Graphql Log
Add logging to your GraphQL resolvers so you know what's going on in your app.
Stars: ✭ 94 (-77.35%)
Mutual labels:  graphql, graphql-server, graphql-js, graphql-schema
Grial
A Node.js framework for creating GraphQL API servers easily and without a lot of boilerplate.
Stars: ✭ 194 (-53.25%)
Mutual labels:  graphql, graphql-server, graphql-api, graphql-js
36 Graphql Concepts
📜 36 concepts every GraphQL developer should know.
Stars: ✭ 209 (-49.64%)
Mutual labels:  graphql, graphql-server, graphql-api, graphql-schema
Graphql Cost Analysis
A Graphql query cost analyzer.
Stars: ✭ 527 (+26.99%)
Mutual labels:  graphql, graphql-server, graphql-js, graphql-schema
Hangzhou Graphql Party
杭州 GraphQLParty 往期记录(slide,照片,预告,视频等)
Stars: ✭ 142 (-65.78%)
Mutual labels:  graphql, graphql-server, graphql-api, graphql-js
Api Platform
Create REST and GraphQL APIs, scaffold Jamstack webapps, stream changes in real-time.
Stars: ✭ 7,144 (+1621.45%)
Mutual labels:  api, graphql, graphql-server, graphql-api
Wp Graphql
🚀 GraphQL API for WordPress
Stars: ✭ 3,097 (+646.27%)
Mutual labels:  api, graphql, graphql-server, graphql-api
Graphql Api For Wp
[READ ONLY] GraphQL API for WordPress
Stars: ✭ 136 (-67.23%)
Mutual labels:  api, graphql, graphql-server
Wp Graphql Woocommerce
Add WooCommerce support and functionality to your WPGraphQL server
Stars: ✭ 318 (-23.37%)
Mutual labels:  api, graphql, graphql-server
Graphql Core
A Python 3.6+ port of the GraphQL.js reference implementation of GraphQL.
Stars: ✭ 344 (-17.11%)
Mutual labels:  api, graphql, graphql-js
Conventions
GraphQL Conventions Library for .NET
Stars: ✭ 198 (-52.29%)
Mutual labels:  api, graphql, schema
Graphbrainz
A fully-featured GraphQL interface for the MusicBrainz API.
Stars: ✭ 130 (-68.67%)
Mutual labels:  api, graphql, graphql-schema

npm version npm version codecov Build Status

What is typegql?

demo

typegql is set of decorators allowing creating GraphQL APIs quickly and in type-safe way.

Examples:

Basic example

Example below is able to resolve such query

query {
  hello(name: "Bob") # will resolve to 'Hello, Bob!'
}
import { compileSchema, SchemaRoot, Query } from 'typegql';

@SchemaRoot()
class SuperSchema {
  @Query()
  hello(name: string): string {
    return `Hello, ${name}!`;
  }
}

const compiledSchema = compileSchema({ roots: [SuperSchema] });

compiledSchema is regular executable schema compatible with graphql-js library.

To use it with express, you'd have to simply:

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

const app = express();

app.use(
  '/graphql',
  graphqlHTTP({
    schema: compiledSchema,
    graphiql: true,
  }),
);
app.listen(3000, () => console.log('Graphql API ready on http://localhost:3000/graphql'));

Adding nested types

For now, our query field returned scalar (string). Let's return something more complex. Schema will look like:

mutation {
  createProduct(name: "Chair", price: 99.99) {
    name
    price
    isExpensive
  }
}

Such query will have a bit more code and here it is:

import { Schema, Query, ObjectType, Field, Mutation, compileSchema } from 'typegql';

@ObjectType({ description: 'Simple product object type' })
class Product {
  @Field() name: string;

  @Field() price: number;

  @Field()
  isExpensive() {
    return this.price > 50;
  }
}

@Schema()
class SuperSchema {
  @Mutation()
  createProduct(name: string, price: number): Product {
    const product = new Product();
    product.name = name;
    product.price = price;
    return product;
  }
}

const compiledSchema = compileSchema(SuperSchema);

Forcing field type.

Until now, typegql was able to guess type of every field from typescript type definitions.

There are, however, some cases where we'd have to define them explicitly.

  • We want to strictly tell if field is nullable or not
  • We want to be explicit about if some number type is Float or Int (GraphQLFloat or GraphQLInt) etc
  • Function we use returns type of Promise<SomeType> while field itself is typed as SomeType
  • List (Array) type is used. (For now, typescript Reflect api is not able to guess type of single array item. This might change in the future)

Let's modify our Product so it has additional categories field that will return array of strings. For sake of readibility, I'll ommit all fields we've defined previously.

@ObjectType()
class Product {
  @Field({ type: [String] }) // note we can use any native type like GraphQLString!
  categories(): string[] {
    return ['Tables', 'Furniture'];
  }
}

We've added { type: [String] } as @Field options. Type can be anything that is resolvable to GraphQL type

  • Native JS scalars: String, Number, Boolean.
  • Any type that is already compiled to graphql eg. GraphQLFloat or any type from external graphql library etc
  • Every class decorated with @ObjectType
  • One element array of any of above for list types eg. [String] or [GraphQLFloat]

Writing Asynchronously

Every field function we write can be async and return Promise. Let's say, instead of hard-coding our categories, we want to fetch it from some external API:

@ObjectType()
class Product {
  @Field({ type: [String] }) // note we can use any native type like GraphQLString!
  async categories(): Promise<string[]> {
    const categories = await api.fetchCategories();
    return categories.map(cat => cat.name);
  }
}

Before 1.0.0

Before version 1.0.0 consider APIs of typegql to be subject to change. We encourage you to try this library out and provide us feedback so we can polish it to be as usable and efficent as possible.

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].