
Security News
Feross on TBPN: Socket's Series C and the State of Software Supply Chain Security
Feross Aboukhadijeh joins TBPN to discuss Socket's $60M Series C, 500%+ ARR growth, AI's impact on open source, and the rise in supply chain attacks.
decapi?
decapi is a set of decorators for creating GraphQL APIs in typescript. Write your types and GQL schema at once killing two birds with one stone.

Example below is able to resolve such query
query {
hello(name: "Bob") # will resolve to 'Hello, Bob!'
}
import { SchemaRoot, Query, compileSchema } from 'decapi'
@SchemaRoot()
class SuperSchema {
@Query()
hello(name: string): string {
return `Hello, ${name}!`
}
}
const compiledSchema = compileSchema(SuperSchema)
compiledSchema is a regular GQL executable schema compatible with graphql-js library.
To use it with express, you'd have to simply:
import express from 'express'
import 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')
)
Although it is encouraged to prefer fastify as it is a bit faster when used with jit.
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 {
SchemaRoot,
Query,
ObjectType,
Field,
Mutation,
compileSchema
} from 'decapi'
@ObjectType({ description: 'Simple product object type' })
class Product {
@Field()
name: string
@Field()
price: number
@Field()
isExpensive() {
return this.price > 50
}
}
@SchemaRoot()
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)
In previous examples, decapi was able to determine the type of every field from typescript type definitions.
There are, however cases where we have to define them explicitly.
number type is Float or Int (GraphQLFloat or GraphQLInt) etcPromise<SomeType> while field itself is typed as SomeTypeReflect api is not able to guess type of single array item)For major version 1.0.0 we will use https://github.com/rezonant/typescript-rtti so it will be able to determine all types directly from the TS type. This is WIP ATM.
Let's modify our Product so it has additional categories field that will return array of strings. For the sake of readability, let's 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
String, Number, Boolean, Date.graphql eg. GraphQLFloat or any type from external graphql library etc@ObjectType[String] or [GraphQLFloat]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)
}
}
There is a much more popular library with the same goals-so what makes decapi different? Decapi has smaller API surface-it only has hooks on top of the basic decorators for constructing schemas. Whereas type-graphql has authorization, middleware, guards. Also decapi supports graphql v16. Typegraphql is still only supporting Graphql v15
I wanted to contribute to typegql and work on it together with @pie6k, but it soon became obvious that we both have something different in mind. Just to briefly summarise the differences:
@DuplexObjectType and @DuplexFieldcastTo Field config1.0.0Before version 1.0.0 consider APIs of decapi 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.
Please if you enjoy decapi, go and star repo with the proposal-decorators . That's a way to show the TC39 members that you are using decorators and you want to have them in the language.
FAQs

The npm package decapi receives a total of 23 weekly downloads. As such, decapi popularity was classified as not popular.
We found that decapi demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Feross Aboukhadijeh joins TBPN to discuss Socket's $60M Series C, 500%+ ARR growth, AI's impact on open source, and the rise in supply chain attacks.

Security News
OSV withdrew 157 OSV malware reports after automated false positives incorrectly flagged trusted npm and PyPI packages, sending bad records into tools that rely on OSV data.

Research
/Security News
TrapDoor crypto stealer hits 36 malicious packages across npm, PyPI, and Crates.io, targeting crypto, DeFi, AI, and security developers.