We have this Contentful API. But you want to extend that with your own custom logic. Well, we've just seen how easy that is with a few lines of code. So here we have g.extend, we give the name of our new field, then we return the type of the field, and then the resolver weather, the name for the resolver which is weather, and then we just create a single file that we call weather. And that's all we need to do.
Now, if you wanted to do, maybe, so you wanted to fetch the location latitude and longitude. We can also do that as well. So for the sake of this, we could just fetch, we could do something like make a fetch request, and inside of here we could say a lat is equal to lat and the longitude is equal to the long value here. And this location is what comes from the property or from the query, so if we add that inside of here, we can access these values inside of here, and that's what that root or parent argument is for.
So, yeah, please do ask any questions, if none of this makes sense, or if it all makes sense, then great. Don't ask anything, or whatever, just let me know if there's anything I can help with. Now then, at this point, we can go on to add more data sources. Let's just add a few more.
At this point, we've stitched in remote APIs and we've designed our own GraphQL query with our own data, but what if we want to extend an API that is not a GraphQL API? Maybe we just want to embed a GraphQL API or a REST API and transform that into a GraphQL API and output GraphQL and do all of this kind of REST to GraphQL stuff. Well, because something like the GraphQL open API spec exists, that's a specification that Graphbase can read and then our system, our engine, can take all of that data and it can transform that into GraphQL.
So if I bring this example across and walk through that very briefly, here we have Stripe, very famous API, very cool. Very cool API. But they don't have GraphQL. What they do have is an open API spec. We're able to take that specification and pipe that into the open API connector, configure all the headers that we need, and then any transforms. And with transforms, what's cool about that is we can say, okay, I want you to exclude certain fields. So maybe we only want to fetch the user's field or the customer's field or the orders query. We can extend all of that as well and exclude that, sorry.
So with that, if we save that, I will need to off screen again, just add my environment variable to that all important .env file. I think we're good. Yeah, so I've added that. So now this stripe API key is my secret stripe API key. Now with this running, the changes have been detected and you can see here that we detected that stripe API key and we've set that inside of .env. We're now able to use that. So if we go back to Google Chrome, we should now see that we have a query for stripe. And all of these queries here were automatically generated for us based off the OpenAPI spec. That's why I love specifications that we're able to read one and you know, use because we know what the field is called and the type of the field and the URL and we have all the properties of each individual field where we're able to do what we need to do to transform all of that, all predictable.
So here, I'm going to make a query to fetch my customers from Stripe so we'll grab the name and maybe so grab the email. We perform that request. We then get the data that comes back from this. Just need to admit a few more. There we go. So yeah, we have a query that's going to Stripe and it's fetching back all of my customers.
Now, the concept that we had before with extending Contentful with a custom field. Let's say we wanted to do the same for Stripe. Now, let's say we want to get the avatar or the gravatar for each of our Stripe customers. Now, Stripe doesn't have a field called gravatar, but because we're using graph base, we're able to create a resolver for that. So just to save us a bit of time, I'm going to copy and paste the code to make that happen and we'll walk through it. So below that Stripe data source, we'll do exactly the same as what we did before. We'll call g.extend and this time we'll pass the type name StripeCustomer, then we'll call the field gravatar and then for that, we'll specify that there are some arguments to this field. We can specify the size, the default image, if an image doesn't exist, of course, a rating and the Boolean to only fetch secure images. And then we return a URL that is optional or nullable and then we use the file gravatar. And this up here, where is it? enum ref, the rating. We've just created a GraphQL enum here. So I'm going to save this.
Comments