Building a Minimum Viable Stack with RedwoodJS and FaunaDB

So, here’s what I do:

Client

This would be your FaunaDB gql client.

// /api/src/lib/hasuraClient.js

import { GraphQLClient } from 'graphql-request'

export const request = async (
  query = {},
  domain = process.env.HASURA_DOMAIN
) => {
  const endpoint = `https://${domain}/v1/graphql`

  const graphQLClient = new GraphQLClient(endpoint, {
    headers: {
      'x-hasura-admin-secret': process.env.HASURA_KEY,
    },
  })

  try {
    return await graphQLClient.request(query)
  } catch (error) {
    console.log(error)
    return error
  }
}

Service

// src/services/stories/stories.js
import { requireAuth } from 'src/lib/auth.js'
import { request } from 'src/lib/hasuraClient'

export const stories = async () => {
  requireAuth()

  const query = `
  {
    stories {
      author
      createdAt
      imageUrl
      site
      summary
      title
      url
    }
  }
 `
  const data = await request(query, process.env.HASURA_DOMAIN)

  return data['stories']
}

SDL

import gql from 'graphql-tag'

export const schema = gql`
  type Story {
    author: String
    createdAt: String
    imageUrl: String
    site: String
    summary: String
    title: String
    url: String
  }

  type Query {
    stories: [Story!]
  }
`

And then just query stories as you would normally from any Cell on web using gql calling your RW api.

1 Like