Skip to content

Update Tutorial-ObjectTypes.md #2892

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

Closed
wants to merge 2 commits into from
Closed
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
36 changes: 18 additions & 18 deletions docs/Tutorial-ObjectTypes.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ type Query {
}
```

If we wanted to have more and more methods based on a random die over time, we could implement this with a `RandomDie` object type instead.
If we wanted to have more and more methods based on a random dice over time, we could implement this with a `RandomDice` object type instead.

```graphql
type RandomDie {
type RandomDice {
roll(numRolls: Int!): [Int]
}

type Query {
getDie(numSides: Int): RandomDie
getDice(numSides: Int): RandomDice
}
```

Instead of a root-level resolver for the `RandomDie` type, we can instead use an ES6 class, where the resolvers are instance methods. This code shows how the `RandomDie` schema above can be implemented:
Instead of a root-level resolver for the `RandomDice` type, we can instead use an ES6 class, where the resolvers are instance methods. This code shows how the `RandomDice` schema above can be implemented:

```js
class RandomDie {
class RandomDice {
constructor(numSides) {
this.numSides = numSides;
}
Expand All @@ -50,27 +50,27 @@ class RandomDie {
}

var root = {
getDie: function ({ numSides }) {
return new RandomDie(numSides || 6);
getDice: function ({ numSides }) {
return new RandomDice(numSides || 6);
},
};
```

For fields that don't use any arguments, you can use either properties on the object or instance methods. So for the example code above, both `numSides` and `rollOnce` can actually be used to implement GraphQL fields, so that code also implements the schema of:

```graphql
type RandomDie {
type RandomDice {
numSides: Int!
rollOnce: Int!
roll(numRolls: Int!): [Int]
}

type Query {
getDie(numSides: Int): RandomDie
getDice(numSides: Int): RandomDice
}
```

Putting this all together, here is some sample code that runs a server with this GraphQL API:
Putting this altogether, here is some sample code that runs a server with this GraphQL API:

```js
var express = require('express');
Expand All @@ -79,19 +79,19 @@ var { buildSchema } = require('graphql');

// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
type RandomDie {
type RandomDice {
numSides: Int!
rollOnce: Int!
roll(numRolls: Int!): [Int]
}

type Query {
getDie(numSides: Int): RandomDie
getDice(numSides: Int): RandomDice
}
`);

// This class implements the RandomDie GraphQL type
class RandomDie {
// This class implements the RandomDice GraphQL type
class RandomDice {
constructor(numSides) {
this.numSides = numSides;
}
Expand All @@ -111,8 +111,8 @@ class RandomDie {

// The root provides the top-level API endpoints
var root = {
getDie: function ({ numSides }) {
return new RandomDie(numSides || 6);
getDice: function ({ numSides }) {
return new RandomDice(numSides || 6);
},
};

Expand All @@ -130,11 +130,11 @@ app.listen(4000, () => {
});
```

When you issue a GraphQL query against an API that returns object types, you can call multiple methods on the object at once by nesting the GraphQL field names. For example, if you wanted to call both `rollOnce` to roll a die once, and `roll` to roll a die three times, you could do it with this query:
When you issue a GraphQL query against an API that returns object types, you can call multiple methods on the object at once by nesting the GraphQL field names. For example, if you wanted to call both `rollOnce` to roll a dice once, and `roll` to roll a dice three times, you could do it with this query:

```graphql
{
getDie(numSides: 6) {
getDice(numSides: 6) {
rollOnce
roll(numRolls: 3)
}
Expand Down