Using Stainless SDKs with GraphQL APIs
Last updated: October 10, 2024
While all Stainless SDKs are based on REST—the most common and popular API architectural style—you might be wondering how to integrate them with your GraphQL APIs. Although GraphQL isn't inherently compatible with REST, you can bridge the gap using a middle-man layer. This article will guide you through the recommended solution: sofa-api.
What is sofa-api?
Sofa-api is an open-source tool that converts your GraphQL APIs into RESTful endpoints and generates an OpenAPI (Swagger) specification file from your GraphQL schema. This allows you to leverage REST-based tools and SDKs, like those provided by Stainless, without restructuring your existing GraphQL backend.
How to Set Up sofa-api with Your GraphQL API
Follow these steps to integrate sofa-api with your GraphQL API and generate an OpenAPI spec file.
1. Install sofa-api
npm install sofa-api2. Configure sofa-api
In your server setup file, import sofa-api and configure it with your GraphQL schema.
import { useSofa } from 'sofa-api';
import express from 'express';
const app = express();
app.use(
'/api',
useSofa({
basePath: '/api',
schema,
})
);
// GET /api/users
// GET /api/messagesFind more examples at https://github.com/Urigo/sofa
3. Generate the OpenAPI Specification File
After setting up the routes, sofa can generate an OpenAPI spec file for you.
4. Use the OpenAPI Spec with Stainless
Now that you have the OpenAPI spec file, you can use it as input for the Stainless code generator. Go to the SDK Studio, create a new project and upload your spec file to get started.