GraphQL API 框架,具有强大的约定和自动生成的模式
Node.js GraphQL Framework for building APIs with strong conventions through auto-generated code. With Warthog, set up your data models and resolvers, and it does the rest.
Warthog is a Node.js GraphQL API framework for quickly building consistent GraphQL APIs that have sorting, filtering and pagination out of the box. It is written in TypeScript and makes heavy use of decorators for concise, declarative code.
Warthog is now on version 2.0! There were a few breaking changes that you should consider while upgrading. Also, we tried to keep all new features development on v1, but did end up adding JSON filtering directly to 2.0 as it was much easier given some foundation refactors.
Expand for Breaking change details
A few fields have been updated to use more specific GraphQL scalars:
String. Dates now use type IDString. Dates now use type DateTimeSince your GraphQL schema has changed and so have the associated TypeScript types in classes.ts, there might be changes in your server code and even perhaps some associated client code if you use these generated classes in your client code.
mockDBConnection has been removedThe old codegen pipeline used TypeORM's metadata in order to generate the GraphQL schema since Warthog didn't also capture this metadata. Warthog now captures the necessary metadata, so we no longer need to lean on TypeORM and therefore we don't need the mockDBConnection we previously used during codegen. Searching your codebase for mockDBConnection and WARTHOG_MOCK_DATABASE/MOCK_DATABASE should do it. If you've been using the Warthog CLI for codegen, you shouldn't have anything to do here.
Staying on the latest versions of libraries is good for security, performance and new features. We've bumped to the latest stable versions of each of Warthog's dependencies. This might require some changes to your package.json.
If you get an error like:
Cannot get connection "default" from the connection manager. Make sure you have created such connection. Also make sure you have called useContainer(Container) in your application before you established a connection and importing any entity.It could be caused by 2 things:
Container injectionIn V1 of Warthog, the README suggested that you should explicitly create your DI containers and pass them into your App instance like so:
import { Container } from 'typedi'; // REMOVE this
import { useContainer } from 'typeorm'; // REMOVE this
import { App } from 'warthog';
async function run() {
useContainer(Container); // REMOVE this
const app = new App({ container: Container }); // REMOVE the container option here
await app.start();
}In V2, it is recommended that you no longer do this unless you explicitly need access to the Container.
It can sometimes cause problems to explicitly require Warthog's depdendencies (ie type-graphql, typedi, typeorm and typeorm-typedi-extensions). In future versions, remove these explicit dependencies from package.json:
- "type-graphql": "...",
- "typedi": "...",
- "typeorm": "...",
- "typeorm-typedi-extensions": "...",This library is intentionally opinionated and generates as much code as possible. When teams build products quickly, even if they have strong conventions and good linters, the GraphQL can quickly become inconsistent, making it difficult for clients to consume the APIs in a reusable way.
To do this, Warthog automatically generates the following:
Further, it covers the following concerns by hooking into best-in-class open source libraries:
Warthog currently only supports PostgreSQL as a DB engine, so you must have Postgres installed before getting Warthog set up.
Expand for Postgres installation options
If you're on OSX and have homebrew and homebrew-cask installed, you can simply run:
brew cask install postgresOr you can install Homebrew's official version:
brew install postgresql
`brew --prefix`/opt/postgres/bin/createuser -s postgresOtherwise, you can install Postgres.app manually.
See the warthog-starter project for how to use Docker to run Postgres.
Warthog comes with a CLI that makes it easy to get started.
To install in an existing project, you'll need to create several files in place and then you'll need to call a few Warthog CLI commands that:
The following code will get you bootstrapped. You should read through this before running:
…Here's what this looks like in action:
This will open up GraphQL Playground, where you can execute queries and mutations against your API.
First, add a user by entering the following in the window:
mutation {
createUser(data: { name: "Test User", age: 25, verified: false }) {
id
name
createdAt
}
}Then, query for this user:
query {
users {
id
name
createdAt
}
}See introducing-graphql-playground for more info about GraphQL Playground.
Expand for other options for how to play with Warthog
warthog-starter projectAnother way to start playing with Warthog is to clone the warthog-starter repo. To get the starter project up and running, do the following:
git clone [email protected]:goldcaddy77/warthog-starter.git
cd warthog-starter
yarn bootstrap
WARTHOG_AUTO_OPEN_PLAYGROUND=true yarn start:devYou can also clone the Warthog repo and run the examples in the examples folder.
git clone [email protected]:goldcaddy77/warthog.git
cd warthog/examples/01-simple-model
yarn bootstrap
yarn db:seed:dev
yarn startThis has a simple example in place to get you started. There are also a bunch of examples in the folder for more advanced use cases.
Note that the examples in the examples folder use relative import paths to call into Warthog. In your projects, you won't need to set this config value as it's only set to deal with the fact that it's using the Warthog core files without consuming the package from NPM. In your projects, you can omit this as I do in warthog-starter.
A model represents both a GraphQL type and a DB table. Warthog exposes a BaseModel class that provides the following columns for free: id, createdAt, createdById, updatedAt, updatedById, deletedAt, deletedById, version. If you use BaseModel in conjunction with BaseService (see below), all of these columns will be updated as you'd expect. The Warthog server will find all models that match the following glob - '/**/*.model.ts'. Ex: user.model.ts
Custom TypeORM and TypeGraphQL options may be passed into the Model decorator using the following signature.
@Model({ api: { description: 'Custom description' }, db: { name: 'customtablename' } })A Warthog resolver exposes queries (reading data) and mutations (writing data). They interact with the DB through services (described below) and typically make use of a bunch of auto-generated TypeScript types in the generated folder for things like sorting and filtering. Warthog will find all resolvers that match the following glob - '/**/*.resolver.ts'. Ex: user.resolver.ts
Services are the glue between resolvers and models. Warthog exposes a class called BaseService that exposes the following methods: find, findOne, create, update, delete. For the find operator, it also maps the auto-generated WhereInput attributes to the appropriate TypeORM Query Builders. Warthog's convention is to name services .service.ts. Ex: user.service.ts
When you start your server, there will be a new generated folder that Warthog creates automatically. The folder contains:
Model, Query, Resolver, StringField, etc...). Resolvers will import items from here instead of having to manually create them.classes.ts above. Check out this example's schema.graphql to show the additional GraphQL schema Warthog autogenerates.Most of the config in Warthog is done via environment variables (see Config - Environment Variables below). However, more complex/dynamic objects should be passed via the server config.
| attribute | description | default |
|---|---|---|
| container | TypeDI container. Warthog uses dependency injection under the hood. | empty container |
| authChecker | An instance of an AuthChecker to secure your resolvers. | |
| context | Context getter of form (request: Request) => Promise |
empty |
| logger | Logger |
暂无开放 Issues,或尚未同步最近议题。