Skip to content

GraphQL Fastify Server is an implementation of GraphQL.

License

Notifications You must be signed in to change notification settings

pkulcsarnr/graphql-fastify-server

 
 

Repository files navigation

GraphQL Fastify Server

build badge version license

Installation

npm install --save graphql-fastify-server

Usage

const app = fastify();

const server = new GraphQLFastify({
  schema,
  context,
  debug: !isProd,
  playground: {
    introspection: !isProd,
  },
});

server.applyMiddleware({ app, path: '/' });

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.

const cache: Cache<ContextType, Resolvers> = {
  defaultTTL: 1000,
  storage: 'memory',
  policy: {
    add: {
      ttl: 1000,
    },
  },
  extraCacheKeyData: (ctx) => {
    const { locale } = ctx;

    return locale;
  },
};

// --- OR ---

const cache: Cache<ContextType, Resolvers> = {
  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.

const policy: CachePolicy<Resolvers> = {
  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.

const middlewares: Middlewares<ContextType, Resolvers> = [
  {
    handler: (context) => {
      const { isAutheticated } = context;

      if (!isAutheticated) throw new HttpError(401, 'Not authenticated');
    },
    operations: ['add'],
  },
];

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. If you want to contribute, just check how.

License

MIT

About

GraphQL Fastify Server is an implementation of GraphQL.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 80.5%
  • HTML 12.8%
  • JavaScript 6.2%
  • Shell 0.5%