Nhost example app in React Native using Hasura.
- Create a project on Nhost.
- Clone this repo.
- Copy
config-example.yaml
toconfig.yaml
inhasura/
. - Update
config.yaml
withendpoint
andadmin_secret
inhasura/
. - Apply migrations and metadata:
cd hasura; hasura migrate apply; hasura metadata apply;
. - Copy
config-example.ts
toconfig.ts
insrc/
. - Update
config.ts
with the details from you project at Nhost. yarn install
.yarn start
.
npx react-native init <Project Name>
Creating a file src/helpers/api.js
export const GRAPHQL_ENDPOINT = 'https://hasura-[id].nhost.app/v1/graphql';
export const BACKEND_ENDPOINT = 'https://backend-[id].nhost.app';
export const X_HASURA_ADMIN_SECRET = '<Your_Secret>';
npm install --save nhost-js-sdk
Creating a file src/helpers/nhostSdk/index.js
import nhost from 'nhost-js-sdk';
import { BACKEND_ENDPOINT } from '../api';
const config = {
endpoint: BACKEND_ENDPOINT,
};
nhost.initializeApp(config);
const auth = nhost.auth();
const storage = nhost.storage();
export { auth, storage };
Checkout the full usage of Auth and Storage here.
iOS | Android |
---|---|
Look at the file in src/helepers/getRequestObject.js
import { GRAPHQL_ENDPOINT, X_HASURA_ADMIN_SECRET } from "./api";
export const getRequestObject = ({ data, token }) => ({
method: 'POST',
url: GRAPHQL_ENDPOINT,
data,
headers: {
Authorization: `Bearer="${token}"`,
'x-hasura-admin-secret': X_HASURA_ADMIN_SECRET
},
})
// A Sample GraphQL Query
export const getSkills = () => {
return {
query: `query getSkills {
tags {
tag
}
}`
}
}
// A function which returns a response to the axios request, wrapping the graphQL Function
const fetchSkills = async (_, token) => {
const response = await axios(
getRequestObject({
data: getSkills(),
token
})
);
return response;
}
Every query
or mutation
can be composed as shown above to work with Hasura GraphQL.