Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

nullfx/redis-graphql-server

Repository files navigation

Redis GraphQL Server

This is a simple redis based graph-ql server using Node Redis and Apollo Server and Babel.

Not all redis commands are implemented, but the ones that have been are documented here:

Queries

  • get ( key: String! ): String
    • Get the value of key. If the key does not exist the special value nil is returned. An error is returned if the value stored at key is not a string, because GET only handles string values.
  • getrange ( key: String!, start: Int!, end: Int! ): String
    • Returns the substring of the string value stored at key, determined by the offsets start and end (both are inclusive). Negative offsets can be used in order to provide an offset starting from the end of the string. So -1 means the last character, -2 the penultimate and so forth.
  • keys ( pattern: String! ): [String]!
    • Returns all keys matching pattern.
  • mget ( keys: [String]! ): String
    • Returns the values of all specified keys. For every key that does not hold a string value or does not exist, the special value nil is returned. Because of this, the operation never fails.
  • pttl ( key: String! ): Int!
    • Like TTL this command returns the remaining time to live of a key that has an expire set, with the sole difference that TTL returns the amount of remaining time in seconds while PTTL returns it in milliseconds.
  • strlen ( key: String! ): Int!
    • Returns the length of the string value stored at key. An error is returned when key holds a non-string value.
  • ttl ( key: String! ): Int!
    • Returns the remaining time to live of a key that has a timeout. This introspection capability allows a Redis client to check how many seconds a given key will continue to be part of the dataset.

Mutations

  • append ( key: String!, value: String! ): Int!
    • If key already exists and is a string, this command appends the value at the end of the string. If key does not exist it is created and set as an empty string, so APPEND will be similar to SET in this special case.
  • decr ( key: String! ): Int!
    • Decrements the number stored at key by one. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.
  • decrby ( key: String!, decrement: Int! ): Int!
    • Decrements the number stored at key by decrement. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.
  • del ( key: String! ): Int!
    • Removes the specified keys. A key is ignored if it does not exist.
  • expire ( key: String!, seconds: Int! ): Int!
    • Set a timeout on key. After the timeout has expired, the key will automatically be deleted. A key with an associated timeout is often said to be volatile in Redis terminology.
  • incr ( key: String! ): Int!
    • Increments the number stored at key by one. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.
  • incrby ( key: String!, increment: Int! ): Int!
    • Increments the number stored at key by increment. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.
  • pexpire ( key: String!, milliseconds: Int! ): Int!
    • This command works exactly like EXPIRE but the time to live of the key is specified in milliseconds instead of seconds.
  • persist ( key: String! ): Int!
    • Remove the existing timeout on key, turning the key from volatile (a key with an expire set) to persistent (a key that will never expire as no timeout is associated).
  • rename ( key: String!, newkey: String! ): Boolean!
    • Renames key to newkey. It returns an error when key does not exist. If newkey already exists it is overwritten, when this happens RENAME executes an implicit DEL operation, so if the deleted key contains a very big value it may cause high latency even if RENAME itself is usually a constant-time operation.
  • renamenx ( key: String!, newkey: String! ): Int!
    • Renames key to newkey if newkey does not yet exist. It returns an error when key does not exist.
  • set ( key: String!, value: String! ): Boolean!
    • Set key to hold the string value. If key already holds a value, it is overwritten, regardless of its type. Any previous time to live associated with the key is discarded on successful SET operation.
  • setex ( key: String!, seconds: Int!, value: String! ): String!
    • Set key to hold the string value and set key to timeout after a given number of seconds.
  • setnx ( key: String!, value: String! ): Boolean!
    • Set key to hold string value if key does not exist. In that case, it is equal to SET. When key already holds a value, no operation is performed. SETNX is short for "SET if Not eXists".
  • setrange ( key: String!, offset: Int!, value: String! ): Int!
    • Overwrites part of the string stored at key, starting at the specified offset, for the entire length of value. If the offset is larger than the current length of the string at key, the string is padded with zero-bytes to make offset fit. Non-existing keys are considered as empty strings, so this command will make sure it holds a string large enough to be able to set value at offset.

Usage

Docker

DockerHub repository https://hub.docker.com/r/stevewhitley/redis-graphql

This server uses environment variables to determine the host / ip address of the redis server, you can pass them in to the container's configuration as shown below.

docker run -d -p 8181:80 -e "REDIS_SERVERNAME=cloud.redis.server" stevewhitley/redis-graphql

Development

To assist running the server locally this module comes with some helper shell scripts. setup.sh will install dependencies (docker, nodejs, npm, and dependent node modules)

sh setup.sh

Once dependencies have been installed / updated and downloaded you can run the server by running the following command

sh run.sh

which will start the redis docker container (if it hasn't been started), and start the graph-ql front end.

Alternatively you can run

npm start

to run the graphql server, assuming your redis server is already up and running / you don't need the docker redis server that run.sh provides.

Once running you can access the GraphQL Playground by browsing to

http://localhost:8086/graphql

Note, if you've modified the port in the .env file the URL displayed in the console when the app is running will display the location to browse to

Configuration

By default run.sh creates a local redis container for the server to connect to. If you have a stand-alone redis server you can edit the .env file and specify the new server host name or ip address and port which will allow the server to connect to redis regardless of where its running.

you can specify an alternate graph-ql server port here as well

# .env

# hostname or ip address of redis server
REDIS_SERVERNAME=localhost
# port where redis server is listening
REDIS_PORT=6379
# the port where the graphql service will listen
GRAPHQL_PORT=8086