You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Provide people with tabs so they can use classes as well (#4284)
This provides people with the option to choose between the template
approach or the classes approach. This is a proposal to tackle
#1368
[Preview](https://graphql-7w0ort26u-the-graph-ql-foundation.vercel.app/)
This has been applied throughout the codebase now, however one of the
things I am uncertain about is how we offer `buildSchema` with the
GraphQLDefer/... directives? Should we add an option to `buildSchema`?
The exports defined in that chapter seem to only exist in v17 so we
should explicitly flag that.
Copy file name to clipboardexpand all lines: website/pages/authentication-and-express-middleware.mdx
+54-6
Original file line number
Diff line number
Diff line change
@@ -3,22 +3,67 @@ title: Authentication and Express Middleware
3
3
sidebarTitle: Authentication & Middleware
4
4
---
5
5
6
+
import { Tabs } from'nextra/components';
7
+
6
8
It's simple to use any Express middleware in conjunction with `graphql-http`. In particular, this is a great pattern for handling authentication.
7
9
8
10
To use middleware with a GraphQL resolver, just use the middleware like you would with a normal Express app. The `request` object is then available as the second argument in any resolver.
9
11
10
12
For example, let's say we wanted our server to log the IP address of every request, and we also want to write an API that returns the IP address of the caller. We can do the former with middleware, and the latter by accessing the `request` object in a resolver. Here's server code that implements this:
console.log('Running a GraphQL API server at localhost:4000/graphql');
48
-
```
93
+
````
94
+
95
+
</Tabs.Tab>
96
+
</Tabs>
49
97
50
98
In a REST API, authentication is often handled with a header, that contains an auth token which proves what user is making this request. Express middleware processes these headers and puts authentication data on the Express `request` object. Some middleware modules that handle authentication like this are [Passport](http://passportjs.org/), [express-jwt](https://github.com/auth0/express-jwt), and [express-session](https://github.com/expressjs/session). Each of these modules works with `graphql-http`.
Copy file name to clipboardexpand all lines: website/pages/basic-types.mdx
+60-1
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
title: Basic Types
3
3
---
4
4
5
+
import { Tabs } from'nextra/components';
6
+
5
7
In most situations, all you need to do is to specify the types for your API using the GraphQL schema language, taken as an argument to the `buildSchema` function.
6
8
7
9
The GraphQL schema language supports the scalar types of `String`, `Int`, `Float`, `Boolean`, and `ID`, so you can use these directly in the schema you pass to `buildSchema`.
@@ -12,6 +14,8 @@ To use a list type, surround the type in square brackets, so `[Int]` is a list o
12
14
13
15
Each of these types maps straightforwardly to JavaScript, so you can just return plain old JavaScript objects in APIs that return these types. Here's an example that shows how to use some of these basic types:
Copy file name to clipboardexpand all lines: website/pages/constructing-types.mdx
+21-14
Original file line number
Diff line number
Diff line change
@@ -2,26 +2,30 @@
2
2
title: Constructing Types
3
3
---
4
4
5
+
import { Tabs } from'nextra/components';
6
+
5
7
For many apps, you can define a fixed schema when the application starts, and define it using GraphQL schema language. In some cases, it's useful to construct a schema programmatically. You can do this using the `GraphQLSchema` constructor.
6
8
7
9
When you are using the `GraphQLSchema` constructor to create a schema, instead of defining `Query` and `Mutation` types solely using schema language, you create them as separate object types.
8
10
9
11
For example, let's say we are building a simple API that lets you fetch user data for a few hardcoded users based on an id. Using `buildSchema` we could write a server with:
console.log('Running a GraphQL API server at localhost:4000/graphql');
113
-
```
117
+
````
118
+
119
+
</Tabs.Tab>
120
+
</Tabs>
114
121
115
-
When we use this method of creating the API, the root level resolvers are implemented on the `Query` and `Mutation` types rather than on a `root` object.
122
+
When we use the `GraphQLSchema` constructor method of creating the API, the root level resolvers are implemented on the `Query` and `Mutation` types rather than on a `root` object.
116
123
117
-
This is particularly useful if you want to create a GraphQL schema automatically from something else, like a database schema. You might have a common format for something like creating and updating database records. This is also useful for implementing features like union types which don't map cleanly to ES6 classes and schema language.
124
+
This can be particularly useful if you want to create a GraphQL schema automatically from something else, like a database schema. You might have a common format for something like creating and updating database records. This is also useful for implementing features like union types which don't map cleanly to ES6 classes and schema language.
Copy file name to clipboardexpand all lines: website/pages/getting-started.mdx
+45-9
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,8 @@ title: Getting Started With GraphQL.js
3
3
sidebarTitle: Getting Started
4
4
---
5
5
6
+
import { Tabs } from'nextra/components';
7
+
6
8
{/* title can be removed in Nextra 4, since sidebar title will take from first h1 */}
7
9
8
10
# Getting Started With GraphQL.js
@@ -19,7 +21,7 @@ and arrow functions, so if you aren't familiar with them you might want to read
19
21
20
22
To create a new project and install GraphQL.js in your current directory:
21
23
22
-
```bash
24
+
```sh npm2yarn
23
25
npm init
24
26
npm install graphql --save
25
27
```
@@ -28,18 +30,49 @@ npm install graphql --save
28
30
29
31
To handle GraphQL queries, we need a schema that defines the `Query` type, and we need an API root with a function called a “resolver” for each API endpoint. For an API that just returns “Hello world!”, we can put this code in a file named `server.js`:
0 commit comments