Getting Started
Install
Install the package with Orchid ORM, a PostgreSQL driver, and GraphQL:
sh
npm i orchid-graphql orchid-orm postgres graphqlThe example below also uses Apollo Server and a GraphQL client:
sh
npm i @apollo/server graphql-tag graphql-requestIt connects to the PostgreSQL database at DATABASE_URL and creates a post table there.
Minimal all-in-one example
Run the GraphQL server:
ts
// Everything is put into a single file for demonstration purposes.
//
// In real projects, you will want to separate tables, typedefs,
// resolvers, and the server into their own modules.
import { env } from "node:process"
import type { ApolloServerOptions } from "@apollo/server"
import { ApolloServer } from "@apollo/server"
import { startStandaloneServer } from "@apollo/server/standalone"
import gql from "graphql-tag"
import * as r from "orchid-graphql"
import { createBaseTable } from "orchid-orm"
import { orchidORM } from "orchid-orm/postgres-js"
// Define database tables
const BaseTable = createBaseTable()
class PostTable extends BaseTable {
readonly table = "post"
columns = this.setColumns(t => ({
id: t.identity().primaryKey(),
text: t.text(),
}))
}
const db = orchidORM(
{
databaseURL: env.DATABASE_URL,
log: true,
},
{
post: PostTable,
},
)
await db.$query`
create table post (
id serial primary key,
text text not null
);
`
// Define GraphQL schema
const typeDefs = gql`
type Post {
id: Int!
text: String!
}
type Mutation {
create_post(text: String!): Post!
}
type Query {
posts: [Post!]!
}
`
// Map GraphQL types to table resolvers
const graph = r.graph({
Post: r.table(db.post),
})
// Define resolvers
const resolvers: ApolloServerOptions<any>["resolvers"] = {
Mutation: {
async create_post(_parent, args, context, info) {
const post = await db.post.create(args)
return await graph.resolve(db.post.find(post.id), { context, info })
},
},
Query: {
async posts(_parent, _args, context, info) {
return await graph.resolve(db.post, { context, info })
},
},
}
// Start GraphQL server
const server = new ApolloServer({ typeDefs, resolvers })
const { url } = await startStandaloneServer(server, {
listen: { port: 4000 },
})
console.log(`Listening on ${url}`)Query it with a GraphQL client:
ts
import { GraphQLClient } from "graphql-request"
import gql from "graphql-tag"
const client = new GraphQLClient("http://127.0.0.1:4000")
await client.request(
gql`
mutation create_post($text: String!) {
new_post: create_post(text: $text) {
id
}
}
`,
{ text: "Hello, world!" },
)
const { posts } = await client.request(gql`
query {
posts {
id
text
}
}
`)
console.log(posts)How it works
Map each GraphQL type whose objects your tables load to its table with r.graph. In each root resolver, build an Orchid ORM query and return graph.resolve(query, { context, info }). The graph reads the fields that the client selected from info and adds the matching columns and relations to the query.
Next steps
- Resolving Queries: resolve lists, single objects, mutation results, and objects nested in a root field's result.
- Relations: resolve nested relations.
- Fields: choose, rename, and compute the fields of a type.
- Access Control: hide fields and rows from a viewer.
- Pagination: return lists page by page.
- Filters: let clients filter lists with GraphQL arguments.