FYI. @shansmith01 and I have exchange some examples to get SOFA running here: Explore REST API from GraphQL Schema using Sofa · Issue #5480 · redwoodjs/redwood · GitHub
Hi @shansmith01 - I finally got around to testing today and here’s how I configured my GraphQL handler:
Be sure to install the plugin:
yarn workspace api add @graphql-yoga/plugin-sofa
import { useSofa } from '@graphql-yoga/plugin-sofa'
import { createGraphQLHandler } from '@redwoodjs/graphql-server'
import directives from 'src/directives/**/*.{js,ts}'
import sdls from 'src/graphql/**/*.sdl.{js,ts}'
import services from 'src/services/**/*.{js,ts}'
import { db } from 'src/lib/db'
import { logger } from 'src/lib/logger'
export const handler = createGraphQLHandler({
loggerConfig: { logger, options: {} },
directives,
sdls,
services,
extraPlugins: [
useSofa({
basePath: '/graphql',
swaggerUI: {
endpoint: '/swagger',
},
}),
],
onException: () => {
// Disconnect from your database with an unhandled exception.
db.$disconnect()
},
})
Then, visit http://localhost:8911/graphql/swagger
Note: I scaffolded a Country
model with 4 seeded countries.
And curl:
~ % curl -X 'GET' \
'http://localhost:8911/graphql/countries' \
-H 'accept: application/json'
[{"id":1,"name":"Sweden","code":"SE"},{"id":2,"name":"Finland","code":"FI"},{"id":3,"name":"USA","code":"US"},{"id":4,"name":"Canada","code":"CA"}]%
~ %
Note: I think there are some other ways to configure to get an endpoint you may prefer.
For example, you can leave your graphql.ts
function as is, and the duplicate it in functions as rest.ts
, and then:
import { useSofa } from '@graphql-yoga/plugin-sofa'
import { createGraphQLHandler } from '@redwoodjs/graphql-server'
import directives from 'src/directives/**/*.{js,ts}'
import sdls from 'src/graphql/**/*.sdl.{js,ts}'
import services from 'src/services/**/*.{js,ts}'
import { db } from 'src/lib/db'
import { logger } from 'src/lib/logger'
export const handler = createGraphQLHandler({
loggerConfig: { logger, options: {} },
directives,
sdls,
services,
graphiQLEndpoint: 'rest',
extraPlugins: [
useSofa({
basePath: '/rest/api',
swaggerUI: {
endpoint: '/swagger',
},
}),
],
onException: () => {
// Disconnect from your database with an unhandled exception.
db.$disconnect()
},
})
```a
Then you can curl at:
```bash
~ % curl -X 'GET' \
'http://localhost:8911/rest/api/countries' \
-H 'accept: application/json'
[{"id":1,"name":"Sweden","code":"SE"},{"id":2,"name":"Finland","code":"FI"},{"id":3,"name":"USA","code":"US"},{"id":4,"name":"Canada","code":"CA"}]%
~ %
If you want status codes for auth, we haven’t set those in RW by default for Authentication errors etc, but, in your service you can throw
throw new RedwoodGraphQLError('This is a custom auth message', {
http: {
status: 401,
},
})
Might just need some added extension info in redwood/packages/graphql-server/src/errors.ts at e798075ca6e81655f8ae7869664004ccf94633d2 · redwoodjs/redwood · GitHub but that might break the ApolloClient error handling.
If perhaps you have dedicated services for REST endpoints, probably can just throw errors you need for 401 or 4xx as needed.
Hope this helps!