1.4.29 • Published 3 months ago

graphql-fastify-server v1.4.29

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

GraphQL Fastify Server

build badge version license

Installation

npm install --save graphql-fastify-server

Usage

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.

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'],
  },
];

Subscriptions

To enable subscriptions you need to set to true the property subscriptions on the server initialization

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

type Query {
  add(x: Int!, y: Int!): Int!
}

type Subscription {
  added(x: Int!, y: Int!): Int!
}
const resolvers = {
  Query: {
    add: async (_: null, { x, y }: { x: number; y: number }, { pubsub }: ContextType): Promise<number> => {
      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. If you want to contribute, just check how.

License

MIT

1.4.28

4 months ago

1.4.29

3 months ago

1.4.27

6 months ago

1.4.26

7 months ago

1.4.25

8 months ago

1.4.24

9 months ago

1.4.22

9 months ago

1.4.23

9 months ago

1.4.21

1 year ago

1.4.17

1 year ago

1.4.15

1 year ago

1.4.16

1 year ago

1.4.9

2 years ago

1.4.11

1 year ago

1.4.10

2 years ago

1.4.13

1 year ago

1.4.12

1 year ago

1.4.14

1 year ago

1.4.8

2 years ago

1.4.7

2 years ago

1.4.6

2 years ago

1.4.5

2 years ago

1.4.4

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.3.3

2 years ago

1.4.1

2 years ago

1.3.2

2 years ago

1.4.0

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.0

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago