@flightdev/graphql
v0.1.1
Published
GraphQL adapter for Flight Framework - optional, non-imposing
Readme
@flightdev/graphql
GraphQL adapter for Flight Framework. Optional, non-imposing - use it only if you need GraphQL.
Installation
npm install @flightdev/graphql graphql graphql-yogaQuick Start
Create a GraphQL endpoint in your Flight app:
// src/routes/api/graphql.ts
import { createGraphQLHandler } from '@flightdev/graphql';
import { yogaAdapter } from '@flightdev/graphql/yoga';
import { schema } from '../../graphql/schema';
const handler = createGraphQLHandler({
adapter: yogaAdapter(),
schema,
});
export const GET = handler;
export const POST = handler;Creating a Schema
Code-First (Recommended)
// src/graphql/schema.ts
import { createSchema } from 'graphql-yoga';
export const schema = createSchema({
typeDefs: /* GraphQL */ `
type Query {
hello(name: String): String!
users: [User!]!
}
type User {
id: ID!
name: String!
email: String!
}
`,
resolvers: {
Query: {
hello: (_, { name }) => `Hello ${name || 'World'}`,
users: () => db.users.findMany(),
},
},
});Schema-First
import { buildSchema } from 'graphql';
import { createGraphQLHandler } from '@flightdev/graphql';
import { yogaAdapter } from '@flightdev/graphql/yoga';
const schema = buildSchema(/* GraphQL */ `
type Query {
hello: String
}
`);
const rootValue = {
hello: () => 'Hello World',
};
export const POST = createGraphQLHandler({
adapter: yogaAdapter({ rootValue }),
schema,
});Context
Pass request context to resolvers:
const handler = createGraphQLHandler({
adapter: yogaAdapter(),
schema,
context: async (req) => {
const token = req.headers.get('Authorization');
const user = token ? await verifyToken(token) : null;
return { user, db };
},
});Access in resolvers:
const resolvers = {
Query: {
me: (_, __, ctx) => ctx.user,
posts: (_, __, ctx) => ctx.db.posts.findMany(),
},
};GraphiQL
GraphiQL is enabled by default in development. Access it at your endpoint URL.
To disable:
const handler = createGraphQLHandler({
adapter: yogaAdapter({ graphiql: false }),
schema,
});Adapters
GraphQL Yoga (Default)
import { yogaAdapter } from '@flightdev/graphql/yoga';Yoga is the recommended adapter - modern, lightweight, full fetch API support.
Apollo (Coming Soon)
import { apolloAdapter } from '@flightdev/graphql/apollo';TypeScript
Full TypeScript support with type inference:
import type { GraphQLContext, GraphQLAdapter } from '@flightdev/graphql';License
MIT
