UNPKG

5.42 kBMarkdownView Raw
1<p align="center"><a href="https://nexus.js.org"><img src="https://i.imgur.com/Y5BgDGl.png" width="150" /><a></p>
2
3# GraphQL Nexus
4
5Declarative, code-first and strongly typed GraphQL schema construction for TypeScript & JavaScript
6
7> GraphQL Nexus is independent from Prisma. To learn how it can best be combined with Prisma, check out the [`nexus-prisma`](https://github.com/prisma/nexus-prisma) plugin.
8
9## Overview
10
11- **Code-first**: Programmatically define your GraphQL types in JavaScript/TypeScript
12- **Compatible with the GraphQL ecosystem**: Nexus is based on `graphql-js`
13- **Type-safe**: Nexus enables auto-completion and error checks in your IDE (even for JS)
14- **Generates SDL & TS definitions**: SDL schema and typings are updated as you code
15
16## Examples
17
18**"Hello World" GraphQL server with `graphql-yoga`**
19
20```ts
21import { queryType, stringArg, makeSchema } from "nexus";
22import { GraphQLServer } from "graphql-yoga";
23
24const Query = queryType({
25 definition(t) {
26 t.string("hello", {
27 args: { name: stringArg({ nullable: true }) },
28 resolve: (parent, { name }) => `Hello ${name || "World"}!`,
29 });
30 },
31});
32
33const schema = makeSchema({
34 types: [Query],
35 outputs: {
36 schema: __dirname + "/generated/schema.graphql",
37 typegen: __dirname + "/generated/typings.ts",
38 },
39});
40
41const server = new GraphQLServer({
42 schema,
43});
44
45server.start(() => `Server is running on http://localhost:4000`);
46```
47
48All examples of GraphQL Nexus can be found in the [`/examples`](./examples) directory:
49
50- [githunt-api](./examples/githunt-api)
51- [ts-ast-reader](./examples/ts-ast-reader)
52- [apollo-fullstack](./examples/apollo-fullstack)
53- [star-wars](./examples/star-wars)
54- [kitchen-sink](./examples/kitchen-sink)
55
56If you're interested in examples using the [`nexus-prisma`](https://github.com/prisma/nexus-prisma) plugin, check out the official [`prisma-examples`](https://github.com/prisma/prisma-examples/) repo:
57
58- [GraphQL blogging app](https://github.com/prisma/prisma-examples/tree/master/typescript/graphql)
59- [GraphQL blogging app with authentication & authorization](https://github.com/prisma/prisma-examples/tree/master/typescript/graphql-auth)
60- [GraphQL CRUD example](https://github.com/prisma/prisma-examples/tree/master/typescript/graphql-crud)
61
62## Features
63
64- Expressive, declarative API for building schemas
65- No need to re-declare interface fields per-object
66- Optionally possible to reference types by name (with autocomplete) rather than needing to import every single piece of the schema
67- Assumes non-null by default, but makes this configurable on per-schema/per-type basis
68- Interoperable with vanilla `graphql-js` types, and it's _just_ a [`GraphQLSchema`](https://graphql.org/graphql-js/type/#graphqlschema) so it fits in just fine with existing community solutions of `apollo-server`, `graphql-middleware`, etc.
69- Inline function resolvers for when you need to do simple field aliasing
70- Auto-generated graphql SDL schema, great for when seeing how any code changes affected the schema
71- Lots of good [examples](https://github.com/prisma/nexus/tree/develop/examples) to get you started and thorough [API documentation](https://nexus.js.org/docs/api-core-concepts)
72- Full type-safety for free
73- Internal structure allows library authors to build more advanced abstractions
74- Independent from Prisma, but integrates nicely using the [`nexus-prisma`](https://github.com/prisma/nexus-prisma) plugin
75- Allows code re-use by creating higher level "functions" which wrap common fields
76
77## Documentation
78
79You can find the docs for GraphQL Nexus [here](https://nexus.js.org).
80
81## CI
82
83[![CircleCI](https://circleci.com/gh/prisma/nexus.svg?style=svg)](https://circleci.com/gh/prisma/nexus)
84
85## Install
86
87GraphQL Nexus can be installed via the `nexus` package. It also requires `graphql` as a [peer dependency](https://nodejs.org/en/blog/npm/peer-dependencies/):
88
89```
90npm install --save nexus graphql
91```
92
93or
94
95```
96yarn add nexus graphql
97```
98
99## Migrate from SDL
100
101If you've been following an [SDL-first](https://www.prisma.io/blog/the-problems-of-schema-first-graphql-development-x1mn4cb0tyl3/) approach to build your GraphQL server and want to see what your code looks like when written with GraphQL Nexus, you can use the [**SDL converter**](https://nexus.js.org/converter):
102
103![](https://imgur.com/AbkFWNO.png)
104
105---
106
107## License (MIT)
108
109(c) 2018-2019 Tim Griesser
110
111Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
112
113The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
114
115THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.