You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 17, 2020. It is now read-only.
Copy file name to clipboardExpand all lines: content/GraphQL-Binding/01-Overview.md
+20-44
Original file line number
Diff line number
Diff line change
@@ -133,34 +133,28 @@ When responding to webhooks from 3rd party services, bindings are useful to acce
133
133
134
134
For this example, consider an application which responds to webhooks from a service that has its own user accounts system. In this service, the system sends a HTTP `POST` whenever a new user is created and we want to add that user to our database.
curl -d '{"name": "Andy"}' -H "Content-Type: application/json" -X POST https://holy-shad.glitch.me/users/created
144
+
```
143
145
144
-
app.post('/users/created', function (req) {
145
-
constname=req.body.name;
146
+
An example response from running this command is below:
146
147
147
-
// Create a new `User`
148
-
userBinding.mutation
149
-
.createUser(
150
-
{
151
-
name:'Alice',
152
-
},
153
-
`{ id, name }`,
154
-
)
155
-
.then(({ id, name }) =>console.log(`The ID of the created user is: ${id} and the name is ${name}`))
156
-
});
148
+
```sh
149
+
The ID of the created user is: cjkpu8tboo6b00b77sdbnaii5 and the name is Andy
157
150
```
158
151
152
+
159
153
### 3. Usage in a GraphQL gateway
160
154
161
155
When building gateways for GraphQL-based microservices, bindings are especially helpful. In the previous example, we have demonstrated the API where the _selection set_ for the constructed query is passed as a string. For this use case, the binding API allows to pass on the `info` argument of GraphQL resolvers.
162
156
163
-
For this example, consider the `graphql-binding-users` binding representing the SDK of a GraphQL microservice and assume you are now building a GraphQL gateway layer on top of it to adjust the functionality. Let's say we want to add a search feature to the `users` query and also generate random names for the `User`s instead of letting them choose their own names.
157
+
For this example, consider the `graphql-binding-example` binding representing the SDK of a GraphQL microservice and assume you are now building a GraphQL gateway layer on top of it to adjust the functionality. Let's say we want to generate a `User` from an allowed list of names:
164
158
165
159
This means the gateway layer will have the following schema:
166
160
@@ -175,37 +169,19 @@ type Query {
175
169
}
176
170
177
171
typeMutation {
178
-
createUserWithRandomName: User!
172
+
createUserWithAllowedName: User!
179
173
}
180
174
```
181
175
182
-
Now, whenimplementingtheresolversforthisschema, you're going to delegate the queries to the underlying GraphQL microservice - using the binding instance from the `'graphql-binding-users'` package.
176
+
Now, whenimplementingtheresolversforthisschema, you're going to delegate the queries to the underlying GraphQL microservice - using the binding instance from the `'graphql-binding-example'` package.
Play around with the hosted playground to see the binding in action!
207
183
208
-
For the `users` and `createUserWithRandomName` resolvers, the binding will behave exactly like in the previous example and construct a GraphQL query/mutation to send to the API.
184
+
For the `user` and `createUserWithAllowedName` resolvers, the binding will behave exactly like in the previous example and construct a GraphQL query/mutation to send to the API.
209
185
210
186
The big difference to the previous example with the simple Node script is that now the selection set is not _hardcoded_ any more. Instead, it is provided through the [`info`](https://blog.graph.cool/graphql-server-basics-demystifying-the-info-argument-in-graphql-resolvers-6f26249f613a) object of the incoming query/mutation. The `info` object carries the [abstract syntax tree](https://medium.com/@cjoudrey/life-of-a-graphql-query-lexing-parsing-ca7c5045fad8) (AST) of the incoming GraphQL query, meaning it knows the requested fields as well as any arguments and can simply pass them along to the underlying GraphQL API - this is called query [delegation](https://blog.graph.cool/graphql-schema-stitching-explained-schema-delegation-4c6caf468405).
0 commit comments