npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-file-sender

v1.0.29

Published

custom Node Express, Apollo Client plugin to send pdfs over graphql

Downloads

14

Readme

graphql-file-sender

GraphQL File Sender is an Express and Node.js application that allows you to send files from a server to a client using the GraphQL API. It includes encoding and decoding of files in base64 format for seamless transmission and retrieval. This project also provides an Apollo Client and React plugin for easy integration into your existing React applications.

Installation

Option 1: Install using npm

npm install graphql-file-sender

Option 2: Install using yarn

yarn add graphql-file-sender

Option 3: Clone the repository

git clone https://github.com/BenSimmers/graphQL-file-sender.git

Usage

Client

  1. handleDownload - handles the client download of the file. (Note: this function uses an exportable base64ToArray function to convert the base64 string to a Uint8Array custom made for this project)
import { handleDownload } from 'graphql-file-sender';
  • Something you will need to do is add this query to your client. this will query the server for the file.
const GET_FILE = gql`
  query GetFile {
    file {
      filename
      mimetype
      encoding
      content
    }
  }
`;
  • Then you can link the handleDownload function to a button or any other event you want. (Note: this function takes in the data from the query as a parameter)
  • This
const DownLoadFile: React.FC = () => {
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
  const { loading, error, data } = useQuery(GET_FILE);

  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error: {error.message}</p>;

  return (
    <div>
      <button onClick={() => handleDownload(data)}>Download</button>
    </div>
  );
};

Server

On the server end you have 1 method for Express but there is some setup you need to do first.

  1. Import the deconstructFile function from the package
import { deconstructFile } from 'graphql-file-sender';
// or
const { deconstructFile } = require('graphql-file-sender');
  1. Setup the Express server and GraphQL endpoint as you normally would. (Note: you will need to add the cors package to your server)

  2. Then you need to make an addition to your GraphQL schema. You need to add a File type to your schema. This will be used to return the file data to the client. (Note: the content field is the base64 encoded string of the file)

const FileSchema = `
  type File {
    filename: String!
    mimetype: String!
    encoding: String!
    content: String!
  }
`;

const schema = buildSchema(`
  ${FileSchema}
  
  type Query {
    hello: String
    test: String
    file: File
  }
`);
  1. When you create the root object for your GraphQL server, you need to add a resolver for the file query. This resolver will return the file data to the client. (Note: the deconstructFile function takes 2 parameters, 1 is the project root directory and the other is the a generatePdf function, the generate pdf parameter is optional and i recommend making your own function to generate a pdf file with code). Hell, even make a PR to add your own function to the package it would be greatly appreciated :)
const projectDirectory = __dirname;

const root = {
  file: () => {
    return deconstructFile(projectDirectory);
  },
};
  1. At the moment inside the source code ive made a template to generate a pdf file with code. Its very simple and I recommend making your own inside your own api. Have a read through the source code to see how it works.

Apollo Client Integration

This project also provides a seamless integration with Apollo Client and React, so set up apollo client as you normally would with your React application. All you have to do is add the plugin to your app and you are good to go. For example:

import { ApolloClient, InMemoryCache } from '@apollo/client';
import { createUploadLink } from 'apollo-upload-client';

const client = new ApolloClient({
  link: createUploadLink({
    uri: 'http://localhost:4000/graphql',
  }),
  cache: new InMemoryCache(),
});

Contributions are welcome!

If you find any bugs or have suggestions for improvement, please open an issue or submit a pull request.