https://github.com/codejie/fastify-apollo-step
Set up Apollo Server with Fastify.
https://github.com/codejie/fastify-apollo-step
apollo-server fastify graphql
Last synced: 7 months ago
JSON representation
Set up Apollo Server with Fastify.
- Host: GitHub
- URL: https://github.com/codejie/fastify-apollo-step
- Owner: codejie
- License: mit
- Created: 2018-01-03T13:51:57.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-11-19T02:32:10.000Z (over 5 years ago)
- Last Synced: 2025-09-18T21:58:27.690Z (8 months ago)
- Topics: apollo-server, fastify, graphql
- Language: JavaScript
- Size: 105 KB
- Stars: 9
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastify-apollo-step
Set up Apollo Server with Fastify.
`update dependencies to the latest version.`
```
"dependencies": {
"@types/node": "^14.14.8",
"apollo-server-core": "^2.19.0",
"fastify": "^3.8.0",
"graphql": "^15.4.0",
"graphql-playground-html": "^1.6.29",
"graphql-subscriptions": "^1.1.0",
"graphql-tools": "^7.0.1"
}
```
# [fastify](https://github.com/fastify)
Fast and low overhead web framework, for Node.js
# [apollo](https://github.com/apollographql)
A community building flexible open source tools for GraphQL.
# fastify-apollo-step
As Apollo offical page said that Apollo Server can work with many Node.js HTTP frameworks, but no fastify in that list. So let's make one.
fastify-apollo-step is designed as a plugin of fastify, just call `register` to use it.
fastify-apollo-step also supports graphiql/playground module of Apollo Server.
# Usage
```
npm install --save fastify-apollo-step apollo-server-core graphql fastify graphql-playground-html graphql-tools graphql-subscriptions
```
* by fastify, just need apollo-server-core, not apollo-server
# Options
fastify-apollo-step can be configurated with the follow option object, inclues itself configurations and Apollo Server configurations.
```js
const options = {
path: '/ql',
context: undefined,
gqlSchema: undefined,
typeDefs: typeDefs,
resolvers: resolvers,
subscriptions: {
enabled: true,
path: '/subscriptions',
onConnect: (connectionParams, webSocket) => {
},
onDisconnect: (webSocket, connectionContext) => {
}
},
graphiql: {
enabled: true,
path: '/iql'
},
routeBeforeHandler: function (request, reply, done) {
done();
}
};
```
* **`path`**: GraphQL module endpoint;
* **`gqlSchema`**: GraphQL schema, optional if `typeDefs` and `resolvers` exist;
* **`typeDefs`**: GraphQL type definitions, optional if `schema` exists;
* **`resolvers`**: GraphQL resolver definitions, optional if `schema` exists;
* **`subscriptions`**: Options of subscription;
* **`graphiql`**: Options of GraphiQL of Playground;
* **`routeBeforeHandler`**: beforeHandler function of Fastify while calls QL or iQL;
# Example
```js
const options = {
path: '/ql',
typeDefs: typeDefs,
resolvers: resolvers,
...
};
fastify.register(ApolloFastify, options);
fastify.listen(3000, (err) => {
if (err) {
console.error('server starts failed - ', err);
return;
}
console.log('🚀 Server ready at http://localhost:3000');
});
```
# TypeScript
Do NOT support TypeScript because NOT compatible with latest Fastify.
~~Ready. But I am not good at TypeScript yet.~~
~~To conflict with Fastify Plugin Option, I have to change 'schema' and 'beforeHandler' of FastifyApollo's, Please refer to ./test/ts/subscription.ts.~~
# Test
```
npm run query
npm run mutation
npm run subscription
```
`Notes: Graphsql query must be with 'operationName' now.`
```
query [querybook] {
books {
author
title
}
}
```
# ScreenShots
* Query

* Mutaton

* Subscription
