question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GraphQL for a single pdf file

See original GitHub issue

I found documentation for querying all pdf files by using the allFile type, but I want to query a single pdf file. I haven’t been able to find any documentation on querying a single pdf file. This is what I have tried so far but I haven’t had any luck.

const data = useStaticQuery(graphql query { pdf: file(base: { eq: "resume.pdf" }) { publicURL } } )

“a<href={this.props.data.pdf}>a”

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
jonniebigodescommented, Jan 31, 2020

@brandontb12 i’ve picked up on your issue and based on the information you mentioned in the description i’m going to detail a possible solution for your case. This will extrapolated from the information available.

  • Created a new Gatsby website based on the hello world starter.
  • Installed gatsby-source-filesystem
  • Changed gatsby-config.js to the following:
module.exports = {
  /* Your site config here */
  plugins:[
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `data`,
        path: `${__dirname}/src/data/`,
      },
    },
  ]
}
  • Created the src\data folder and copied over a sample pdf file called a_random_example.pdf
  • In my src\pages\index.js i changed it to the following:
import React from "react"
import { useStaticQuery, graphql } from "gatsby"
export default () => {
  const myexamplePdf = useStaticQuery(graphql`
    {
      pdf: file(name: { eq: "a_random_example" }) {
        name
        extension
        publicURL
      }
    }
  `)
  return (
    <div >
      <h1>example with a single pdf</h1>
      <a href={myexamplePdf.pdf.publicURL}><p>{myexamplePdf.pdf.name} without download</p></a>
      <a href={myexamplePdf.pdf.publicURL} download><p>{myexamplePdf.pdf.name} with download</p></a>
    </div>
  )
}

I’m fetching a single file, this time the pdf file i’ve mentioned and adding two links, the first one will display the pdf content and the second one will allow the pdf download. A small caveat regarding the second one. With the gatsby-source-filesystem package, the filename will be changed into name-of-file-uuid.pdf. I’ve issued yarn develop waited for the build process to complete and opening up http://localhost:8000 i’m presented with the following:

brandontb12_file_1

As you can see in the left side is the “page” and on the right side the pdf being displayed.

Feel free to provide feedback so that we can close this issue, or continue to work on it until we find a suitable solution.

0reactions
jonniebigodescommented, Jan 31, 2020

@brandontb12 ok, no need to thank, glad to hear it, feel free to close this as it’s fixed if you don’t mind.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to return PDF file in an Graphql mutation? - Stack Overflow
It can't be done by mutation only. You can't return file(binary)/headers(mime)/etc (needed to be handled as download request behaviour by ...
Read more >
File uploads and downloads in GraphQL | by Boštjan Cigan
This is a feature that allows you to upload files via GraphQL mutations directly. Not all server implementations support this, one of them...
Read more >
GraphQL file uploads - evaluating the 5 most common ...
We're comparing the 5 most common approaches of uploading files with GraphQL APIs, using base64 encoding, Multipart uploads, a custom REST ...
Read more >
GraphQL i - Tutorialspoint
tutorial will introduce you to the fundamental concepts of GraphQL including: ... Query type is one of the many root-level types in GraphQL....
Read more >
Admin graphQL - Returning ID's for PDF files
Further investigation it actually seems to be falling over when it hits a PDF file. So even returning a MediaImage request for example, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found