Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rpvsilva/graphql-fastify-server
GraphQL Fastify Server is an implementation of GraphQL.
https://github.com/rpvsilva/graphql-fastify-server
fastify graphql hacktoberfest nodejs performance typescript
Last synced: 5 days ago
JSON representation
GraphQL Fastify Server is an implementation of GraphQL.
- Host: GitHub
- URL: https://github.com/rpvsilva/graphql-fastify-server
- Owner: rpvsilva
- License: mit
- Created: 2022-02-28T14:48:32.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-12-15T16:18:17.000Z (7 days ago)
- Last Synced: 2024-12-15T17:25:10.778Z (7 days ago)
- Topics: fastify, graphql, hacktoberfest, nodejs, performance, typescript
- Language: TypeScript
- Homepage:
- Size: 1.16 MB
- Stars: 38
- Watchers: 0
- Forks: 6
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# GraphQL Fastify Server
![build badge](https://github.com/rpvsilva/graphql-fastify-server/actions/workflows/on_push.yaml/badge.svg)
[](https://npmjs.com/package/graphql-fastify-server)
![version](https://img.shields.io/npm/v/graphql-fastify-server?color=brightgreen&label=version)
[](https://snyk.io/test/github/rpvsilva/graphql-fastify-server)
![license](https://img.shields.io/github/license/rpvsilva/graphql-fastify-server?color=blue&logo=github)- [Installation](#installation)
- [Usage](#usage)
- [Using cache](#using-cache)
- [Middlewares](#middlewares)
- [Subscriptions](#subscriptions)
- [Liveness & Readiness](#liveness--readiness)
- [Contributing](#contributing)
- [License](#license)## Installation
```shell
npm install --save graphql-fastify-server
```## Usage
```javascript
const app = fastify();const server = new GraphQLFastify({
schema,
context,
playground: {
introspection: !isProd,
},
});server.applyMiddleware({ app, path: '/' }).then(() => {
app.listen({ port: +PORT }, () => {
console.log(`Server listening on port http://0.0.0.0:${PORT}`);
});
});```
### Using cache
On GraphQL Fastify Server you can use two types of cache, one is memory cache and the other is using a Redis instance. Then you inject the cache variable into the GraphQLFastify instance.```javascript
const cache: Cache = {
defaultTTL: 1000,
storage: 'memory',
policy: {
add: {
ttl: 1000,
},
},
extraCacheKeyData: (ctx) => {
const { locale } = ctx;return locale;
},
};// --- OR ---
const cache: Cache = {
defaultTTL: 1000,
storage: new Redis({
host: 'localhost',
port: 6379,
}),
policy: {
add: {
ttl: 1000,
},
},
extraCacheKeyData: (ctx) => {
const { locale } = ctx;return locale;
},
};
```Also, you can define the query scope between `PUBLIC` and `PRIVATE` for caching. This tells the cache to use the authorization token from the headers to compute the cache key.
```javascript
const policy: CachePolicy = {
add: {
ttl: 1000,
scope: 'PUBLIC',
},
sub: {
ttl: 1500,
scope: 'PRIVATE',
}
}
```### Middlewares
You can use middlewares at the Fastify and you can define in which operations you want to execute them.
```javascript
const middlewares: Middlewares = [
{
handler: (context) => {
const { isAutheticated } = context;if (!isAutheticated) throw new HttpError(401, 'Not authenticated');
},
operations: ['add'],
},
];
```### Subscriptions
To enable subscriptions you need to set to `true` the property `subscriptions` on the server initialization
```javascript
const server = new GraphQLFastify({
schema,
context,
subscriptions: true
});
```On resolvers side, you can use the `pubsub` property available on context. With the following schema, you can use the subscriptions feature like:
> `pubsub` property is only available subscriptions are enabled
```graphql
type Query {
add(x: Int!, y: Int!): Int!
}type Subscription {
added(x: Int!, y: Int!): Int!
}
``````javascript
const resolvers = {
Query: {
add: async (_: null, { x, y }: { x: number; y: number }, { pubsub }: ContextType): Promise => {
await pubsub.publish(`add-${x}-${y}`, { add: x + y });return x + y;
},
},
Subscription: {
added: {
subscribe: async (_: null, { x, y }: { x: number; y: number }, { pubsub }: ContextType) => {
return pubsub.subscribe(`add-${x}-${y}`);
},
},
},
};
```## Liveness & Readiness
To check the health of the server you can make a GET request to the endpoint `/server-health`
## Contributing
If you have any doubt or to point out an issue just go ahead and create a new [issue](https://github.com/rpvsilva/graphql-fastify-server/issues/new). If you want to contribute, just [check](./CONTRIBUTING.md) how.## License
[MIT](./LICENSE)