@@ -24,7 +24,7 @@ type User {
24
24
}
25
25
```
26
26
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 `
28
28
29
29
## The `Delegate` class
30
30
@@ -46,13 +46,34 @@ Creates a new instance of `Delegate`.
46
46
47
47
``` js
48
48
const fetch = require (' node-fetch' )
49
- const { Delegate } = require (' graphql-binding' )
49
+ const { Delegate } = require (' graphql-binding/dist/Delegate ' )
50
50
const { HttpLink } = require (' apollo-link-http' )
51
51
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
+ `
53
74
54
75
// Create the remote schema
55
- const endpoint = ` https://example.org/user -service`
76
+ const endpoint = ` https://graphql-binding- example-service-kcbreqbsbh.now.sh `
56
77
const link = new HttpLink ({ uri: endpoint, fetch })
57
78
const schema = makeRemoteExecutableSchema ({ link, schema: typeDefs })
58
79
@@ -94,12 +115,15 @@ const mutation = `
94
115
mutation CreateUserMutation($name: String!) {
95
116
createUser(name: $name) {
96
117
id
118
+ name
97
119
}
98
120
}
99
121
`
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 ) })
103
127
```
104
128
105
129
#### ` delegate `
@@ -127,35 +151,23 @@ delegate(operation: QueryOrMutation, fieldName: string, args: {
127
151
** Example: Hardcode the selection set as a string:**
128
152
129
153
``` 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 (
133
158
` mutation` ,
134
159
` createUser` ,
135
160
args,
136
161
selectionSet
137
- ).then (createUserResult => JSON .stringify (createUserResult))
162
+ )
163
+ .then (createUserResult => console .log (JSON .stringify (createUserResult)))
164
+ .catch ((e ) => { console .error (e) })
138
165
```
139
166
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 >
157
170
158
- > [ Learn more about the ` info ` object.] ( https://blog.graph.cool/graphql-server-basics-demystifying-the-info-argument-in-graphql-resolvers-6f26249f613a )
159
171
160
172
#### ` delegateSubscription `
161
173
0 commit comments