Skip to content

Refactor every code-first example to leverage resolve #4372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions website/pages/docs/authentication-and-express-middleware.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ const {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: { ip: { type: GraphQLString } },
fields: {
ip: {
type: GraphQLString,
resolve: (_, args, context) => {
return context.ip;
}
}
},
}),
});
Expand All @@ -70,28 +77,21 @@ function loggingMiddleware(req, res, next) {
next();
}
const root = {
ip(args, context) {
return context.ip;
},
};
const app = express();
app.use(loggingMiddleware);
app.all(
'/graphql',
createHandler({
schema: schema,
rootValue: root,
context: (req) => ({
ip: req.raw.ip,
}),
}),
);
app.listen(4000);
console.log('Running a GraphQL API server at localhost:4000/graphql');
````
```
</Tabs.Tab>
</Tabs>

Expand Down
32 changes: 13 additions & 19 deletions website/pages/docs/basic-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const root = {
return Math.random();
},
rollThreeDice() {
return [1, 2, 3].map((\_) => 1 + Math.floor(Math.random() \* 6));
return [1, 2, 3].map((_) => 1 + Math.floor(Math.random() * 6));
},
};

Expand Down Expand Up @@ -73,33 +73,27 @@ const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
quoteOfTheDay: { type: GraphQLString },
random: { type: GraphQLFloat },
rollThreeDice: { type: new GraphQLList(GraphQLFloat) },
quoteOfTheDay: {
type: GraphQLString,
resolve: () => Math.random() < 0.5 ? 'Take it easy' : 'Salvation lies within'
},
random: {
type: GraphQLFloat,
resolve: () => Math.random()
},
rollThreeDice: {
type: new GraphQLList(GraphQLFloat),
resolve: () => [1, 2, 3].map((_) => 1 + Math.floor(Math.random() * 6))
},
},
}),
});

// The root provides a resolver function for each API endpoint
const root = {
quoteOfTheDay() {
return Math.random() < 0.5 ? 'Take it easy' : 'Salvation lies within';
},
random() {
return Math.random();
},
rollThreeDice() {
return [1, 2, 3].map((_) => 1 + Math.floor(Math.random() * 6));
},
};

const app = express();

app.all(
'/graphql',
createHandler({
schema: schema,
rootValue: root,
}),
);

Expand Down
2 changes: 1 addition & 1 deletion website/pages/docs/constructing-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const userType = new graphql.GraphQLObjectType({
},
});

// Define the Query type
// Define the Query type with inline resolver
const queryType = new graphql.GraphQLObjectType({
name: 'Query',
fields: {
Expand Down
25 changes: 9 additions & 16 deletions website/pages/docs/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ npm install graphql --save

## Writing Code

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

<Tabs items={['SDL', 'Code']}>
<Tabs.Tab>
Expand All @@ -54,7 +54,7 @@ graphql({
console.log(response);
});

````
```
</Tabs.Tab>
<Tabs.Tab>
```javascript
Expand All @@ -65,29 +65,22 @@ const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
hello: { type: GraphQLString },
hello: {
type: GraphQLString,
resolve: () => 'Hello world!'
},
},
}),
});

// The rootValue provides a resolver function for each API endpoint
const rootValue = {
hello() {
return 'Hello world!';
},
};

// Run the GraphQL query '{ hello }' and print out the response
graphql({
schema,
source: '{ hello }',
rootValue,
}).then((response) => {
console.log(response);
});
````

</Tabs.Tab>
```
</Tabs.Tab>
</Tabs>

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

Congratulations - you just executed a GraphQL query!

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).
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).
Loading