-
My setupi'm building a graphql api with pothos and hono.js. my resolvers call internal rest apis that have swagger definitions. for example, my
The problemi converted the swagger schema to graphql schema (works fine). but now i don't know how to leverage this generated schema in pothos without duplicating all the type definitions. right now i'd have to do: // generated from swagger
type Page {
id: ID!
title: String!
content: String!
// ... 20 more fields
}
// then duplicate everything in pothos
builder.objectType('Page', {
fields: (t) => ({
id: t.exposeID('id'),
title: t.exposeString('title'),
content: t.exposeString('content'),
// ... same 20 fields again
}),
});this feels wrong - i'm maintaining the same types twice. What i'm trying to achieveit's like schema stitching but at the type definition level. i want to import the graphql schema and use it in pothos without manual duplication. ideally something like: import { pageSchema } from './generated/graphql-schemas';
builder.objectType('Page', {
schema: pageSchema, // reuse generated schema
fields: (t) => ({
// extend with custom fields
isPublished: t.boolean({
resolve: (page) => page.status === 'published',
}),
}),
});My questions
Why this mattersthe rest apis are managed by other teams and their swagger specs change. i want to avoid manually updating my graphql layer every time. any suggestions on how to handle this use case? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
I think this plugin probably does a large portion of what you want: https://pothos-graphql.dev/docs/plugins/add-graphql There are a lot of different ways to go about something like this with different tradeoffs. If you already have a way to build working graphql types, and just need to reference them in a few places in your Pothos schema this plugin should work great. There's lots of other options (schema stitching, federation, generating Pothos code directly, dynamically building schemas at runtime) that have different tradeoffs, so it's hard to say what's actually best without knowing a lot about your specific use case. Hopefully this plugin is what you need though as it's one of the easiest to use options. |
Beta Was this translation helpful? Give feedback.
Assuming you are just generating graphql SDL and don't need resolvers, you would probably need to parse the schema text into a GraphQLSchema object to use the plugin, but that should be relatively easy.
You'll probably also need to generate typescript types to add either to the schema types of object refs when using them with Pothos