GraphQL
Install @heronjs/gql alongside core, common, and the HTTP extension. Declare schema classes in @Module({graphqls: [...]}):
import {Module} from '@heronjs/common';
import {HeronJS} from '@heronjs/core';
import {GraphQL, Resolver} from '@heronjs/gql';
@GraphQL({typeDefs: '', Query: 'greeting: String!'})
class Queries {
@Resolver()
greeting() {
return 'Hello from HeronJS!';
}
}
@Module({graphqls: [Queries]})
class AppModule {}
const app = await HeronJS.create({module: AppModule});
app.add({graphql: {prefix: '/graphql', gqlDir: './schemas'}});
await app.listen({port: 3000, host: '127.0.0.1'});@GraphQL accepts inline type definitions and root field declarations. The adapter can also load .graphql/.gql files; gqlDir selects their directory. @Resolver marks query/mutation methods; @GqlContext, @GqlInfo, @GqlParams, and @GqlParent inject resolver inputs. Guards and DI apply to resolver execution, and Observable results use the first emission.
GraphQL uses graphql-http/lib/use/fastify on the shared listener. The default endpoints are POST /graphql, GET /schema, and GET /introspect. Supply actual callbacks for error formatting and validation rules in application configuration. Unexpected resolver errors are masked; GraphQLAPIError supports explicit client-facing errors.
Validate resolver arguments
import {GraphQL, Params, Resolver} from '@heronjs/gql';
import {z} from 'zod';
const OrderArgs = z.object({quantity: z.number().int().min(1)});
@GraphQL({typeDefs: '', Query: 'quantity(quantity: Int!): Int!'})
class OrderQueries {
@Resolver()
quantity(@Params({schema: OrderArgs}) args: z.output<typeof OrderArgs>) {
return args.quantity;
}
}Register OrderQueries in your module’s graphqls list. GraphQL coerces arguments before Zod validates the complete argument object; declare the arguments in SDL as well. Schemas may transform values or perform async refinements. Validation errors use BAD_USER_INPUT with extensions.validation. See Input validation.
GraphQL authenticates once per field and enforces all class and field guard policies. Header, cookie, and session credentials pass through the configured resolver before OnGuard; session credentials use the same string-value contract. See Authorization.