887 Free APIs For Developers
Looking for a free API to use in a project? We've got you covered. This is a list of free APIs available to the public.
In this tutorial I’ll show you how to use 8base to quickly create a GraphQL API and connect to it from a React app. 8base is a Developer Acceleration Platform offering a plethora of features to help front-end developers build applications faster and simpler than ever before. With 8base, there is no need to rely on back-end developers or DevOps!
Using the 8base platform, you can build your backend using a simple GUI that allows you to perform actions such as:
I believe that the best way to illustrate the utility of 8base is to show interested users how it can be integrated with projects they are already working on. By connecting a simple Todo MVC app to an 8base backend, we’ll learn how to:
This tutorial is aimed mostly toward users who are new to 8base and don’t have much prior experience with GraphQL. If you are familiar with GraphQL, connecting to your 8base backend should be a somewhat familiar process. Otherwise, everyone is encouraged to follow this tutorial to see 8base’s recommended implementation using the 8base SDK.
The developers at 8base created this platform with the intention of making something that they would want to use. As you navigate the 8base UI, you’ll notice that plenty of time and care were put into creating this platform to assure that the development experience would be pleasantly intuitive and that elements within the application are fluid and responsive in a modern way. In this tutorial, we will only use a few features offered by 8base, but I highly encourage you to explore the entirety of the platform and utilize other tools in the suite.
Now that you’ve prepared your backend, let’s head over to the 8base TodoMVC repo to prepare our client application.
The master branch contains the completed code for this project so you will be unable to follow along with the tutorial if you don’t complete this step.
{% code-block language="js" %}
yarn add @8base/app-provider graphql graphql-tag react-apollo && yarn
{% code-block-end %}
{% code-block language="js" %}
module.exports = {<br/>
client: {
service: {
name: '8base',
url: '8BASE_API_URL',
},
includes: [
"src/*.{ts,tsx,js,jsx}"
]
},
};
{% code-block-end %}
The Apollo GraphQL extension for VS code provides validation and autocomplete for writing GraphQL queries.
The application repo that we set up has the following code built in, so you can focus on understanding what’s going on. In instances where you have to add code, just uncomment what is specified. In instances where the code has to be removed, the specified code is appended by the comment "//Remove this" in the code base.
Note that all relevant code in this tutorial will be placed in the file src/App.js — this is not best practice using React but has been done this way to simplify the tutorial.
{% code-block language="js" %}
import gql from "graphql-tag";
import { graphql } from "react-apollo";
import { EightBaseAppProvider } from '@8base/app-provider';
{% code-block-end %}
You’ll see here not only are we importing gql and graphql, but we are also importing EightBaseAppProvider from the 8base SDK. The 8base SDK makes integration with 8base and GraphQL simple. We've done this by taking a lot of the boilerplate/setup-code and packaging it inside the SDK, so all the developer needs to do is wrap their application in the {% code-line %}<EightBaseAppProvider>{% code-line-end %} tag and pass it the appropriate props to allow data access to all the child components. EightBaseAppProvider uses Apollo Client, so if you are familiar with it already, there is nothing new to learn.
2. Initialize EightBaseAppProvider
{% code-block language="js" %}
<EightBaseAppProvider uri={ENDPOINT_URL} >
{({ loading }) => loading ? <div>"Loading..."</div> : (
<div className="todoapp">...</div> })}
</EightBaseAppProvider>
{% code-block-end %}
You need to provide a function as a child of EightBaseAppProvider that will tell React what we want to render. We can use the loading property to display a loader while the application is being initialized. During the initialization, EightBaseAppProvider loads the 8base table schema which gives you access to all properties of the data model inside your front end code. We'll touch on this feature more in-depth in a future tutorial.
3. Add a Query to Fetch todos
{% code-block language="js" %}
const TODO_LIST_QUERY = gql`
query TodoList {
todosList(orderBy: [completed_ASC, createdAt_DESC]) {
items {
id
text
completed
}
}
}
`;
const withTodos = graphql(TODO_LIST_QUERY, {
props: ({ data: { todosList: ({ items } = {}) } }) => {
return {
todos: items || []
};
},
});
{% code-block-end %}
In the code above, we are creating a Higher-Order Component using the graphql function provided by react-apollo.
From react-apollo — The graphql() function is the most important thing exported by react-apollo. With this function you can create higher-order components that can execute queries and update reactively based on the data in your Apollo store. The graphql() function returns a function which will “enhance” any component with reactive GraphQL capabilities. This follows the React higher-order component pattern which is also used by react-redux’s connect function.
Essentially, by using graphql we are able to write the code to query our backend in one place and inject this functionality into multiple components rather than writing it everywhere we want to be able to use it. The graphql function takes a query as its first parameter, config as the second, and the return value is a HOC that should be executed with the desired component as an argument. Note that in our example, we use the config parameter in graphql to specify that our data will be accessible as {%code-line%}props.todos{%code-line-end%} rather than {%code-line%}props.data.todosList.items{%code-line-end%}. As for our TODO_LIST_QUERY, we've already given you the appropriate syntax, but typically we might design our queries in the 8base API Explorer. If you navigate to the API Explorer and copy/paste the TODO_LIST_QUERY, you can see what data will be returned. Experiment with this query and see other ways that data can be returned.
{% code-block language="js" %}
Main = compose(
withRouter,
withTodos /* Add this */
)(Main);
{% code-block-end %}
{% code-block language="js" %}
Footer = compose(
withRouter,
withTodos /* Add this */
)(Footer);
{% code-block-end %}
Ultimately in our example, we want to display all the data from our 8base backend in the Main component and the Footer component. In the code above we are using the compose function provided by the lodash library to chain multiple Higher-Order Components together, giving our target components the functionality from each HOC. You can read more about how the compose function works here.
{% code-block language="js" %}
<Main
todos={ this.state.todos } /* Remove this */
<Footer
todos={ this.state.todos } /* Remove this */
{% code-block-end %}
When we first set up our application, all Todos were held in state because we were not connected to a backend. Our components still need to access the todos props, but we no longer have to pass it down explicitly because they are provided by withTodos.
4. Add a Query to Create a todo
{% code-block language="js" %}
const CREATE_TODO_MUTATION = gql`
mutation TodoCreate($data: TodoCreateInput!) {
todoCreate(data: $data) {
id
text
completed
}
}
`;
const withCreateTodo = graphql(CREATE_TODO_MUTATION, {
props: ({ mutate }) => ({
createTodo: ({ text }) => {
mutate({
variables: { data: { text, completed: false } },
refetchQueries: [{ query: TODO_LIST_QUERY }]
});
}
})
});
{% code-block-end %}
Here we are repeating the process we took previously to query our 8base backend, only this time we are using GraphQL mutation to create a Todo and then calling refetchQueries to update the state of our application with our newly added data. This will allow new data to populate throughout the application without having to make a separate request to our backend.
{% code-block language="js" %}
Header = withCreateTodo(Header);
{% code-block-end %}
The syntax above illustrates the enhancement of the Header component using our HOC. Note that withCreateTodo acts as a function, the WORDS_HIGHLIGHT:1:Header component is passed to the function as an argument and the enhanced component is then set to the variable Header to be used in our application.
{% code-block language="js" %}
<Header
createTodo={ this.createTodo } /* Remove this */
{% code-block-end %}
5. Add a Query to update todos
{% code-block language="js" %}
const TOGGLE_TODO_MUTATION = gql`
mutation TodoToggle($id: ID!, $completed: Boolean!) {
todoUpdate(filter: { id: $id }, data: {
completed: $completed
}) {
id
text
completed
}
}
`;
const withToggleTodo = graphql(TOGGLE_TODO_MUTATION, {
props: ({ mutate }) => ({
toggleTodo: ({ id, completed }) => {
mutate({
variables: { id, completed },
refetchQueries: [{ query: TODO_LIST_QUERY }]
});
}
})
});
{% code-block-end %}
{% code-block language="js" %}
Main = compose(
withRouter,
withTodos,
withToggleTodo /* Add this */
)(Main);
{% code-block-end %}
{% code-block language="js" %}
<Main
toggleTodo={ this.toggleTodo } /* Remove this */
...
{% code-block-end %}
Above we’ve repeated the previous pattern to add functionality to the Main component. This will give the user the ability to toggle Todos as complete or incomplete.
6. Add a Query to mark all todos as complete
{% code-block language="js" %}
const withToggleAllTodos = graphql(TOGGLE_TODO_MUTATION, {
props: ({ mutate, ownProps: { todos }}) => ({
toggleAllTodos: ({ completed }) => {
todos.forEach((todo) => {
mutate({
variables: { id: todo.id, completed },
refetchQueries: [{ query: TODO_LIST_QUERY }]
});
});
}
})
});
{% code-block-end %}
{% code-block language="js" %}
Main = compose(
withRouter,
withTodos,
withToggleTodo,
withToggleAllTodos /* Add this */
)(Main);
{% code-block-end %}
{% code-block language="js" %}
<Main
toggleAllTodos={ this.toggleAllTodos } /* Remove this */
...
{% code-block-end %}
7. Add a Query to delete todos
{% code-block language="js" %}
<Main
const DELETE_TODO_MUTATION = gql`
mutation TodoDelete($id: ID!) {
todoDelete(filter: { id: $id }) {
success
}
}
`;
const withRemoveTodo = graphql(DELETE_TODO_MUTATION, {
props: ({ mutate }) => ({
removeTodo: ( id ) => {
mutate({
variables: { id },
refetchQueries: [{ query: TODO_LIST_QUERY }]
});
}
})
});
{% code-block-end %}
{% code-block language="js" %}
Main = compose(
withRouter,
withTodos,
withToggleTodo,
withRemoveTodo /* Add this */
)(Main);
{% code-block-end %}
{% code-block language="js" %}
<Main
removeTodo={ this.removeTodo } /* Remove this */
...
{% code-block-end %}
As you can see, once you understand the basic pattern of enhancing components using the graphql HOC, it becomes fairly simple to write new queries that can be easily accessed throughout your application.
Finally, test that your application works properly by navigating to the root directory and running {% code-line %}yarn start{% code-line-end %} .
You should now be able to create, update and destroy Todos that are persisted to the database.
8base is currently working on implementations using other frameworks and libraries. We currently have examples using React and React-Native as well as more simple examples using CURL, Node, and Vanilla JS. Feel free to visit our Docs, send a message on our website, or reach out to me personally with feedback on your experience with 8base.
Completed example with 8base backend connected can be found in the master branch.
We're excited about helping you achieve amazing results.