import {
createServer
} from 'node:http';
import {
createSchema,
createYoga
} from 'graphql-yoga';
const projects = [{
id: '1',
tenantId: 'acme',
name: 'Website'
},
{
id: '2',
tenantId: 'globex',
name: 'Mobile app'
},
];
const yoga = createYoga < {
tenantId: string
} > ({
schema: createSchema({
typeDefs: `type Project { id: ID!, name: String! }
type Query { projects: [Project!]! }`,
resolvers: {
Query: {
projects: (_parent, _args, ctx) =>
projects.filter((p) => p.tenantId === ctx.tenantId),
},
},
}),
// In production, derive the tenant from a verified session or JWT.
context: ({
request
}) => ({
tenantId: request.headers.get('x-tenant') ?? ''
}),
});
createServer(yoga).listen(4000, () => console.log(
'http://localhost:4000/graphql'));