Can the graphql-yoga build a subgraph? #803
Answered
by
saihaj
hazem-alabiad
asked this question in
Q&A
|
Can |
Answered by
saihaj
Jan 13, 2022
Replies: 2 comments 2 replies
|
Any spec compliant graphql schema can be used. I haven't tried it but most likely will work. Let us know if it doesn't then we can explore more. |
2 replies
Answer selected by
Urigo
|
Yes, graphql-yoga supports Federation subgraph mode — here's how to wire it up: import { createYoga } from 'graphql-yoga';
import { buildSubgraphSchema } from '@apollo/subgraph';
import { parse } from 'graphql';
// Define your subgraph schema with Federation directives
const typeDefs = parse(`
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0",
import: ["@key", "@shareable", "@external", "@requires"])
type Product @key(fields: "id") {
id: ID!
name: String!
price: Float!
}
type Query {
product(id: ID!): Product
products: [Product!]!
}
`);
const resolvers = {
Product: {
// Reference resolver — called when the gateway needs to resolve a product
// from another subgraph that only has the key fields
__resolveReference: async (reference: { id: string }) => {
return await getProductById(reference.id);
},
},
Query: {
product: (_, { id }) => getProductById(id),
products: () => getAllProducts(),
},
};
const yoga = createYoga({
schema: buildSubgraphSchema({ typeDefs, resolvers }),
graphiql: {
// Enable Apollo Explorer for schema introspection during development
title: 'Product Subgraph',
},
});Federation v2 features that work well with Yoga:
Testing without a full gateway: // Mock the _service query that the gateway expects
// buildSubgraphSchema handles this automatically
fetch('http://localhost:4001/graphql', {
method: 'POST',
body: JSON.stringify({ query: '{ _service { sdl } }' }),
headers: { 'Content-Type': 'application/json' },
});For multi-agent APIs where each agent exposes its own GraphQL subgraph and a gateway federates them, this pattern works well: https://blog.kinthai.ai/221-agents-multi-agent-coordination-lessons |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any spec compliant graphql schema can be used. I haven't tried it but most likely will work. Let us know if it doesn't then we can explore more.