Skip to content

Commit bba6f44

Browse files
authored
Improved quick start and various ways to create schema (#1813)
1 parent c85dee4 commit bba6f44

File tree

1 file changed

+144
-36
lines changed

1 file changed

+144
-36
lines changed

website/src/pages/v3/index.mdx

Lines changed: 144 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,78 +3,185 @@ id: quick-start
33
title: Quick Start
44
---
55

6-
GraphQL Yoga is a batteries-included cross-platform [GraphQL over HTTP](https://github.com/graphql/graphql-over-http) spec-compliant GraphQL Server using [Envelop](https://envelop.dev), and [GraphQL Tools](https://graphql-tools.com) that runs anywhere.
6+
GraphQL Yoga is a batteries-included cross-platform [GraphQL over HTTP spec-compliant](https://github.com/enisdenjo/graphql-http/tree/master/implementations/graphql-yoga) GraphQL server powered by [Envelop](https://envelop.dev) and [GraphQL Tools](https://graphql-tools.com) that runs anywhere; focused on easy setup, performance and great developer experience.
77

88
## Installation
99

1010
import { PackageCmd } from '@theguild/components'
1111

1212
<PackageCmd packages={['graphql graphql-yoga']} />
1313

14-
## Usage
14+
## Schema
1515

16-
You will need to provide schema to Yoga, either by an existing executable schema, or by providing your type definitions and resolver map.
16+
You will need to provide a schema to Yoga, there are many ways to assemble a GraphQL schema, here's just a few:
1717

18-
```ts
19-
import { createYoga, createSchema } from 'graphql-yoga'
20-
import { createServer } from 'http'
18+
import { Tabs, Tab } from '@theguild/components'
2119

22-
const yoga = createYoga({
23-
schema: createSchema({
24-
typeDefs: /* GraphQL */ `
25-
type Query {
26-
hello: String
27-
}
28-
`,
29-
resolvers: {
30-
Query: {
31-
hello: () => 'Hello from Yoga!',
32-
},
20+
<Tabs items={['graphql-yoga', '@graphql-tools/schema', 'Pothos', 'GraphQL Nexus', 'gqtx', 'graphql-js']}>
21+
<Tab>
22+
23+
Use the `createSchema` function included in Yoga. It actually reuses the [`makeExecutableSchema` from @graphql-tools/schema](https://www.graphql-tools.com/docs/generate-schema).
24+
25+
```js filename="schema.js"
26+
import { createSchema } from 'graphql-yoga'
27+
28+
export const schema = createSchema({
29+
typeDefs: /* GraphQL */ `
30+
type Query {
31+
hello: String
32+
}
33+
`,
34+
resolvers: {
35+
Query: {
36+
hello: () => 'world',
37+
},
38+
},
39+
})
40+
```
41+
42+
</Tab>
43+
44+
<Tab>
45+
46+
[`makeExecutableSchema` from @graphql-tools/schema](https://www.graphql-tools.com/docs/generate-schema) is very simple, but powerful. Install it and create a schema.
47+
48+
<PackageCmd packages={['@graphql-tools/schema']} />
49+
50+
```js filename="schema.js"
51+
import { makeExecutableSchema } from '@graphql-tools/schema'
52+
53+
export const schema = makeExecutableSchema({
54+
typeDefs: /* GraphQL */ `
55+
type Query {
56+
hello: String
57+
}
58+
`,
59+
resolvers: {
60+
Query: {
61+
hello: () => 'world',
3362
},
63+
},
64+
})
65+
```
66+
67+
</Tab>
68+
69+
<Tab>
70+
71+
[Pothos](https://pothos-graphql.dev) is a plugin based GraphQL schema builder for TypeScript. Install it and create a schema.
72+
73+
<PackageCmd packages={['@pothos/core']} />
74+
75+
```js filename="schema.js"
76+
import SchemaBuilder from '@pothos/core'
77+
78+
const builder = new SchemaBuilder({})
79+
80+
builder.queryType({
81+
fields: (t) => ({
82+
hello: t.string({
83+
resolve: () => 'world',
84+
}),
3485
}),
3586
})
3687

37-
const server = createServer(yoga)
38-
server.listen(4000, () => {
39-
console.info('Server is running on http://localhost:4000/graphql')
88+
export const schema = builder.toSchema()
89+
```
90+
91+
</Tab>
92+
93+
<Tab>
94+
95+
Declarative, Code-First GraphQL Schemas for JavaScript/TypeScript through [Nexus GraphQL](https://nexusjs.org/). Install it and create schema.
96+
97+
<PackageCmd packages={['nexus']} />
98+
99+
```js filename="schema.js"
100+
import { makeSchema, queryField } from 'nexus'
101+
102+
const helloField = queryField('hello', {
103+
type: 'String',
104+
resolve: () => 'world',
40105
})
106+
107+
export const schema = makeSchema({ types: [helloField] })
41108
```
42109

43-
> Yoga uses GraphQL Tools under the hood so you'll want to follow the [`makeExecutableSchema`](https://www.graphql-tools.com/docs/generate-schema) pattern.
110+
</Tab>
111+
<Tab>
44112

45-
**That is it!**
113+
Code-first type-safe GraphQL schema without codegen or metaprogramming using [gqtx](https://github.com/sikanhe/gqtx). Install it and create schema.
46114

47-
Now visit [`http://localhost:4000/graphql`](http://localhost:4000/graphql) to execute the followiing query operation:
115+
<PackageCmd packages={['gqtx']} />
48116

49-
```graphql
50-
query HelloWorld {
51-
hello
52-
}
117+
```js filename="schema.js"
118+
import { createTypesFactory, buildGraphQLSchema } from 'gqtx'
119+
120+
const t = createTypesFactory()
121+
122+
const Query = t.queryType({
123+
fields: () => [
124+
t.field({
125+
name: 'hello',
126+
type: t.String,
127+
resolve: () => 'world',
128+
}),
129+
],
130+
})
131+
132+
export const schema = buildGraphQLSchema({ query: Query })
53133
```
54134

55-
## Using existing schema
135+
</Tab>
56136

57-
You can also pass an existing `GraphQLSchema` instance to `createServer`.
137+
<Tab>
58138

59-
If you're using a library such as [Pothos](https://pothos-graphql.dev/), [GraphQL Nexus](https://nexusjs.org/), [gqtx](https://github.com/sikanhe/gqtx), or vanilla [`graphql-js`](https://graphql.org/graphql-js/type/#graphqlschema), you simply can pass the `GraphQLSchema` to `schema`:
139+
[graphql-js](https://github.com/graphql/graphql-js) is a reference implementation of GraphQL for JavaScript. You should already have it installed, just create schema.
60140

61-
```ts
62-
import { createYoga } from 'graphql-yoga'
63-
import { createServer } from 'http'
141+
```js filename="schema.js"
142+
import { GraphQLSchema, GraphQLObjectType, GraphQLString } from 'graphql'
64143

144+
export const schema = new GraphQLSchema({
145+
query: new GraphQLObjectType({
146+
name: 'Query',
147+
fields: {
148+
hello: {
149+
type: GraphQLString,
150+
resolve: () => 'world',
151+
},
152+
},
153+
}),
154+
})
155+
```
156+
157+
</Tab>
158+
</Tabs>
159+
160+
## Server
161+
162+
After you have created a GraphQL schema, simply pass it in to Yoga and liftoff! 🚀
163+
164+
```js
165+
import { createYoga } from 'graphql-yoga'
166+
import { createServer } from 'node:http'
65167
import { schema } from './schema'
66168

67-
const yoga = createYoga({
68-
schema,
69-
})
169+
// Create a Yoga instance with a GraphQL schema.
170+
const yoga = createYoga({ schema })
70171

172+
// Pass it into a server to hook into request handlers.
71173
const server = createServer(yoga)
72174

175+
// Start the server and you're done!
73176
server.listen(4000, () => {
74177
console.info('Server is running on http://localhost:4000/graphql')
75178
})
76179
```
77180

181+
Visit [`http://localhost:4000/graphql`](http://localhost:4000/graphql) to see Yoga in action.
182+
183+
## Showcase
184+
78185
<iframe
79186
width="100%"
80187
height="400"
@@ -84,3 +191,4 @@ server.listen(4000, () => {
84191
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
85192
allowFullScreen
86193
></iframe>
194+
````

0 commit comments

Comments
 (0)