Fetching data from an API in GraphQL

Поділитися
Вставка
  • Опубліковано 16 жов 2024
  • Learn how to fetch data from an API to fulfill GraphQL queries.
    Code: github.com/ben...
    Links from video:
    swapi.co/
    ----
    Video Suggestions:
    Trello: trello.com/b/m...
    Join the Trello board: trello.com/inv...
    ----
    Follow Me Online Here:
    GitHub: github.com/ben...
    LinkedIn: / benawad
    Instagram: / benawad97
    Patreon: / benawad
    ----
    Join the Discord: / discord
    Twitter: / benawad
    #benawad
    TikTok: / benawad ​

    ----
    Follow me online: voidpet.com/be...
    #benawad

КОМЕНТАРІ • 54

  • @yasinnkhann
    @yasinnkhann 2 роки тому +1

    This was the video I was looking for, thank you so much Ben

  • @camiloguerrero7313
    @camiloguerrero7313 6 років тому +3

    Keep these coming, your videos are always helpful

  • @flipl-online-arbitrage
    @flipl-online-arbitrage 4 роки тому +2

    Dude’s OG on the keyboard shortcuts

  • @tedspens
    @tedspens 5 років тому

    I'm just beginning with graphql and already learned a whole new way to use it. Thanks!

  • @leonpeter8855
    @leonpeter8855 4 роки тому

    You fucking LIFE SAVER!!! I've been looking everywhere for a video like this, thanks man!!!

  • @ehodges2004
    @ehodges2004 6 років тому +1

    Cool! I've been wondering about this but had not looked into it yet.

  • @PandemicGameplay
    @PandemicGameplay 6 років тому +1

    What is your opinion on fetch? It isn't supported by all browser versions is it? It looks like a lot simpler version of ajax.

    • @bawad
      @bawad  6 років тому +1

      I like how simple it is, but I'm indifferent about it

  • @rameshdoddi3665
    @rameshdoddi3665 2 роки тому

    I have a requirement to hit graphQL and fetch the data based on the request from SAP PI/PO midleware.. Any idea on how to create the structure in SAP PI

  • @dennistennis5622
    @dennistennis5622 6 років тому +1

    Nice video Ben! 😃👍👍

  • @ronifintech9434
    @ronifintech9434 6 років тому +1

    Hi Ben,
    Thanks for the video...
    What if you would like to connect the API fetching directly to your front-end... how can it be done?

    • @bawad
      @bawad  6 років тому

      Are you asking how to do a API request in React?

    • @ronifintech9434
      @ronifintech9434 6 років тому +1

      Ben Awad
      yes, React and Apollo Client on the front end...

    • @ronifintech9434
      @ronifintech9434 6 років тому +1

      Ben Awad or once we connect to the API with GraphQL, and connect the GraphQL end point to our front end through Apollo Client, we should be able to query the API?

    • @bawad
      @bawad  6 років тому +2

      yeah, so: react -> graphql -> api
      You don't need to do any: react -> api
      In react you will make only graphql requests.

  • @d2lmobileutil
    @d2lmobileutil 4 роки тому

    Hi Ben, Could you please tell me what is the URL for the Playground server [purple icon tab]? I'd like to use it too, but I have a hard time to find it. Thanks

  • @gregg2696
    @gregg2696 5 років тому

    Ben- Fantastic Video!!! Thank you!!! Do you have a video or can you recommend a place to learn how to cache the responses?

    • @bawad
      @bawad  5 років тому

      I have one video on caching with redis ua-cam.com/video/BM4_IvSjRYw/v-deo.html
      but it might be hard to follow if you haven't watched the series.
      I don't know of any good articles/tutorials on caching like this, but I'm planning on making some videos on it

  • @chrisdascoli6815
    @chrisdascoli6815 6 років тому +1

    nice video! Im curious about the resolver process here (in this instance specifically the second API call of films or homeworld however if this were a DB the same question applies), if you did a query of getPerson but didnt ask for films (or homeworld) in it does it still try to resolve films and/or home world? asked another way will it still do that API call (or hit the DB if this were a DB example) despite not requesting that information in the query? I'm wondering about performance, it would be great if it didnt unless you needed it so it would be one less API call or DB query.

    • @bawad
      @bawad  6 років тому +1

      It won't call the resolver unless the user requests that data.

    • @chrisdascoli6815
      @chrisdascoli6815 6 років тому +1

      awesome! thanks for the quick reply! love the videos man, you single-handedly taught me GraphQL in my free time so keep up the great work.

    • @bawad
      @bawad  6 років тому +1

      That's awesome!

  • @HassanKhan-od5xx
    @HassanKhan-od5xx 3 роки тому

    Hi there. I was having trouble doing this for my own project and coudn't seem to run my own code until eventually copying this code, trying to run it and receive the same error. Does the code from 3 years ago changed or can it still work??

  • @StartupSpells
    @StartupSpells 6 років тому +1

    Ben, how do you delete everything on a line after your cursor in VSCode. Any shortcut key ?

    • @bawad
      @bawad  6 років тому +1

      I'm using the vim plugin which allows all kinds of shortcuts. Deleting the rest of the line can be accomplished by typing `D` (without the backticks)

    • @rubinbajracharya9319
      @rubinbajracharya9319 6 років тому +2

      Yes there is one. To Delete all in left side use cmd+delete and to delete all in right side use cmd+fn+delete

    • @StartupSpells
      @StartupSpells 6 років тому +1

      Wow thanks

  • @renai777
    @renai777 3 роки тому

    any idea how to call an api in java spring boot?for graphql.

  • @rawknee04
    @rawknee04 6 років тому

    Thank you! I am looking to use a headless CMS with Gatsby, and there is no GraphQL api for it, so this was exactly what I was looking to do. Do you know how I would get this into Gatsby, or would I need to create a source plugin or something to make it work?

    • @bawad
      @bawad  6 років тому

      I'm not sure, I'm a noob at Gatsby

  • @Appelsinvanden
    @Appelsinvanden 6 років тому +2

    Hey Ben! Great video - I have a question for you about your Slack clone series, I have been trying to implement facebook auth with passport instead of email/password - I can see you already have an example made on your github, where you pass the token/refresh from the server to the client as parameters in the url - I was just wondering what the best approach is on the client/React site, to get the tokens from the url and store it in localstorage? :)

    • @bawad
      @bawad  6 років тому +2

      If you're using react router, one of the parameters to the route will be the tokens

    • @bawad
      @bawad  6 років тому +2

      You could set the token in componentDidMount

    • @Appelsinvanden
      @Appelsinvanden 6 років тому +1

      Cool thanks for your answer! Would you recommend creating a new component just for grabbing the token and storing it in localstorage and then redirect, or should i just do it in one of my existing components?

    • @bawad
      @bawad  6 років тому +1

      I usually create a separate component that redirects

  • @arthurserafim1400
    @arthurserafim1400 5 років тому +1

    Hey Ben! I decided to start a personal project using anilist.co graphql api, but the structure seems to be quite different, so i wonder if you could make a video on that one! Cheers from Brazil

    • @bawad
      @bawad  5 років тому

      How's the structure different?

    • @arthurserafim1400
      @arthurserafim1400 5 років тому

      @@bawad Its not separated into something/person/id, its all mixed up, i cant describe it :/

  • @amirhoseinfarhadi2268
    @amirhoseinfarhadi2268 3 роки тому

    Any update on GraphQL Tools ? Did you use new Playground ?

  • @gregg2696
    @gregg2696 5 років тому

    Two other questions:
    The resolveFilms function saves a lot of coding, but it is the exact same function as resolveSpecies, resolveStarships, etc. with the exception of the type. Is there a way to pass type and just use one function?
    How would you fetch swapi.co/api/people, which would return all people across multiple pages?

    • @bawad
      @bawad  5 років тому

      yeah you could create a higher order function:
      const createTypeResolver = type => parent => {
      const promises = parent[type].map(async url => {
      const response = await fetch(url);
      return response.json();
      });
      return Promise.all(promises);
      };
      const resolveFilms = createTypeResolver('films')
      /people you would return an array of people

    • @gregg2696
      @gregg2696 5 років тому

      The higher order function is great - will try that - thanks!!!
      My people query below works, but is very slow since it is awaiting each fetch before doing the next. I am trying to figure out how to use Promise.all for the fetches in the for loop, but this may be a question I have for you in your next office hours (love that Patreon option BTW)
      async people() {
      const perPage = 10
      let allResults = []
      // Get first page and total number of results
      let people = await fetch(`swapi.co/api/people`)
      let data = await people.json()
      const totalPages = Math.ceil(data.count / perPage)
      // Add results to array
      allResults = allResults.concat(data.results)
      // If the total results is greater than the results per page, get the rest of the results and add to the allResults array
      if (data.count > perPage) {
      let page = 2
      for (page; page

    • @bawad
      @bawad  5 років тому

      Oh I see the problem now. You could optimize it some by doing Promise.all, but some people are in the same films so you are requesting the same film multiple times which is inefficient. Dataloader would be a good choice to solve this. We can talk about that in office hours if you like.

  • @andresmontoya7852
    @andresmontoya7852 6 років тому

    It's okay to use always graphql?

    • @bawad
      @bawad  6 років тому

      I think for most cases it's good to us GraphQL

  • @DevKhalilKhalil
    @DevKhalilKhalil 4 роки тому

    thanks

  • @alakdam3970
    @alakdam3970 4 роки тому

    Mate,
    How I can fetch this data from this site: digitransit.fi/en/developers/apis/1-routing-api/ . I have been trying almost 5 days. Did not find any clue. Can you please help me out?

  • @atulrajotiya4469
    @atulrajotiya4469 5 років тому

    what if API have authorization bearer

    • @bawad
      @bawad  5 років тому

      you can add it in the header when you fetch

    • @kerrykimrusso
      @kerrykimrusso 5 років тому

      ​@@bawad where would you make the call to get the auth token and where would the logic of retrieving the token live? think i'd wanna pass the token down to each resolver through the context once it's received and not make the call to get it in every function of my class that extends REST Datasource.

    • @bawad
      @bawad  5 років тому

      yeah I would put it where you create the context

  • @berliandro
    @berliandro 3 роки тому

    swapi .co now switch domain to swapi .dev

  • @AbhishekBM
    @AbhishekBM 2 роки тому

    4:51