|
1 | | -# Drizzle-GraphQL |
2 | | - |
3 | | -Automatically create GraphQL schema or customizable schema config fields from Drizzle ORM schema |
4 | | - |
5 | | -## Usage |
6 | | - |
7 | | -- Pass your drizzle database instance and schema into builder to generate `{ schema, entities }` object |
8 | | -- Use `schema` if pre-built schema already satisfies all your neeeds. It's compatible witn any server that consumes `GraphQLSchema` class instance |
9 | | - |
10 | | - Example: hosting schema using [GraphQL Yoga](https://the-guild.dev/graphql/yoga-server) |
11 | | - |
12 | | - ```Typescript |
13 | | - import { createServer } from 'node:http' |
14 | | - import { createYoga } from 'graphql-yoga' |
15 | | - import { buildVanillaSchema } from 'drizzle-graphql' |
16 | | - |
17 | | - // db - your drizzle instance, schema - your drizzle tables |
18 | | - import { db, schema as dbSchema } from './database' |
19 | | - |
20 | | - const { schema } = buildVanillaSchema(db, dbSchema) |
21 | | - |
22 | | - const yoga = createYoga({ schema }) |
23 | | - |
24 | | - server.listen(4000, () => { |
25 | | - console.info('Server is running on http://localhost:4000/graphql') |
26 | | - }) |
27 | | - ``` |
28 | | - |
29 | | -- If you want to customize your schema, you can use `entities` object to build your own new schema |
30 | | - |
31 | | - ```Typescript |
32 | | - import { createServer } from 'node:http' |
33 | | - import { GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLSchema } from 'graphql' |
34 | | - import { createYoga } from 'graphql-yoga' |
35 | | - import { buildVanillaSchema } from 'drizzle-graphql' |
36 | | -
|
37 | | - // Schema contains 'Users' and 'Customers' tables |
38 | | - import { db, schema as dbSchema } from './database' |
39 | | -
|
40 | | - const { entities } = buildVanillaSchema(db, dbSchema) |
41 | | -
|
42 | | - // You can customize which parts of queries or mutations you want |
43 | | - const schema = new GraphQLSchema({ |
44 | | - query: new GraphQLObjectType({ |
45 | | - name: 'Query', |
46 | | - fields: { |
47 | | - // Select only wanted queries out of all generated |
48 | | - users: entities.queries.users, |
49 | | - customer: entities.queries.customersSingle, |
50 | | -
|
51 | | - // Create a custom one |
52 | | - customUsers: { |
53 | | - // You can reuse and customize types from original schema |
54 | | - type: new GraphQLList(new GraphQLNonNull(entities.types.UsersItem)), |
55 | | - args: { |
56 | | - // You can reuse inputs as well |
57 | | - where: { |
58 | | - type: entities.inputs.UsersFilters |
59 | | - } |
60 | | - }, |
61 | | - resolve: async (source, args, context, info) => { |
62 | | - // Your custom logic goes here... |
63 | | - const result = await db.select(schema.Users).where()... |
64 | | -
|
65 | | - return result |
66 | | - } |
67 | | - } |
68 | | - } |
69 | | - }), |
70 | | - // Same rules apply to mutations |
71 | | - mutation: new GraphQLObjectType({ |
72 | | - name: 'Mutation', |
73 | | - fields: entities.mutations |
74 | | - }), |
75 | | - // In case you need types inside your schema |
76 | | - types: [...Object.values(entities.types), ...Object.values(entities.inputs)] |
77 | | - }) |
78 | | -
|
79 | | - const yoga = createYoga({ |
80 | | - schema |
81 | | - }) |
82 | | -
|
83 | | - server.listen(4000, () => { |
84 | | - console.info('Server is running on http://localhost:4000/graphql') |
85 | | - }) |
86 | | - ``` |
| 1 | +# Drizzle-GraphQL |
| 2 | + |
| 3 | +Automatically create GraphQL schema or customizable schema config fields from Drizzle ORM schema |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +- Pass your drizzle database instance and schema into builder to generate `{ schema, entities }` object |
| 8 | +- Use `schema` if pre-built schema already satisfies all your neeeds. It's compatible witn any server that consumes `GraphQLSchema` class instance |
| 9 | + |
| 10 | + Example: hosting schema using [GraphQL Yoga](https://the-guild.dev/graphql/yoga-server) |
| 11 | + |
| 12 | + ```Typescript |
| 13 | + import { createServer } from 'node:http' |
| 14 | + import { createYoga } from 'graphql-yoga' |
| 15 | + import { buildVanillaSchema } from 'drizzle-graphql' |
| 16 | + |
| 17 | + // db - your drizzle instance, schema - your drizzle tables |
| 18 | + import { db, schema as dbSchema } from './database' |
| 19 | + |
| 20 | + const { schema } = buildVanillaSchema(db, dbSchema) |
| 21 | + |
| 22 | + const yoga = createYoga({ schema }) |
| 23 | + |
| 24 | + server.listen(4000, () => { |
| 25 | + console.info('Server is running on http://localhost:4000/graphql') |
| 26 | + }) |
| 27 | + ``` |
| 28 | + |
| 29 | +- If you want to customize your schema, you can use `entities` object to build your own new schema |
| 30 | + |
| 31 | + ```Typescript |
| 32 | + import { createServer } from 'node:http' |
| 33 | + import { GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLSchema } from 'graphql' |
| 34 | + import { createYoga } from 'graphql-yoga' |
| 35 | + import { buildVanillaSchema } from 'drizzle-graphql' |
| 36 | +
|
| 37 | + // Schema contains 'Users' and 'Customers' tables |
| 38 | + import { db, schema as dbSchema } from './database' |
| 39 | +
|
| 40 | + const { entities } = buildVanillaSchema(db, dbSchema) |
| 41 | +
|
| 42 | + // You can customize which parts of queries or mutations you want |
| 43 | + const schema = new GraphQLSchema({ |
| 44 | + query: new GraphQLObjectType({ |
| 45 | + name: 'Query', |
| 46 | + fields: { |
| 47 | + // Select only wanted queries out of all generated |
| 48 | + users: entities.queries.users, |
| 49 | + customer: entities.queries.customersSingle, |
| 50 | +
|
| 51 | + // Create a custom one |
| 52 | + customUsers: { |
| 53 | + // You can reuse and customize types from original schema |
| 54 | + type: new GraphQLList(new GraphQLNonNull(entities.types.UsersItem)), |
| 55 | + args: { |
| 56 | + // You can reuse inputs as well |
| 57 | + where: { |
| 58 | + type: entities.inputs.UsersFilters |
| 59 | + } |
| 60 | + }, |
| 61 | + resolve: async (source, args, context, info) => { |
| 62 | + // Your custom logic goes here... |
| 63 | + const result = await db.select(schema.Users).where()... |
| 64 | +
|
| 65 | + return result |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + }), |
| 70 | + // Same rules apply to mutations |
| 71 | + mutation: new GraphQLObjectType({ |
| 72 | + name: 'Mutation', |
| 73 | + fields: entities.mutations |
| 74 | + }), |
| 75 | + // In case you need types inside your schema |
| 76 | + types: [...Object.values(entities.types), ...Object.values(entities.inputs)] |
| 77 | + }) |
| 78 | +
|
| 79 | + const yoga = createYoga({ |
| 80 | + schema |
| 81 | + }) |
| 82 | +
|
| 83 | + server.listen(4000, () => { |
| 84 | + console.info('Server is running on http://localhost:4000/graphql') |
| 85 | + }) |
| 86 | + ``` |
0 commit comments