In it, Stefan from our DevRel team will walk you through how to get started with GraphQL, React and Contentful covering GraphQL tooling, fragments, directives, variables, query complexity costs (and much more). If you're interested in learning more, check out our GraphQL course. Hopefully these examples can inspire you to dive deeper into this new way of querying data. The response is more predictable, and we’re not bogged down with additional information that we might not need. To make the calls that we used in our query example, we would have to make multiple requests to the Contentful Rest API. By taking advantage of GraphQL, we’re able to get only the data that we requested in a single query. These examples show how consuming GraphQL isn’t harder than a traditional restful API, but its benefits are far greater than Rest’s. If we really wanted to, we could simplify these requests even further by dropping the requests library from the Python or JavaScript examples, choosing to just use the built-in url processing capabilities of the languages. We’ve made a GraphQL request over HTTP in five different ways (cURL, Python, Javascript, Ruby and PHP). Go ahead and run this with PHP request.php to get our final JSON blob. If you get a response without any errors, log the output. To wrap things up, make the POST Request. Like the previous example, append your space ID onto the Contentful GraphQL endpoint, and set up the headers and request body. Similar to Ruby, we don’t need to install any libraries for PHP. We can run this file with ruby requests.rb. Create a new requests.rb file with the following code. Everything we'll use in the following example is already built into Ruby. Making HTTP requests with Ruby is something that we can do without installing libraries. We can run this example with node request.js to get our JSON blob. Create a new request.js file with the following code. Since the JavaScript version of requests is no longer supported, we'll take advantage of got. Similarly to the Python instructions, we'll be making use of an HTTP library for JavaScript. Run the code with python request.py and you'll see a JSON blob! 3. At the end, if we get a 200 HTTP status code, use the JSON library to format the output. I'm a fan of f-strings - the new way to do string manipulation in Python - so I'm using that format. But, since this is an example, we can save it in the file.Ĭreate your endpoint URL and Authorization headers. In your newly created Python file, import the libraries. Since this is a new Python project, spin up a new virtual environment, install requests and create a new Python file. It's extremely simple although, unlike urllib, we will have to install it. There are few different ways to make an HTTP request in Python, which gives us options in how we'd make a GraphQL query. When that command is run in a terminal it will output a JSON blob matching that of our previous example! Every one of our examples following this will use a similar format to the cURL request we made. If you’re using your own query, space id and accessToken, go ahead and replace them. We have a query, so we can modify the cURL example provided in the Contentful developer docs.Įven though the Contentful GQL API supports both POST and GET, we'll be using POST exclusively in this blog post, because POST enables us to send the query as a part of the JSON payload. Since cURL is a command-line utility, we can dive into all of our arguments before moving onto programming languages. I'll be using the following query, space id and access token in this example, but feel free to substitute your own if you'd like to try it yourself. If the query doesn't have any errors a JSON blob containing the data from your space will show up on the right. GraphQL is self-documenting, meaning that we can use both the documentation explorer and GraphiQL's built-in autocompletion (brought up via ctrl-space) to create a query. Check out this blog post if you'd like to learn more, but the important part is it's already got some content models and content in the space.įrom GraphiQL we can start creating a query. Open up, replacing the spaceID and accessToken with your own.įor this example, I'm using the space from my Serverless SuperHero website. To find a query we can use GraphiQL, an in-browser GraphQL IDE. For the API endpoint, we can use Contentful's API Base URL, taken from the GraphQL docs. Getting both of those items via Contentful is a breeze. Before we can get started, we need a query and an API endpoint to hit.
0 Comments
Leave a Reply. |