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
Copy file name to clipboardExpand all lines: website/pages/basic-types.mdx
+57Lines changed: 57 additions & 0 deletions
Display the source diff
Display the rich diff
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/getting-started.mdx
+42-9Lines changed: 42 additions & 9 deletions
Display the source diff
Display the rich diff
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,16 @@ 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`:
Copy file name to clipboardExpand all lines: website/pages/passing-arguments.mdx
+95Lines changed: 95 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
title: Passing Arguments
3
3
---
4
4
5
+
import { Tabs } from'nextra/components';
6
+
5
7
Just like a REST API, it's common to pass arguments to an endpoint in a GraphQL API. By defining the arguments in the schema language, typechecking happens automatically. Each argument must be named and have a type. For example, in the [Basic Types documentation](/basic-types/) we had an endpoint called `rollThreeDice`:
6
8
7
9
```graphql
@@ -12,11 +14,44 @@ type Query {
12
14
13
15
Insteadofhardcoding “three”, wemightwantamoregeneralfunctionthatrolls `numDice` dice, eachofwhichhave `numSides` sides. WecanaddargumentstotheGraphQLschema language like this:
14
16
17
+
<Tabs items={['Template', 'Classes']}>
18
+
<Tabs.Tab>
15
19
```graphql
16
20
typeQuery {
17
21
rollDice(numDice: Int!, numSides: Int): [Int]
18
22
}
19
23
```
24
+
</Tabs.Tab>
25
+
<Tabs.Tab>
26
+
```js
27
+
const {
28
+
GraphQLObjectType,
29
+
GraphQLNonNull,
30
+
GraphQLInt,
31
+
GraphQLString,
32
+
GraphQLList,
33
+
GraphQLFloat,
34
+
} = require('graphql');
35
+
36
+
newGraphQLObjectType({
37
+
name: 'Query',
38
+
fields: {
39
+
rollDice: {
40
+
type: newGraphQLList(GraphQLFloat),
41
+
args: {
42
+
numDice: {
43
+
type: newGraphQLNonNull(GraphQLInt)
44
+
},
45
+
numSides: {
46
+
type: newGraphQLNonNull(GraphQLInt)
47
+
},
48
+
},
49
+
},
50
+
},
51
+
})
52
+
```
53
+
</Tabs.Tab>
54
+
</Tabs>
20
55
21
56
The exclamation point in `Int!` indicates that `numDice` can't be null, which means we can skip a bit of validation logic to make our server code simpler. We can let `numSides` be null and assume that by default a die has 6 sides.
22
57
@@ -52,6 +87,8 @@ If you're familiar with destructuring, this is a bit nicer because the line of c
52
87
53
88
The entire code for a server that hosts this `rollDice` API is:
console.log('Running a GraphQL API server at localhost:4000/graphql');
181
+
```
182
+
</Tabs.Tab>
183
+
</Tabs>
89
184
90
185
When you call this API, you have to pass each argument by name. So for the server above, you could issue this GraphQL query to roll three six-sided dice:
Copy file name to clipboardExpand all lines: website/pages/running-an-express-graphql-server.mdx
+54-13Lines changed: 54 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,28 +3,28 @@ title: Running an Express GraphQL Server
3
3
sidebarTitle: Running Express + GraphQL
4
4
---
5
5
6
+
import { Tabs } from'nextra/components';
7
+
6
8
The simplest way to run a GraphQL API server is to use [Express](https://expressjs.com), a popular web application framework for Node.js. You will need to install two additional dependencies:
7
9
8
-
```bash
10
+
```sh npm2yarn
9
11
npm install express graphql-http graphql --save
10
12
```
11
13
12
14
Let's modify our “hello world” example so that it's an API server rather than a script that runs a single query. We can use the 'express' module to run a webserver, and instead of executing a query directly with the `graphql` function, we can use the `graphql-http` library to mount a GraphQL API server on the “/graphql” HTTP endpoint:
0 commit comments