Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/svengau/apollo-link-directive
An Apollo Link to easily add custom directives in your GraphQL queries.
https://github.com/svengau/apollo-link-directive
Last synced: 3 days ago
JSON representation
An Apollo Link to easily add custom directives in your GraphQL queries.
- Host: GitHub
- URL: https://github.com/svengau/apollo-link-directive
- Owner: svengau
- License: mit
- Created: 2019-10-09T23:20:08.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-10-23T05:05:28.000Z (23 days ago)
- Last Synced: 2024-10-24T20:29:26.946Z (21 days ago)
- Language: TypeScript
- Size: 32.2 KB
- Stars: 7
- Watchers: 3
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Apollo Directive Link
[![Build Status](https://travis-ci.org/svengau/apollo-link-directive.svg?branch=master)](https://travis-ci.org/svengau/apollo-link-directive)
## Purpose
An Apollo Link to easily add custom directives in your GraphQL queries.
## Installation
```bash
npm install apollo-link-directive --save
# or
yarn add apollo-link-directive```
## Usage
### Basics
This sample code shows how to use this Apollo Link to change the HttpLink uri with the "admin" directive:
```js
import { from } from 'apollo-link';
import { HttpLink } from 'apollo-link-http';
import { DirectiveLink } from "apollo-link-directive";const adminDirectiveLink = new DirectiveLink([
{
directive: 'admin',
callback: (operation, _forward) => operation.setContext({ uri: '/graphql-admin' })
},
]);const httpLink = new HttpLink({
uri: '/graphql',
});// Configure the ApolloClient
const client = new ApolloClient({
link: from([adminDirectiveLink, httpLink]),
cache: new InMemoryCache(),
});```
this sample code will update all queries having admin directive with uri: /graphql-admin, for example from this query:
```
const query = gql`
query luke {
person @admin {
name
}
}
`;```
## Options
The options you can pass to DirectiveLink are an array of directives like this:
- `directive`: the name of the directive
- `callback`: a callback function with args `operation` and `forward`
- `remove`: an option to remove the directive from the code. Default: true## Thanks
Setting up the project has been largely inspired by the wonderful [apollo-link-rest](https://github.com/apollographql/apollo-link-rest) project.