Skip to content

Commit b968c46

Browse files
committed
Refactor every code-first example to leverage reoslve
1 parent 7dd7812 commit b968c46

8 files changed

+209
-168
lines changed

Diff for: website/pages/docs/authentication-and-express-middleware.mdx

+9-9
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,14 @@ const {
6161
const schema = new GraphQLSchema({
6262
query: new GraphQLObjectType({
6363
name: 'Query',
64-
fields: { ip: { type: GraphQLString } },
64+
fields: {
65+
ip: {
66+
type: GraphQLString,
67+
resolve: (_, args, context) => {
68+
return context.ip;
69+
}
70+
}
71+
},
6572
}),
6673
});
6774
@@ -70,28 +77,21 @@ function loggingMiddleware(req, res, next) {
7077
next();
7178
}
7279
73-
const root = {
74-
ip(args, context) {
75-
return context.ip;
76-
},
77-
};
78-
7980
const app = express();
8081
app.use(loggingMiddleware);
8182
app.all(
8283
'/graphql',
8384
createHandler({
8485
schema: schema,
85-
rootValue: root,
8686
context: (req) => ({
8787
ip: req.raw.ip,
8888
}),
8989
}),
9090
);
9191
app.listen(4000);
9292
console.log('Running a GraphQL API server at localhost:4000/graphql');
93-
````
9493
94+
```
9595
</Tabs.Tab>
9696
</Tabs>
9797

Diff for: website/pages/docs/basic-types.mdx

+12-18
Original file line numberDiff line numberDiff line change
@@ -73,33 +73,27 @@ const schema = new GraphQLSchema({
7373
query: new GraphQLObjectType({
7474
name: 'Query',
7575
fields: {
76-
quoteOfTheDay: { type: GraphQLString },
77-
random: { type: GraphQLFloat },
78-
rollThreeDice: { type: new GraphQLList(GraphQLFloat) },
76+
quoteOfTheDay: {
77+
type: GraphQLString,
78+
resolve: () => Math.random() < 0.5 ? 'Take it easy' : 'Salvation lies within'
79+
},
80+
random: {
81+
type: GraphQLFloat,
82+
resolve: () => Math.random()
83+
},
84+
rollThreeDice: {
85+
type: new GraphQLList(GraphQLFloat),
86+
resolve: () => [1, 2, 3].map((_) => 1 + Math.floor(Math.random() * 6))
87+
},
7988
},
8089
}),
8190
});
8291
83-
// The root provides a resolver function for each API endpoint
84-
const root = {
85-
quoteOfTheDay() {
86-
return Math.random() < 0.5 ? 'Take it easy' : 'Salvation lies within';
87-
},
88-
random() {
89-
return Math.random();
90-
},
91-
rollThreeDice() {
92-
return [1, 2, 3].map((_) => 1 + Math.floor(Math.random() * 6));
93-
},
94-
};
95-
9692
const app = express();
97-
9893
app.all(
9994
'/graphql',
10095
createHandler({
10196
schema: schema,
102-
rootValue: root,
10397
}),
10498
);
10599

Diff for: website/pages/docs/constructing-types.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const userType = new graphql.GraphQLObjectType({
8686
},
8787
});
8888
89-
// Define the Query type
89+
// Define the Query type with inline resolver
9090
const queryType = new graphql.GraphQLObjectType({
9191
name: 'Query',
9292
fields: {

Diff for: website/pages/docs/getting-started.mdx

+9-16
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ npm install graphql --save
2828

2929
## Writing Code
3030

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`:
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`:
3232

3333
<Tabs items={['SDL', 'Code']}>
3434
<Tabs.Tab>
@@ -54,7 +54,7 @@ graphql({
5454
console.log(response);
5555
});
5656

57-
````
57+
```
5858
</Tabs.Tab>
5959
<Tabs.Tab>
6060
```javascript
@@ -65,29 +65,22 @@ const schema = new GraphQLSchema({
6565
query: new GraphQLObjectType({
6666
name: 'Query',
6767
fields: {
68-
hello: { type: GraphQLString },
68+
hello: {
69+
type: GraphQLString,
70+
resolve: () => 'Hello world!'
71+
},
6972
},
7073
}),
7174
});
7275

73-
// The rootValue provides a resolver function for each API endpoint
74-
const rootValue = {
75-
hello() {
76-
return 'Hello world!';
77-
},
78-
};
79-
80-
// Run the GraphQL query '{ hello }' and print out the response
8176
graphql({
8277
schema,
8378
source: '{ hello }',
84-
rootValue,
8579
}).then((response) => {
8680
console.log(response);
8781
});
88-
````
89-
90-
</Tabs.Tab>
82+
```
83+
</Tabs.Tab>
9184
</Tabs>
9285

9386
If you run this with:
@@ -108,4 +101,4 @@ You should see the GraphQL response printed out:
108101

109102
Congratulations - you just executed a GraphQL query!
110103

111-
For practical applications, you'll probably want to run GraphQL queries from an API server, rather than executing GraphQL with a command line tool. To use GraphQL for an API server over HTTP, check out [Running an Express GraphQL Server](./running-an-express-graphql-server).
104+
For practical applications, you'll probably want to run GraphQL queries from an API server, rather than executing GraphQL with a command line tool. To use GraphQL for an API server over HTTP, check out [Running an Express GraphQL Server](./running-an-express-graphql-server).

0 commit comments

Comments
 (0)