Skip to content
This repository was archived by the owner on Oct 17, 2020. It is now read-only.

Commit 773b399

Browse files
committed
fix(delegate): docs for delegate
1 parent 452464b commit 773b399

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

content/GraphQL-Binding/02-API-Reference.md

+41-29
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type User {
2424
}
2525
```
2626

27-
Assume the GraphQL API above is deployed to this URL: `https://example.org/user-service`
27+
The GraphQL API above is deployed to this URL: `https://graphql-binding-example-service-kcbreqbsbh.now.sh`
2828

2929
## The `Delegate` class
3030

@@ -46,13 +46,34 @@ Creates a new instance of `Delegate`.
4646

4747
```js
4848
const fetch = require('node-fetch')
49-
const { Delegate } = require('graphql-binding')
49+
const { Delegate } = require('graphql-binding/dist/Delegate')
5050
const { HttpLink } = require('apollo-link-http')
5151
const { makeRemoteExecutableSchema } = require('graphql-tools')
52-
const typeDefs = require('./user-service.graphql')
52+
53+
const typeDefs = `
54+
type Query {
55+
user(id: ID!): User
56+
users: [User!]!
57+
}
58+
59+
type Mutation {
60+
createUser(name: String!): User!
61+
updateUser(id: ID!, name: String!): User
62+
deleteUser(id: ID!): User
63+
}
64+
65+
type Subscription {
66+
userCreated: User!
67+
}
68+
69+
type User {
70+
id: ID!
71+
name: String!
72+
}
73+
`
5374

5475
// Create the remote schema
55-
const endpoint = `https://example.org/user-service`
76+
const endpoint = `https://graphql-binding-example-service-kcbreqbsbh.now.sh`
5677
const link = new HttpLink({ uri: endpoint, fetch })
5778
const schema = makeRemoteExecutableSchema({ link, schema: typeDefs })
5879

@@ -94,12 +115,15 @@ const mutation = `
94115
mutation CreateUserMutation($name: String!) {
95116
createUser(name: $name) {
96117
id
118+
name
97119
}
98120
}
99121
`
100-
const variables = { name: `Alice` }
101-
userServiceDelegate.request(query, variables)
102-
.then(createUserResult => JSON.stringify(createUserResult))
122+
const variables = { name: `Andy` }
123+
124+
delegate.request(mutation, variables)
125+
.then(createUserResult => console.log(JSON.stringify(createUserResult)))
126+
.catch((e) => { console.error(e) })
103127
```
104128

105129
#### `delegate`
@@ -127,35 +151,23 @@ delegate(operation: QueryOrMutation, fieldName: string, args: {
127151
**Example: Hardcode the selection set as a string:**
128152

129153
```js
130-
const args = { name: `Alice` }
131-
const selectionSet = `{ id }`
132-
userServiceDelegate.delegate(
154+
const args = { name: `Rowan` }
155+
const selectionSet = `{ id, name }`
156+
157+
delegate.delegate(
133158
`mutation`,
134159
`createUser`,
135160
args,
136161
selectionSet
137-
).then(createUserResult => JSON.stringify(createUserResult))
162+
)
163+
.then(createUserResult => console.log(JSON.stringify(createUserResult)))
164+
.catch((e) => { console.error(e) })
138165
```
139166

140-
**Example: Dynamic selection set based on the `info` object inside a GraphQL resolver**:
141-
142-
```js
143-
const Mutation = {
144-
createUserWithRandomName: (parent, args, context, info) => {
145-
const randomName = generateRandomName()
146-
const args = { name: randomName }
147-
return userServiceDelegate.delegate(
148-
`mutation`,
149-
`createUser`,
150-
args,
151-
// passing the info AST from the parent resolver
152-
info
153-
)
154-
}
155-
}
156-
```
167+
<div class="glitch-embed-wrap" style="height: 420px; width: 100%;">
168+
<iframe src="https://glitch.com/embed/#!/embed/silver-pilot?path=server.js&sidebarCollapsed=true" alt="silver-pilot on glitch" style="height: 100%; width: 100%; border: 0;"></iframe>
169+
</div>
157170

158-
> [Learn more about the `info` object.](https://blog.graph.cool/graphql-server-basics-demystifying-the-info-argument-in-graphql-resolvers-6f26249f613a)
159171

160172
#### `delegateSubscription`
161173

0 commit comments

Comments
 (0)