/supabase-tutorials

How to use Supabase with GraphQL?

Learn how to integrate Supabase with GraphQL step-by-step—from setting up your project and API keys to using Apollo Client for querying and mutating data securely.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to use Supabase with GraphQL?

 

Step 1: Set Up Supabase Project

 

  1. Begin by visiting Supabase's official website.
  2. Create a new account if you haven't already, or simply log in.
  3. After logging in, create a new project:
  • Select "New Project" on the dashboard.
  • Enter a suitable name for your project.
  • Choose the desired database region.
  • Set a secure password for your database.

 

Step 2: Configure API Keys and Environment

 

  1. Navigate to your project's settings.
  2. Go to the "API" section to find your project's URL and anon key.
  3. Keep these values handy, as they will be used for client-side access.

 

Step 3: Enable GraphQL in Supabase

 

  1. Supabase provides a built-in PostGraphile instance to work with GraphQL.
  2. Check your instance URL (usually in the form https://your-project-ref.supabase.co).
  3. Append /graphql/v1 to your project URL to access GraphQL.
  4. Use the Apollo Client or any other GraphQL client to connect using the URL:
  • https://your-project-ref.supabase.co/graphql/v1

 

Step 4: Set Up Supabase Client with GraphQL

 


import { createClient } from '@supabase/supabase-js'
import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client'

// Supabase setup
const supabaseUrl = 'https://your-project-ref.supabase.co'
const supabaseAnonKey = 'your-anon-key'
const supabase = createClient(supabaseUrl, supabaseAnonKey)

// Apollo Client setup
const apolloClient = new ApolloClient({
  link: new HttpLink({
    uri: `${supabaseUrl}/graphql/v1`,
    headers: {
      apikey: supabaseAnonKey,
      Authorization: `Bearer ${supabaseAnonKey}`
    }
  }),
  cache: new InMemoryCache()
})

 

Step 5: Querying with GraphQL

 

  1. Use Apollo Client to write a GraphQL query. For example, to fetch all rows from a table called posts:

apolloClient.query({
  query: gql\`
    query GetPosts {
      posts {
        id
        title
        content
      }
    }
  \`
}).then(response => {
  console.log(response.data)
}).catch(error => {
  console.error(error)
})

 

Step 6: Mutating Data with GraphQL

 

  1. To insert data into a table with a mutation query:

apolloClient.mutate({
  mutation: gql\`
    mutation AddPost($title: String!, $content: String!) {
      insert\_posts(objects: { title: $title, content: $content }) {
        returning {
          id
          title
          content
        }
      }
    }
  \`,
  variables: {
    title: "New Post",
    content: "This is the content of the new post."
  }
}).then(response => {
  console.log('New post added:', response.data)
}).catch(error => {
  console.error(error)
})

 

Step 7: Handling Authentication and Security

 

  1. Consider setting up authentication for secure data operations.
  2. Use Supabase's built-in authentication features like email/password, OAuth, etc.
  3. Update the Authorization header dynamically after user login.

 

Step 8: Test and Deploy

 

  1. Test your integration thoroughly with various queries and mutations.
  2. Deploy your application ensuring secure and efficient operations.

 

By following these steps, you will successfully integrate Supabase with GraphQL in your application. Adjust configurations and queries as per your specific application requirements.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022