Registry / api / sofa-api

sofa-api

JSON →
library0.18.10jsnpmunverified

Convert a GraphQL schema into REST API endpoints automatically. Current stable version is 0.18.10, released as a mature library under the GraphQL Hive organization. It generates RESTful routes from GraphQL queries, mutations, and subscriptions, supporting nested models with ID-based linking and customization of HTTP methods, paths, and status codes. Differentiators: no framework dependency, extensible with custom context and ignored fields, and built-in model resource pattern for nested data.

npm install sofa-api
INSTALL
IMPORT
SIG · SOFA-API
S
sofa-api
apijavascriptv0.18.10
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

useSofa
import { useSofa } from 'sofa-api'
import useSofa from 'sofa-api'
useSofa is a named export, not a default export. TypeScript included.
useSofa (default)
const { useSofa } = require('sofa-api')
const useSofa = require('sofa-api')
CommonJS requires destructuring because it's a named export.
SofaConfig type
import type { SofaConfig } from 'sofa-api'
Type is exported for TypeScript users to configure options.

Creates an Express app with Sofa middleware that exposes GraphQL queries as REST endpoints.

import express from 'express'; import { useSofa } from 'sofa-api'; import { buildSchema } from 'graphql'; const schema = buildSchema(` type Chat { id: ID text: String } type Query { chat(id: ID): Chat chats: [Chat] } `); const app = express(); app.use( '/api', useSofa({ basePath: '/api', schema, async context({ req }) { return { req }; }, }) ); app.listen(4000, () => console.log('Sofa API running on http://localhost:4000/api'));
Debug
Known issues
breakingGraphQL v15/v16 - peer dependency may cause peer conflict if project uses GraphQL.js v14 or earlier.
fix
Upgrade GraphQL.js to v15 or v16.
affects: >=0.18.0
deprecatedSofa used to export a default function; current versions require named import { useSofa }.
fix
Change import to named import: import { useSofa } from 'sofa-api'.
affects: >=0.16.0
gotchaModel types require an 'id' field and two queries (list and single) to generate REST resources. Missing these will cause parts of the schema to not be exposed as REST endpoints.
fix
Ensure models have 'id' field and both a query returning a list (no required args) and a query accepting 'id: ID'.
affects: >=0.18.0
gotchaUsing 'ignore' option with type.field pattern (e.g., 'Message.author') requires proper case sensitivity. Incorrect casing will be silently ignored.
fix
Verify the exact GraphQL type and field name in the schema.
affects: >=0.18.0
Errors
Common errors & fixes
Cannot find module 'sofa-api'
Package not installed or not in node_modules.
fix
Run 'npm install sofa-api graphql' or 'yarn add sofa-api graphql'.
TypeError: useSofa is not a function
Incorrect import: using default import instead of named import.
fix
Change import to: import { useSofa } from 'sofa-api'.
GraphQLError: Model "Message" requires an id field of type ID!
A type used as a model lacks an 'id' field of type ID in the GraphQL schema.
fix
Add an 'id' field of type ID to the type, e.g., 'id: ID'.
Upgrade
Version history
0.18.10latest on npm
Audit
Dependencies
graphqlrequiredpeer dependency required to process GraphQL schemas
Agent activity
32 hits · last 30 days
node
24
OpenAI (training)
1
Resources
sofa-api — npm install sofa-api · libregistry