Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip - Pothos EdgeDB Plugin #539

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/plugin-edgedb/.gitignore
@@ -0,0 +1,3 @@
dbschema/edgeql-js/*
TODO.md
api-design.md
7 changes: 7 additions & 0 deletions packages/plugin-edgedb/.npmignore
@@ -0,0 +1,7 @@
dbschema
edgedb.toml
test
tests
.turbo
babel.config.js
tsconfig.tsbuildinfo
6 changes: 6 additions & 0 deletions packages/plugin-edgedb/LICENSE
@@ -0,0 +1,6 @@
ISC License (ISC)
Copyright 2021 Michael Hayes

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
92 changes: 92 additions & 0 deletions packages/plugin-edgedb/README.md
@@ -0,0 +1,92 @@
# EdgeDB Plugin for Pothos

This plugin provides tighter integration with `edgedb`, making it easier to define edgedb based
object types.

## Example

Here is a quick example of what an API using this plugin might look like. There is a more thorough
breakdown of what the methods and options used in the example below.

```typescript
// Create an object type based on a edgedb model
// without providing any custom type information
builder.edgeDBObject('User', {
fields: (t) => ({
// expose fields from the database
id: t.exposeID('id'),
email: t.exposeString('email'),
name: t.exposeString('name', { nullable: true }),
// Multi Link field for Users Posts
posts: t.link("posts"),
}),
});

builder.queryType({
fields: (t) => ({
// Define a field that issues an optimized edgedb query
me: t.edgeDBField({
type: 'User',
resolve: async (query, root, args, ctx, info) => {
const db_query = e
.select(e.User, (user) => {
// the `query` argument will add in the `select`s fields to
// resolve as much of the request in a single query as possible
...query,
filter: e.op(user.id, '=', ctx.user.id),
});

return await db.run(db_query);
}
}),
}),
});
```

Given this schema, you would be able to resolve a query like the following with a single edgedb
query.

```graphql
query {
me {
email
posts {
title
author {
id
}
}
}
}
```

## EdgeDB Expressions

The `e` variable provides everything you need to build an edgedb query. All EdgeQL commands,
standard library functions, and types are available as properties on e.

[Source](https://www.edgedb.com/docs/clients/js/querybuilder#expressions)

```ts
import e from './dbschema/edgeql-js';

// commands
e.select;
e.insert;
e.update;
e.delete;

// types
e.str;
e.bool;
e.cal.local_date;
e.User;
e.Post;
e....;

// functions
e.str_upper;
e.len;
e.count;
e.math.stddev;
```
64 changes: 64 additions & 0 deletions packages/plugin-edgedb/dbschema/default.esdl
@@ -0,0 +1,64 @@
module default {
type Post {
required property big_int_id -> bigint {
constraint exclusive;
};
required property created_at -> datetime {
default := datetime_current();
constraint exclusive;
readonly := true;
};
required property updated_at -> datetime;

required property title -> str;
property content -> str;
required property published -> bool;

link author -> User;
}

type Media {
required property url -> str;
multi link posts -> PostMedia;
link uploaded_by -> User;
}

type PostMedia {
required link post -> Post;
required link media -> Media;
}

type Comment {
required property created_at -> datetime {
default := datetime_current();
constraint exclusive;
readonly := true;
};
required property content -> str;
required link author -> User;
required link post -> Post;
}

type Profile {
property bio -> str;
required link user -> User;
}

type User {
required property email -> str {
constraint exclusive;
};
property name -> str;
multi link posts -> Post;
multi link comments -> Comment;
link profile -> Profile;
multi link followers -> Follow;
multi link following -> Follow;
multi link media -> Media;
}

type Follow {
required link from_user -> User;
required link to_user -> User;
}
}
18 changes: 18 additions & 0 deletions packages/plugin-edgedb/dbschema/migrations/00001.edgeql
@@ -0,0 +1,18 @@
CREATE MIGRATION m1co7oxdqc52kxete3rozfhbkb67kofxnqspppu7ek6pe327a5mtyq
ONTO initial
{
CREATE TYPE default::Post {
CREATE REQUIRED PROPERTY big_int_id -> std::bigint {
CREATE CONSTRAINT std::exclusive;
};
CREATE PROPERTY content -> std::str;
CREATE REQUIRED PROPERTY created_at -> std::datetime {
SET default := (std::datetime_current());
SET readonly := true;
CREATE CONSTRAINT std::exclusive;
};
CREATE REQUIRED PROPERTY published -> std::bool;
CREATE REQUIRED PROPERTY title -> std::str;
CREATE REQUIRED PROPERTY updated_at -> std::datetime;
};
};
56 changes: 56 additions & 0 deletions packages/plugin-edgedb/dbschema/migrations/00002.edgeql
@@ -0,0 +1,56 @@
CREATE MIGRATION m1goni6inys6wt4wokvqvemtqiiambmkfxudnf6rsculrh645bozla
ONTO m1co7oxdqc52kxete3rozfhbkb67kofxnqspppu7ek6pe327a5mtyq
{
CREATE TYPE default::Comment {
CREATE REQUIRED LINK post -> default::Post;
CREATE REQUIRED PROPERTY content -> std::str;
CREATE REQUIRED PROPERTY created_at -> std::datetime {
SET default := (std::datetime_current());
SET readonly := true;
CREATE CONSTRAINT std::exclusive;
};
};
CREATE TYPE default::User {
CREATE MULTI LINK comments -> default::Comment;
CREATE MULTI LINK posts -> default::Post;
CREATE REQUIRED PROPERTY email -> std::str {
CREATE CONSTRAINT std::exclusive;
};
CREATE PROPERTY name -> std::str;
};
ALTER TYPE default::Comment {
CREATE REQUIRED LINK author -> default::User;
};
CREATE TYPE default::Follow {
CREATE REQUIRED LINK from_user -> default::User;
CREATE REQUIRED LINK to_user -> default::User;
};
ALTER TYPE default::User {
CREATE MULTI LINK followers -> default::Follow;
CREATE MULTI LINK following -> default::Follow;
};
CREATE TYPE default::Media {
CREATE LINK uploaded_by -> default::User;
CREATE REQUIRED PROPERTY url -> std::str;
};
CREATE TYPE default::PostMedia {
CREATE REQUIRED LINK media -> default::Media;
CREATE REQUIRED LINK post -> default::Post;
};
ALTER TYPE default::Media {
CREATE MULTI LINK posts -> default::PostMedia;
};
ALTER TYPE default::User {
CREATE MULTI LINK media -> default::Media;
};
ALTER TYPE default::Post {
CREATE LINK author -> default::User;
};
CREATE TYPE default::Profile {
CREATE REQUIRED LINK user -> default::User;
CREATE PROPERTY bio -> std::str;
};
ALTER TYPE default::User {
CREATE LINK profile -> default::Profile;
};
};
2 changes: 2 additions & 0 deletions packages/plugin-edgedb/edgedb.toml
@@ -0,0 +1,2 @@
[edgedb]
server-version = "2.1"
4 changes: 4 additions & 0 deletions packages/plugin-edgedb/esm/.gitignore
@@ -0,0 +1,4 @@
*
!.gitignore
!.npmignore
!package.json
Empty file.
3 changes: 3 additions & 0 deletions packages/plugin-edgedb/esm/package.json
@@ -0,0 +1,3 @@
{
"type": "module"
}
6 changes: 6 additions & 0 deletions packages/plugin-edgedb/jest.config.js
@@ -0,0 +1,6 @@
module.exports = {
transformIgnorePatterns: [`node_modules`],
transform: {
'^.+\\.(t|j)sx?$': ['@swc/jest'],
},
};
57 changes: 57 additions & 0 deletions packages/plugin-edgedb/package.json
@@ -0,0 +1,57 @@
{
"name": "@pothos/plugin-edgedb",
"version": "3.4.0",
"description": "An edgedb plugin for Pothos",
"main": "./lib/index.js",
"types": "./dts/index.d.ts",
"module": "./esm/index.js",
"exports": {
"types": "./dts/index.d.ts",
"import": "./esm/index.js",
"require": "./lib/index.js"
},
"scripts": {
"generate": "edgeql-js --output-dir tests/client/",
"type": "tsc --project tsconfig.type.json",
"build": "pnpm build:clean && pnpm build:cjs && pnpm build:esm && pnpm build:dts",
"build:clean": "git clean -dfX esm lib",
"build:cjs": "swc src -d lib --config-file ../../.swcrc -C module.type=commonjs",
"build:esm": "swc src -d esm --config-file ../../.swcrc -C module.type=es6 && pnpm esm:extensions",
"build:dts": "tsc",
"esm:extensions": "TS_NODE_PROJECT=../../tsconfig.json node -r @swc-node/register ../../.config/esm-transformer.ts",
"test": "jest --runInBand"
},
"repository": {
"type": "git",
"url": "git+https://github.com/hayes/pothos.git"
},
"author": "Michael Hayes",
"license": "ISC",
"keywords": [
"giraphql",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably don't need giraphql in keywords anymore, I think pothos had gotten enough traction that there shouldn't be many people still looking for giraphql

"pothos",
"graphql",
"schema",
"edgedb",
"plugin"
],
"publishConfig": {
"access": "public"
},
"peerDependencies": {
"@pothos/core": "*",
"graphql": ">=15.1.0"
},
"devDependencies": {
"@pothos/core": "workspace:*",
"@pothos/plugin-complexity": "workspace:*",
"@pothos/plugin-errors": "workspace:*",
"@pothos/plugin-relay": "workspace:*",
"@pothos/plugin-simple-objects": "workspace:*",
"@pothos/test-utils": "workspace:*",
"edgedb": "^0.21.3",
"graphql": "16.5.0",
"graphql-tag": "^2.12.6"
},
"gitHead": "9dfe52f1975f41a111e01bf96a20033a914e2acc"
}