Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaSemenov committed Jun 20, 2024
1 parent f8ed18b commit 1a08b9e
Show file tree
Hide file tree
Showing 47 changed files with 3,132 additions and 1,195 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-and-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: 18.12.1
cache: "pnpm"
cache: pnpm

- name: Install deps
run: pnpm install --frozen-lockfile
Expand Down
1 change: 0 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"recommendations": [
"dbaeumer.vscode-eslint",
"EditorConfig.EditorConfig",
"esbenp.prettier-vscode",
"GraphQL.vscode-graphql",
"GraphQL.vscode-graphql-syntax"
]
Expand Down
27 changes: 21 additions & 6 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
{
"[json][jsonc][markdown]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
// Disable the default formatter, use eslint instead
"prettier.enable": false,
"editor.formatOnSave": false,

// Auto fix
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit",
"source.organizeImports": "never"
},
"editor.formatOnSave": true,
"eslint.experimental.useFlatConfig": true,
"eslint.validate": ["javascript", "typescript"],

// Enable eslint for all supported languages
"eslint.validate": [
"javascript",
"typescript",
"html",
"markdown",
"json",
"jsonc",
"yaml",
"toml",
"xml",
"gql",
"graphql"
],

"search.exclude": {
"**/node_modules": true,
"packages/*/dist": true
Expand Down
4 changes: 4 additions & 0 deletions 1.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Post {
id: Int!
text: String!
}
9 changes: 9 additions & 0 deletions 1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import gql from "graphql-tag"

export const typeDefs = gql`
type Post {
id: Int!
text: String!
text: Int!
}
`
58 changes: 43 additions & 15 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
import eslint from "@eslint/js"
import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended"
import simpleImportSort from "eslint-plugin-simple-import-sort"
import tseslint from "typescript-eslint"
import config from "@antfu/eslint-config"
import * as graphqlPlugin from "@graphql-eslint/eslint-plugin"

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
export default config(
{
plugins: {
"simple-import-sort": simpleImportSort,
formatters: {
graphql: true,
},
stylistic: {
quotes: "double",
},
rules: {
// Always use { } after if/for.
"curly": ["error", "all"],
"import/order": [
"error",
{
// At least one new line between each group will be enforced, and new lines inside a group will be forbidden.
"newlines-between": "always",
"alphabetize": {
order: "asc",
orderImportKind: "asc",
},
},
],
"no-console": "warn",
// Don't replace "a" + b to `a${b}`
"prefer-template": "off",
"test/consistent-test-it": "off",
// Allow types similar to interfaces
"ts/consistent-type-definitions": "off",
// Allow shorthand method type signature
"ts/method-signature-style": "off",
// One true brace style.
"style/brace-style": ["error", "1tbs"],
"unicorn/template-indent": [
"error",
],
},
},
{
files: ["**/*.md/*.ts", "**/readme.sample.ts"],
rules: {
"simple-import-sort/imports": "warn",
"simple-import-sort/exports": "warn",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-empty-interface": "off",
"no-console": "off",
},
},
).prepend(
{
ignores: ["packages/*/dist/"],
files: ["**/*.ts"],
processor: graphqlPlugin.processors.graphql,
},
eslintPluginPrettierRecommended,
)
26 changes: 12 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
{
"description": "graphql-orm monorepo root",
"private": true,
"type": "module",
"version": "0.0.0",
"private": true,
"description": "graphql-orm monorepo root",
"author": "Ilya Semenov",
"license": "MIT",
"type": "module",
"scripts": {
"lint": "eslint --fix . && prettier --write '**/*.{json,md}'",
"lint": "eslint --fix .",
"prepare": "husky"
},
"devDependencies": {
"@changesets/cli": "~2.27.1",
"eslint": "~8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "~5.1.3",
"eslint-plugin-simple-import-sort": "~12.0.0",
"husky": "~9.0.11",
"lint-staged": "~15.2.2",
"prettier": "^3.2.5",
"typescript": "~5.3.3",
"typescript-eslint": "~7.0.2"
"@antfu/eslint-config": "^2.21.1",
"@changesets/cli": "^2.27.1",
"@graphql-eslint/eslint-plugin": "^3.20.1",
"eslint": "^9.5.0",
"eslint-plugin-format": "^0.1.2",
"husky": "^9.0.11",
"lint-staged": "^15.2.2",
"typescript": "^5.3.3"
}
}
12 changes: 6 additions & 6 deletions packages/graphql-orm/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "graphql-orm",
"description": "Common base for objection-graphql-resolver and orchid-graphql",
"private": true,
"type": "module",
"version": "1.4.1",
"private": true,
"description": "Common base for objection-graphql-resolver and orchid-graphql",
"author": "Ilya Semenov",
"license": "MIT",
"type": "module",
"dependencies": {
"graphql-parse-resolve-info": "^4.13.0"
},
"peerDependencies": {
"graphql": "^16"
},
"dependencies": {
"graphql-parse-resolve-info": "^4.13.0"
},
"devDependencies": {
"tsconfig-vite-node": "^1.1.0"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql-orm/src/filters/filters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OrmAdapter } from "../orm/orm"
import { TableResolveContext } from "../resolvers/table"
import type { OrmAdapter } from "../orm/orm"
import type { TableResolveContext } from "../resolvers/table"
import { is_plain_object } from "../utils/is-plain-object"

// TODO: per-field definitions
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql-orm/src/orm/orm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GetPageFn } from "../paginators/base"
import type { GetPageFn } from "../paginators/base"

export type OrmModifier<Orm extends OrmAdapter> = (
query: Orm["Query"],
Expand Down Expand Up @@ -32,7 +32,7 @@ export interface OrmAdapter<Table = any, Query = any, QueryTransform = any> {

// Select

select_field(query: Query, opts: { field: string; as: string }): Query
select_field(query: Query, opts: { field: string, as: string }): Query

select_relation(
query: Query,
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql-orm/src/paginators/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OrmAdapter } from "../orm/orm"
import { GraphResolveContext, GraphResolver } from "../resolvers/graph"
import type { OrmAdapter } from "../orm/orm"
import type { GraphResolveContext, GraphResolver } from "../resolvers/graph"

export interface Paginator<Orm extends OrmAdapter, Context> {
/** GraphQL path, for example: `["nodes"]` */
Expand Down
8 changes: 4 additions & 4 deletions packages/graphql-orm/src/paginators/cursor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OrmAdapter } from "../orm/orm"
import { PaginateContext, Paginator } from "./base"
import type { OrmAdapter } from "../orm/orm"

import type { PaginateContext, Paginator } from "./base"

export function defineCursorPaginator(
options: Partial<CursorPaginatorOptions> = {},
Expand All @@ -23,8 +24,7 @@ export interface CursorPaginatorPage<M> {
}

class CursorPaginator<Orm extends OrmAdapter, Context>
implements Paginator<Orm, Context>
{
implements Paginator<Orm, Context> {
readonly path = ["nodes"]
readonly options: CursorPaginatorOptions

Expand Down
5 changes: 3 additions & 2 deletions packages/graphql-orm/src/resolvers/field.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OrmAdapter } from "../orm/orm"
import type { OrmAdapter } from "../orm/orm"
import { run_after_query } from "../utils/run-after"
import { TableResolveContext } from "./table"

import type { TableResolveContext } from "./table"

export interface FieldResolverOptions<Orm extends OrmAdapter, Context> {
/** Table field (if different from GraphQL field) */
Expand Down
10 changes: 6 additions & 4 deletions packages/graphql-orm/src/resolvers/graph.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { GraphQLResolveInfo } from "graphql"
import { parseResolveInfo, ResolveTree } from "graphql-parse-resolve-info"
import type { ResolveTree } from "graphql-parse-resolve-info"
import { parseResolveInfo } from "graphql-parse-resolve-info"

import type { FiltersDef } from "../filters/filters"
import type { OrmAdapter } from "../orm/orm"
import type { Paginator } from "../paginators/base"

import { FiltersDef } from "../filters/filters"
import { OrmAdapter } from "../orm/orm"
import { Paginator } from "../paginators/base"
import type { TableResolver, TableResolverOptions } from "./table"

export type GraphResolverOptions<Orm extends OrmAdapter, Context> = Pick<
Expand Down
10 changes: 6 additions & 4 deletions packages/graphql-orm/src/resolvers/page.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { OrmAdapter } from "../orm/orm"
import { Paginator } from "../paginators/base"
import { FieldResolver, parse_field_options } from "./field"
import { RelationResolverOptions } from "./relation"
import type { OrmAdapter } from "../orm/orm"
import type { Paginator } from "../paginators/base"

import type { FieldResolver } from "./field"
import { parse_field_options } from "./field"
import type { RelationResolverOptions } from "./relation"

export function definePageResolver<Orm extends OrmAdapter, Context>(
paginator: Paginator<Orm, Context>,
Expand Down
14 changes: 6 additions & 8 deletions packages/graphql-orm/src/resolvers/relation.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { FiltersDef } from "../filters/filters"
import { OrmAdapter } from "../orm/orm"
import {
FieldResolver,
FieldResolverOptions,
parse_field_options,
} from "./field"
import { TableResolveModifier } from "./table"
import type { FiltersDef } from "../filters/filters"
import type { OrmAdapter } from "../orm/orm"

import type { FieldResolver, FieldResolverOptions } from "./field"
import { parse_field_options } from "./field"
import type { TableResolveModifier } from "./table"

export interface RelationResolverOptions<Orm extends OrmAdapter, Context>
extends Omit<FieldResolverOptions<Orm, Context>, "modify"> {
Expand Down
37 changes: 20 additions & 17 deletions packages/graphql-orm/src/resolvers/table.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { apply_filters, ApplyFiltersModifier } from "../filters/filters"
import { OrmAdapter } from "../orm/orm"
import type { ApplyFiltersModifier } from "../filters/filters"
import { apply_filters } from "../filters/filters"
import type { OrmAdapter } from "../orm/orm"
import { run_after_query } from "../utils/run-after"
import { defineFieldResolver, FieldResolver } from "./field"

import type { FieldResolver } from "./field"
import { defineFieldResolver } from "./field"
import type { GraphResolveContext, GraphResolver } from "./graph"
import { defineRelationResolver } from "./relation"

Expand Down Expand Up @@ -57,8 +60,8 @@ export class TableResolver<Orm extends OrmAdapter, Context> {
Record<string, FieldResolver<Orm, Context>>
>((resolvers, field) => {
const r0 = fields[field]
const r: FieldResolver<Orm, Context> | undefined =
typeof r0 === "function"
const r: FieldResolver<Orm, Context> | undefined
= typeof r0 === "function"
? r0
: r0 === true
? this._get_default_field_resolver(field)
Expand Down Expand Up @@ -90,10 +93,10 @@ export class TableResolver<Orm extends OrmAdapter, Context> {
)
}

const allow_all_fields =
this.options.allowAllFields ??
graph.options.allowAllFields ??
!this.options.fields
const allow_all_fields
= this.options.allowAllFields
?? graph.options.allowAllFields
?? !this.options.fields

if (!allow_all_fields && !this.table_field_resolvers) {
throw new Error(
Expand All @@ -107,9 +110,9 @@ export class TableResolver<Orm extends OrmAdapter, Context> {

for (const subtree of Object.values(tree.fieldsByTypeName[type])) {
const field = subtree.name
const r: FieldResolver<Orm, Context> | undefined =
this.table_field_resolvers?.[field] ||
(allow_all_fields ? this._get_default_field_resolver(field) : undefined)
const r: FieldResolver<Orm, Context> | undefined
= this.table_field_resolvers?.[field]
|| (allow_all_fields ? this._get_default_field_resolver(field) : undefined)
if (!r) {
throw new Error(`No field resolver defined for field ${type}.${field}.`)
}
Expand All @@ -118,8 +121,8 @@ export class TableResolver<Orm extends OrmAdapter, Context> {
}
}

const allow_all_filters =
this.options.allowAllFilters ?? graph.options.allowAllFilters
const allow_all_filters
= this.options.allowAllFilters ?? graph.options.allowAllFilters

const effective_filters = allow_all_filters ? true : filters

Expand Down Expand Up @@ -151,11 +154,11 @@ export class TableResolver<Orm extends OrmAdapter, Context> {
): FieldResolver<Orm, Context> {
const table_field_lookup = tableField || field
if (
this.options.modifiers?.[table_field_lookup] ||
this.virtual_fields.has(table_field_lookup)
this.options.modifiers?.[table_field_lookup]
|| this.virtual_fields.has(table_field_lookup)
) {
// Keep query as is.
return (query) => query
return query => query
} else if (this.relations.has(table_field_lookup)) {
// TODO: pre-create and cache
return defineRelationResolver({ tableField })
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-orm/src/utils/run-after.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OrmAdapter } from "../orm/orm"
import type { OrmAdapter } from "../orm/orm"

export function run_after_query<Orm extends OrmAdapter>(
orm: Orm,
Expand Down
2 changes: 1 addition & 1 deletion packages/objection-graphql-resolver/docs/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Define modifiers on a model class:
```ts
class PostModel extends Model {
static modifiers = {
public: (query) => query.whereNull("delete_time"),
public: query => query.whereNull("delete_time"),
search: (query, term) => query.where("text", "ilike", `%${term}%`),
}
}
Expand Down
Loading

0 comments on commit 1a08b9e

Please sign in to comment.