flamelink / gatsby-source-flamelink

Gatsby source plugin for Flamelink
MIT License
8 stars 9 forks source link

Feat/local image (WIP) #7

Closed d4rekanguok closed 5 years ago

d4rekanguok commented 5 years ago

Close #5

Hi @jperasmus, sorry for hogging this issue for so long! Shortly after our first discussion my deadline & priorities were all changed, so I ended up creating an ad-hoc solution on userland & rolled with it.

Recently @PatrickResponsive contacted me & show interested in working on this issue, so I quickly (& shamelessly) copied my dirty code over & create this PR to have something to discuss. I tried to at least finish the image locator function, so this PR is not so half-baked. I think my solution is not so great though, would love feedback from you guys.

Here's a rundown:

Then, users with a media field in their schema can query their images like so:

image
query Blog {
  flamelinkBlogContent {
    hero {
      localFile {
        name
        childImageSharp {
          id
        }
      }
    }
  }
}

and get this data:

{
  "data": {
    "flamelinkBlogContent": {
      "hero": [
        {
          "localFile": {
            "name": "flamelink%2Fmedia%2F1555643636571_01",
            "childImageSharp": {
              "id": "b6e23db4-b292-50ee-bd8f-8173ba6b4ff5"
            }
          }
        }
      ]
    }
  }
}

Things that I think should be worked on:

I set up a Flamelink project to try it out. Perhaps I can share the test site & credential for it in Slack?

I also set up Jest to quickly test the image locator function, but I'm by no mean married to it, we can remove it / set up a different test method / whatever.

Sorry again about the delay!

d4rekanguok commented 5 years ago

@PatrickResponsive also I've just invited you to my fork so you can push stuff here if it makes sense to you!

PatrickResponsive commented 5 years ago

Awesome work thanks @d4rekanguok , Im gonna clone this and try get it working locally, Yeah, Ill try tackle refactoring this recursion function 👍

jperasmus commented 5 years ago

What we could do is use the schemas data and find all fields for each specific schema where the type is media and only use those.

d4rekanguok commented 5 years ago

@jp @PatrickResponsive Extending on JP's comment earlier, I tried to include images in a bunch of different places in a flamelink schema & got these 3 so far:

We can check on these 3 types of fields in any schema to see if they includes images & go from there. (I've added a schema data in the test folder in prepare for this.) I think we might run into image duplication problem though. For example, if I already have an image field in tag schema, then have a relational field in blog schema that points to tag, it may download the image in tag again.

jperasmus commented 5 years ago

Hi @d4rekanguok & @PatrickResponsive

Apologies for my radio silence, been a bit busy on other work. Derek, I have not probably said thanks for this PR, so here goes: "thanks 👍". I love that you took this on in your spare time and made this plugin better.

Regarding the conflicting image ID's, what about prepending the schema's key and the specific field's key in front of the image ID, and do this like a breadcrumb as deep into the nested relationships as possible?

d4rekanguok commented 5 years ago

@jperasmus I'm happy to contribute!

The image duplication I mentioned earlier turns out to be a non-issue. Since the flamelink ID for each image is already unique, gatsby already knows enough to store only 1 file per image.

What I was worrying about was that gatsby downloads the same image multiple times on first run, since the download operations happens asynchronously:

Screen Shot 2019-05-28 at 4 50 10 PM

But it looks like createRemoteFileNode has already taken care of this. As you can see in the screenshot above, even though cat.png is listed 3 times with 5 other images to the total of 8 images, only 6 images is actually downloaded.

jperasmus commented 5 years ago

ah awesome, that is very interesting. Thanks for the info. I am happy for this to be merged then. We can address any other issues as they come up.

jperasmus commented 5 years ago

This is now available in v1.2.0. Well done @d4rekanguok

d4rekanguok commented 5 years ago

awesome, thank you @jperasmus!