{"id":13454281,"url":"https://github.com/prismake/typegql","last_synced_at":"2025-04-04T13:06:23.517Z","repository":{"id":29909515,"uuid":"123168026","full_name":"prismake/typegql","owner":"prismake","description":"Create GraphQL schema with TypeScript classes.","archived":false,"fork":false,"pushed_at":"2022-04-09T15:32:45.000Z","size":2875,"stargazers_count":425,"open_issues_count":17,"forks_count":19,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-28T12:04:03.618Z","etag":null,"topics":["api","decorators","graphql","graphql-api","graphql-js","graphql-schema","graphql-server","graphql-tools","schema","typeorm","typescript"],"latest_commit_sha":null,"homepage":"https://prismake.github.io/typegql/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prismake.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-27T18:03:45.000Z","updated_at":"2025-02-11T17:18:39.000Z","dependencies_parsed_at":"2022-08-07T14:31:06.012Z","dependency_job_id":null,"html_url":"https://github.com/prismake/typegql","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prismake%2Ftypegql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prismake%2Ftypegql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prismake%2Ftypegql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prismake%2Ftypegql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prismake","download_url":"https://codeload.github.com/prismake/typegql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247177293,"owners_count":20896620,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","decorators","graphql","graphql-api","graphql-js","graphql-schema","graphql-server","graphql-tools","schema","typeorm","typescript"],"created_at":"2024-07-31T08:00:52.585Z","updated_at":"2025-04-04T13:06:23.471Z","avatar_url":"https://github.com/prismake.png","language":"TypeScript","readme":"[![npm version](https://badge.fury.io/js/typegql.svg)](https://badge.fury.io/js/typegql)\n[![npm version](https://david-dm.org/prismake/typegql.svg)](https://david-dm.org/prismake/typegql)\n[![codecov](https://codecov.io/gh/prismake/typegql/branch/master/graph/badge.svg)](https://codecov.io/gh/prismake/typegql)\n[![Build Status](https://api.travis-ci.org/prismake/typegql.svg?branch=master)](https://travis-ci.org/prismake/typegql)\n\n### What is `typegql`?\n\n![demo](assets/demo.gif)\n\ntypegql is set of decorators allowing creating GraphQL APIs quickly and in type-safe way.\n\n* [Documentation](https://prismake.github.io/typegql/)\n\n### Examples:\n\n* [Basic Express example](examples/basic-express-server)\n* [Typeorm integration example](examples/typeorm-basic-integration)\n* [Forward resolution - eg. query only needed db fields](examples/forward-resolution)\n* [Nested mutations or queries](examples/nested-mutation-or-query)\n* [Custom decorators / Higher order decorators](examples/custom-decorators)\n* [Serverless eg. AWS Lambda](examples/serverless)\n* [Merge schemas](examples/merge-schemas)\n\n## Basic example\n\nExample below is able to resolve such query\n\n```graphql\nquery {\n  hello(name: \"Bob\") # will resolve to 'Hello, Bob!'\n}\n```\n\n```typescript\nimport { compileSchema, SchemaRoot, Query } from 'typegql';\n\n@SchemaRoot()\nclass SuperSchema {\n  @Query()\n  hello(name: string): string {\n    return `Hello, ${name}!`;\n  }\n}\n\nconst compiledSchema = compileSchema({ roots: [SuperSchema] });\n```\n\n`compiledSchema` is regular executable schema compatible with `graphql-js` library.\n\nTo use it with `express`, you'd have to simply:\n\n```typescript\nimport * as express from 'express';\nimport * as graphqlHTTP from 'express-graphql';\n\nconst app = express();\n\napp.use(\n  '/graphql',\n  graphqlHTTP({\n    schema: compiledSchema,\n    graphiql: true,\n  }),\n);\napp.listen(3000, () =\u003e console.log('Graphql API ready on http://localhost:3000/graphql'));\n```\n\n## Adding nested types\n\nFor now, our query field returned scalar (string). Let's return something more complex. Schema will look like:\n\n```graphql\nmutation {\n  createProduct(name: \"Chair\", price: 99.99) {\n    name\n    price\n    isExpensive\n  }\n}\n```\n\nSuch query will have a bit more code and here it is:\n\n```typescript\nimport { Schema, Query, ObjectType, Field, Mutation, compileSchema } from 'typegql';\n\n@ObjectType({ description: 'Simple product object type' })\nclass Product {\n  @Field() name: string;\n\n  @Field() price: number;\n\n  @Field()\n  isExpensive() {\n    return this.price \u003e 50;\n  }\n}\n\n@Schema()\nclass SuperSchema {\n  @Mutation()\n  createProduct(name: string, price: number): Product {\n    const product = new Product();\n    product.name = name;\n    product.price = price;\n    return product;\n  }\n}\n\nconst compiledSchema = compileSchema(SuperSchema);\n```\n\n## Forcing field type.\n\nUntil now, `typegql` was able to guess type of every field from typescript type definitions.\n\nThere are, however, some cases where we'd have to define them explicitly.\n\n* We want to strictly tell if field is nullable or not\n* We want to be explicit about if some `number` type is `Float` or `Int` (`GraphQLFloat` or `GraphQLInt`) etc\n* Function we use returns type of `Promise\u003cSomeType\u003e` while field itself is typed as `SomeType`\n* 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)\n\nLet'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.\n\n```typescript\n@ObjectType()\nclass Product {\n  @Field({ type: [String] }) // note we can use any native type like GraphQLString!\n  categories(): string[] {\n    return ['Tables', 'Furniture'];\n  }\n}\n```\n\nWe've added `{ type: [String] }` as `@Field` options. Type can be anything that is resolvable to `GraphQL` type\n\n* Native JS scalars: `String`, `Number`, `Boolean`.\n* Any type that is already compiled to `graphql` eg. `GraphQLFloat` or any type from external graphql library etc\n* Every class decorated with `@ObjectType`\n* One element array of any of above for list types eg. `[String]` or `[GraphQLFloat]`\n\n## Writing Asynchronously\n\nEvery 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:\n\n```typescript\n@ObjectType()\nclass Product {\n  @Field({ type: [String] }) // note we can use any native type like GraphQLString!\n  async categories(): Promise\u003cstring[]\u003e {\n    const categories = await api.fetchCategories();\n    return categories.map(cat =\u003e cat.name);\n  }\n}\n```\n\n## Before `1.0.0`\n\nBefore 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.\n","funding_links":[],"categories":["TypeScript","Table of Contents","Libraries","Community","Built with TypeScript"],"sub_categories":["TypeScript Libraries","Libraries"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprismake%2Ftypegql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprismake%2Ftypegql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprismake%2Ftypegql/lists"}