gqtx

所属分类:自动编程
开发工具:TypeScript
文件大小:76KB
下载次数:0
上传日期:2023-03-06 07:23:41
上 传 者sh-1993
说明:  代码优先类型安全的GraphQL Server,无需代码生成或元编程
(Code-first type-safe GraphQL Server without codegen or metaprogramming)

文件列表:
.prettierignore (17, 2022-06-21)
.prettierrc (26, 2022-06-21)
CHANGELOG.md (1471, 2022-06-21)
WHY.md (1407, 2022-06-21)
examples (0, 2022-06-21)
examples\package.json (25, 2022-06-21)
examples\starwars.ts (8458, 2022-06-21)
examples\tsconfig.json (147, 2022-06-21)
jest.config.cjs (70, 2022-06-21)
package.json (1291, 2022-06-21)
rollup.config.js (851, 2022-06-21)
src (0, 2022-06-21)
src\build.ts (7286, 2022-06-21)
src\define.ts (13177, 2022-06-21)
src\index.ts (166, 2022-06-21)
src\relay.ts (5049, 2022-06-21)
src\types.ts (4972, 2022-06-21)
test-api (0, 2022-06-21)
test-api\api.ts (5674, 2022-06-21)
test-api\tsconfig.json (129, 2022-06-21)
test (0, 2022-06-21)
test\simple.spec.ts (13579, 2022-06-21)
tsconfig.cjs.json (114, 2022-06-21)
tsconfig.json (472, 2022-06-21)
yarn.lock (139242, 2022-06-21)

#### [Why another GraphqQL Server?](https://github.com/sikanhe/gqtx/blob/master/WHY.md) ## Getting Started

yarn add gqtx

## Type-safety without manual work `gqtx` is a thin layer on top of `graphql-js` for writing a type-safe GraphQL server in TypeScript. It provides you with a set of helper functions to create an intermediate representation of a GraphQL schema, and then converts that schema to a raw `graphql-js` schema. So you get to use everything from the reference implementation of GraphQL, but with way more type safety. If a schema compiles, the following holds: - The type of a field agrees with the return type of the resolver. - The arguments of a field agrees with the accepted arguments of the resolver. - The source of a field agrees with the type of the object to which it belongs. - The return type of the resolver will not be input types (InputObject) - The arguments of a field will not be abstract types (Interface, Union) - The context argument for all resolver functions in a schema agree. Most importantly, we achieve all this _without_ having to: - Set up code generation tools - Write SDL and having your schema partially defined in code and in a DSL file - Require special compiler magic such as `reflect-metadata` and decorators ### What does it look like? ```ts import { createTypesFactory, buildGraphQLSchema } from 'gqtx'; enum Role { Admin, User, } type User = { id: string; role: Role; name: string; }; const users: User[] = [ { id: '1', role: Role.Admin, name: 'Sikan' }, { id: '2', role: Role.User, name: 'Nicole' }, ]; type AppContext = { viewerId: 1; users: User[]; }; // We can set the app context type once, and it will // be automatically inferred for all our resolvers! :) const t = createTypesFactory(); const RoleEnum = t.enumType({ name: 'Role', description: 'A user role', values: [ { name: 'Admin', value: Role.Admin }, { name: 'User', value: Role.User }, ], }); const UserType = t.objectType({ name: 'User', description: 'A User', fields: () => [ t.field({ name: 'id', type: t.NonNull(t.ID) }), t.field({ name: 'role', type: t.NonNull(RoleEnum) }), t.field({ name: 'name', type: t.NonNull(t.String) }), ], }); const Query = t.queryType({ fields: [ t.field({ name: 'userById', type: UserType, args: { id: t.arg(t.NonNullInput(t.ID)), }, resolve: (_, args, ctx) => { // `args` is automatically inferred as { id: string } // `ctx` is also automatically inferred as AppContext // All with no extra work! const user = ctx.users.find((u) => u.id === args.id); // Also ensures we return an `User | null` type :) return user || null; }, }), ], }); const schema = buildGraphQLSchema({ query: Query, }); ``` #### Use your favorite server option to serve the schema! ```ts import express from 'express'; import graphqlHTTP from 'express-graphql'; const app = express(); app.use( '/graphql', graphqlHTTP({ schema, graphiql: true, }) ); app.listen(4000); ``` ## `gqtx` works best with TypeScript `strict` mode We recommend using [TypeScript strict mode](https://www.typescriptlang.org/tsconfig#strict) in order to have the best developer experience. **tsconfig.json** ```diff { "compilerOptions": { + "strict": true } } ``` ## To Recap - We created an intermediate representation of a GraphQL schema via the helper functions exported by this library. - Then, we converted the schema to a real graphql-js schema by calling `buildGraphQLSchema` at server startup time. - Used existing express middleware `express-graphql` to server our schema with `graphiql` explorer - That's it! We get a fully type-safe server with almost zero type annotation needed

近期下载者

相关文件


收藏者