Add 3rd party GraphQL API

Dear community,

I’m quite new to RWJS and I’ve been learning GraphQL through RWJS, so my question might be super simple/obvious. I tried googling and searching the forums but didn’t find anything (I probably don’t know the right terms to search for). Please let me know if this question has already been answered.

How do I add another GraphQL endpoint?

I’m using the standard RWJS GraphQL setup, I use GraphQL to CRUD my database and use Auth0 for authentication. Now I want to communicate with a third-party API that does provide a GraphQL endpoint. However, I have no idea where to start?
Any help & tips would be appreciated!

Also, I’m building a dashboard that connects a few SaaS applications, just to make my life easier without having to log in to 4 different sites. I was wondering if there’s an opinion about how one would store & refresh authentication tokens from multiple providers simultaneously?

Thank you for your help!

EDIT: here they do it using the client context, i.e. pass an extra parameter to the query/mutation. Is that something you would recommend?

1 Like

I am very interested in any response to this. I will link to this GitHub Issue discussion about schema stitching. This is along those lines: Support GraphQL Schema stitching · Issue #3640 · redwoodjs/redwood · GitHub.

Also, there is a post in RW docs to add third party API’s: Using a Third Party API | RedwoodJS Docs. This may help you out somewhat.

Hi!

Thank you for linking the issue, I didn’t come across it yet. I’ll read through it! Although on a quick glance it doesn’t look like it’s supported yet?

I found (and used) the third-party API docs, however, that case is specifically for ‘normal’ RESTful APIs rather than GraphQL APIs. I guess I could wrap every single endpoint in its own service in RWJS but that seems a bit much.

Since schema stitching isn’t supported just yet you could just use your services and make requests to whatever graphql API you want like so. graphql-request is already included in redwood.

// util.js
import { request, gql } from 'graphql-request'

const authHeaders = {
  token: "<your_api_token_secret>",
}

const thirdPartyRequests = async (query) => {
    return await request({
      url: 'https://api.thewebsite.com/graphql',
      document: query,
      requestHeaders: authHeaders,
    })
}


// service.js
const eventsQuery = gql`
{
  events {
    id
    name
  }
}
`

export const getThirdPartyData = () => {
  return thirdPartyRequests(eventsQuery)
}

Then just add the getThirPartyData in your sdl and its a part of your graphql api.

Hi. This is exactly what I’m looking for in my current app. Any chance to get an example of this? Whatever I’m doing just doesn’t seem to work and just returns null. Thanks!

Here’s an example using Fauna:

Here’s another example using StepZen: