Skip to content

Commit 1e937f7

Browse files
committed
document mutations
1 parent 2e30fca commit 1e937f7

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This library is intended to simplify the network aspect of data consumption for
2121
|| Build time validations & optimizations | |
2222
|| Client-Side Composition | with improved execution planner (based on GraphQL-Mesh) |
2323
|| Raw Execution (standalone mode) | without a wrapping GraphQL client |
24+
|| Local (client-side) Mutations | |
2425
|| Integration with `@apollo/client` | |
2526
|| Integration with `urql` | |
2627
|| TypeScript support | with built-in GraphQL Codegen and `TypedDocumentNode` |
@@ -377,6 +378,89 @@ async function main() {
377378

378379
> You can find a [TypeScript project example here](./examples/urql/).
379380

381+
#### Client-Side Mutations
382+
383+
Due to the nature of Graph-Client setup, it is possible to add client-side schema, that you can later bridge to run any arbitrary code.
384+
385+
This is helpful since you can implement custom code as part of your GraphQL schema, and have it as unified application schema that is easier to track and develop.
386+
387+
> This document explains how to add custom mutations, but in fact you can add any GraphQL operation (query/mutation/subscriptions). See [Extending the unified schema article](https://www.graphql-mesh.com/docs/guides/extending-unified-schema) for more information about this feature.
388+
389+
To get started, define a `additionalTypeDefs` section in your config file:
390+
391+
```yml
392+
additionalTypeDefs: |
393+
type Mutation {
394+
doSomething(input: SomeCustomInput!): Boolean!
395+
}
396+
397+
input SomeCustomInput {
398+
field: String!
399+
}
400+
```
401+
402+
Then, add a pointer to a custom GraphQL resolvers file:
403+
404+
```yml
405+
additionalResolvers:
406+
- './resolvers'
407+
```
408+
409+
Now, create `resolver.js` (or, `resolvers.ts`) in your project, and implement your custom mutation:
410+
411+
```js
412+
module.exports = {
413+
Mutation: {
414+
doSomething: async (root, args, context, info) => {
415+
// Here, you can run anything you wish.
416+
// For example, use `web3` lib, connect a wallet and so on.
417+
418+
return true
419+
},
420+
},
421+
}
422+
```
423+
424+
If you are using TypeScript, you can also get fully type-safe signature by doing:
425+
426+
```ts
427+
import { Resolvers } from './.graphclient'
428+
429+
// Now it's fully typed!
430+
const resolvers: Resolvers = {
431+
Mutation: {
432+
doSomething: async (root, args, context, info) => {
433+
// Here, you can run anything you wish.
434+
// For example, use `web3` lib, connect a wallet and so on.
435+
436+
return true
437+
},
438+
},
439+
}
440+
441+
export default resolvers
442+
```
443+
444+
If you need to inject runtime variables into your GraphQL execution `context`, you can use the following snippet:
445+
446+
```ts
447+
client.execute(
448+
MY_QUERY,
449+
{},
450+
{
451+
myHelper: {}, // this will be available in your Mutation resolver as `context.myHelper`
452+
},
453+
)
454+
```
455+
456+
> [You can read more about client-side schema extensions here](https://www.graphql-mesh.com/docs/guides/extending-unified-schema)
457+
458+
> [You can also delegate and call Query fields as part of your mutation](https://www.graphql-mesh.com/docs/guides/extending-unified-schema#using-the-sdk-to-fetch-sources)
459+
380460
## License
381461

382462
MIT
463+
464+
```
465+
466+
```

0 commit comments

Comments
 (0)