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

Commit 452464b

Browse files
committed
fix(glitch): add a glitch iframe
1 parent 93dc342 commit 452464b

File tree

1 file changed

+20
-44
lines changed

1 file changed

+20
-44
lines changed

content/GraphQL-Binding/01-Overview.md

+20-44
Original file line numberDiff line numberDiff line change
@@ -133,34 +133,28 @@ When responding to webhooks from 3rd party services, bindings are useful to acce
133133

134134
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.
135135

136-
```js
137-
138-
const express = require('express')
136+
<div class="glitch-embed-wrap" style="height: 420px; width: 100%;">
137+
<iframe src="https://glitch.com/embed/#!/embed/holy-shad?path=server.js&previewSize=0&sidebarCollapsed=true" alt="holy-shad on glitch" style="height: 100%; width: 100%; border: 0;"></iframe>
138+
</div>
139139

140-
const app = express()
140+
To test out a webhook against this Glitch example you can run the following:
141141

142-
const userBinding = require('graphql-binding-users').default;
142+
```sh
143+
curl -d '{"name": "Andy"}' -H "Content-Type: application/json" -X POST https://holy-shad.glitch.me/users/created
144+
```
143145

144-
app.post('/users/created', function (req) {
145-
const name = req.body.name;
146+
An example response from running this command is below:
146147

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
157150
```
158151

152+
159153
### 3. Usage in a GraphQL gateway
160154

161155
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.
162156

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

165159
This means the gateway layer will have the following schema:
166160

@@ -175,37 +169,19 @@ type Query {
175169
}
176170

177171
type Mutation {
178-
createUserWithRandomName: User!
172+
createUserWithAllowedName: User!
179173
}
180174
```
181175

182-
Now, when implementing the resolvers for this schema, you're going to delegate the queries to the underlying GraphQL microservice - using the binding instance from the `'graphql-binding-users'` package.
176+
Now, when implementing the resolvers for this schema, you're going to delegate the queries to the underlying GraphQL microservice - using the binding instance from the `'graphql-binding-example'` package.
183177

184-
```js
185-
const userBinding = require('graphql-binding-users').default
186-
const generateName = require('sillyname')
187-
188-
const resolvers = {
189-
Query: {
190-
user: async (parent, args, context, info) => {
191-
const users = await userBinding.query.users({}, info)
192-
return users.find(user => user.id === args.id)
193-
},
194-
},
195-
Mutation: {
196-
createUserWithRandomName: (parent, args, context, info) => {
197-
const sillyName = generateName()
198-
return userBinding.mutation.createUser(
199-
{
200-
name: sillyname,
201-
},
202-
info,
203-
)
204-
}
205-
}
206-
```
178+
<div class="glitch-embed-wrap" style="height: 420px; width: 100%;">
179+
<iframe src="https://glitch.com/embed/#!/embed/twilight-beauty?path=server.js&sidebarCollapsed=true" alt="twilight-beauty on glitch" style="height: 100%; width: 100%; border: 0;"></iframe>
180+
</div>
181+
182+
Play around with the hosted playground to see the binding in action!
207183

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.
209185

210186
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).
211187

0 commit comments

Comments
 (0)