@smartive/graphql-magic 14.0.2 → 15.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/.env +1 -1
  2. package/CHANGELOG.md +1 -6
  3. package/dist/bin/gqm.cjs +132 -121
  4. package/dist/cjs/index.cjs +134 -119
  5. package/dist/esm/client/queries.d.ts +1 -1
  6. package/dist/esm/client/queries.js +1 -1
  7. package/dist/esm/client/queries.js.map +1 -1
  8. package/dist/esm/context.d.ts +1 -1
  9. package/dist/esm/models/utils.d.ts +4 -4
  10. package/dist/esm/permissions/check.d.ts +1 -0
  11. package/dist/esm/permissions/check.js +19 -11
  12. package/dist/esm/permissions/check.js.map +1 -1
  13. package/dist/esm/resolvers/filters.js +1 -1
  14. package/dist/esm/resolvers/filters.js.map +1 -1
  15. package/dist/esm/resolvers/mutations.js +4 -4
  16. package/dist/esm/resolvers/mutations.js.map +1 -1
  17. package/dist/esm/resolvers/resolver.js +3 -0
  18. package/dist/esm/resolvers/resolver.js.map +1 -1
  19. package/dist/esm/resolvers/resolvers.d.ts +1 -1
  20. package/dist/esm/resolvers/resolvers.js +29 -23
  21. package/dist/esm/resolvers/resolvers.js.map +1 -1
  22. package/dist/esm/resolvers/selects.js +4 -3
  23. package/dist/esm/resolvers/selects.js.map +1 -1
  24. package/dist/esm/schema/generate.js +76 -72
  25. package/dist/esm/schema/generate.js.map +1 -1
  26. package/docker-compose.yml +0 -4
  27. package/package.json +6 -6
  28. package/src/bin/gqm/codegen.ts +3 -1
  29. package/src/bin/gqm/gqm.ts +3 -69
  30. package/src/bin/gqm/parse-knexfile.ts +5 -6
  31. package/src/bin/gqm/settings.ts +29 -3
  32. package/src/bin/gqm/templates.ts +70 -8
  33. package/src/client/queries.ts +2 -2
  34. package/src/context.ts +1 -1
  35. package/src/permissions/check.ts +24 -16
  36. package/src/resolvers/filters.ts +1 -1
  37. package/src/resolvers/mutations.ts +4 -4
  38. package/src/resolvers/resolver.ts +4 -0
  39. package/src/resolvers/resolvers.ts +33 -27
  40. package/src/resolvers/selects.ts +4 -3
  41. package/src/schema/generate.ts +78 -72
package/.env CHANGED
@@ -1,4 +1,4 @@
1
1
  DATABASE_HOST=localhost
2
2
  DATABASE_NAME=postgres
3
3
  DATABASE_USER=postgres
4
- DATABAE_PASSWORD=password
4
+ DATABASE_PASSWORD=password
package/CHANGELOG.md CHANGED
@@ -1,6 +1 @@
1
- ## [14.0.2](https://github.com/smartive/graphql-magic/compare/v14.0.1...v14.0.2) (2024-02-21)
2
-
3
-
4
- ### Bug Fixes
5
-
6
- * undefined case ([4dbb5be](https://github.com/smartive/graphql-magic/commit/4dbb5be09f626b459b581af8e875ed6ef45e319e))
1
+ # [15.0.0](https://github.com/smartive/graphql-magic/compare/v14.1.0...v15.0.0) (2024-04-03)
package/dist/bin/gqm.cjs CHANGED
@@ -1343,7 +1343,7 @@ var generateDefinitions = ({
1343
1343
  entities,
1344
1344
  objects
1345
1345
  }) => {
1346
- return [
1346
+ const definitions = [
1347
1347
  // Predefined types
1348
1348
  ...rawEnums.map((model) => enm(model.name, model.values)),
1349
1349
  ...enums.map((model) => enm(model.name, model.values)),
@@ -1487,80 +1487,84 @@ var generateDefinitions = ({
1487
1487
  ]
1488
1488
  })),
1489
1489
  ...objects.filter((model) => model.name === "Query").flatMap((model) => model.fields)
1490
- ]),
1491
- object("Mutation", [
1492
- ...entities.flatMap((model) => {
1493
- const mutations = [];
1494
- if (!isRootModel(model)) {
1495
- if (model.creatable) {
1496
- mutations.push({
1497
- name: `create${model.name}`,
1498
- type: model.name,
1499
- nonNull: true,
1500
- args: [
1501
- {
1502
- name: "data",
1503
- type: `Create${model.name}`,
1504
- nonNull: true
1505
- }
1506
- ]
1507
- });
1508
- }
1509
- if (model.updatable) {
1510
- mutations.push({
1511
- name: `update${model.name}`,
1512
- type: model.name,
1513
- nonNull: true,
1514
- args: [
1515
- {
1516
- name: "where",
1517
- type: `${model.name}WhereUnique`,
1518
- nonNull: true
1519
- },
1520
- {
1521
- name: "data",
1522
- type: `Update${model.name}`,
1523
- nonNull: true
1524
- }
1525
- ]
1526
- });
1527
- }
1528
- if (model.deletable) {
1529
- mutations.push({
1530
- name: `delete${model.name}`,
1531
- type: "ID",
1532
- nonNull: true,
1533
- args: [
1534
- {
1535
- name: "where",
1536
- type: `${model.name}WhereUnique`,
1537
- nonNull: true
1538
- },
1539
- {
1540
- name: "dryRun",
1541
- type: "Boolean"
1542
- }
1543
- ]
1544
- });
1545
- mutations.push({
1546
- name: `restore${model.name}`,
1547
- type: "ID",
1548
- nonNull: true,
1549
- args: [
1550
- {
1551
- name: "where",
1552
- type: `${model.name}WhereUnique`,
1553
- nonNull: true
1554
- }
1555
- ]
1556
- });
1557
- }
1558
- }
1559
- return mutations;
1560
- }),
1561
- ...objects.filter((model) => model.name === "Mutation").flatMap((model) => model.fields)
1562
1490
  ])
1563
1491
  ];
1492
+ const mutations = [
1493
+ ...entities.flatMap((model) => {
1494
+ const mutations2 = [];
1495
+ if (!isRootModel(model)) {
1496
+ if (model.creatable) {
1497
+ mutations2.push({
1498
+ name: `create${model.name}`,
1499
+ type: model.name,
1500
+ nonNull: true,
1501
+ args: [
1502
+ {
1503
+ name: "data",
1504
+ type: `Create${model.name}`,
1505
+ nonNull: true
1506
+ }
1507
+ ]
1508
+ });
1509
+ }
1510
+ if (model.updatable) {
1511
+ mutations2.push({
1512
+ name: `update${model.name}`,
1513
+ type: model.name,
1514
+ nonNull: true,
1515
+ args: [
1516
+ {
1517
+ name: "where",
1518
+ type: `${model.name}WhereUnique`,
1519
+ nonNull: true
1520
+ },
1521
+ {
1522
+ name: "data",
1523
+ type: `Update${model.name}`,
1524
+ nonNull: true
1525
+ }
1526
+ ]
1527
+ });
1528
+ }
1529
+ if (model.deletable) {
1530
+ mutations2.push({
1531
+ name: `delete${model.name}`,
1532
+ type: "ID",
1533
+ nonNull: true,
1534
+ args: [
1535
+ {
1536
+ name: "where",
1537
+ type: `${model.name}WhereUnique`,
1538
+ nonNull: true
1539
+ },
1540
+ {
1541
+ name: "dryRun",
1542
+ type: "Boolean"
1543
+ }
1544
+ ]
1545
+ });
1546
+ mutations2.push({
1547
+ name: `restore${model.name}`,
1548
+ type: "ID",
1549
+ nonNull: true,
1550
+ args: [
1551
+ {
1552
+ name: "where",
1553
+ type: `${model.name}WhereUnique`,
1554
+ nonNull: true
1555
+ }
1556
+ ]
1557
+ });
1558
+ }
1559
+ }
1560
+ return mutations2;
1561
+ }),
1562
+ ...objects.filter((model) => model.name === "Mutation").flatMap((model) => model.fields)
1563
+ ];
1564
+ if (mutations.length) {
1565
+ definitions.push(object("Mutation", mutations));
1566
+ }
1567
+ return definitions;
1564
1568
  };
1565
1569
  var generate = (models) => document(generateDefinitions(models));
1566
1570
  var printSchema = (schema) => [
@@ -1593,10 +1597,14 @@ var readLine = (prompt) => {
1593
1597
  };
1594
1598
 
1595
1599
  // src/bin/gqm/templates.ts
1596
- var EMPTY_MODELS = `
1597
- import { RawModels, Models } from '@smartive/graphql-magic';
1600
+ var GITIGNORE = (path) => `
1601
+ # graphql-magic
1602
+ ${path}/**/*
1603
+ ${path}/**/.gitkeep
1604
+ `;
1605
+ var EMPTY_MODELS = `import { ModelDefinitions, Models } from '@smartive/graphql-magic';
1598
1606
 
1599
- const rawModels: RawModels = [
1607
+ const modelDefinitions: ModelDefinitions = [
1600
1608
  {
1601
1609
  kind: 'entity',
1602
1610
  name: 'User',
@@ -1604,10 +1612,9 @@ const rawModels: RawModels = [
1604
1612
  },
1605
1613
  ]
1606
1614
 
1607
- export const models = new Models(rawModels);
1615
+ export const models = new Models(modelDefinitions);
1608
1616
  `;
1609
- var KNEXFILE = `
1610
- import { DateTime } from 'luxon';
1617
+ var KNEXFILE = `import { DateTime } from 'luxon';
1611
1618
  import { types } from 'pg';
1612
1619
 
1613
1620
  const dateOids = { date: 1082, timestamptz: 1184, timestamp: 1114 };
@@ -1620,7 +1627,7 @@ for (const oid of Object.values(numberOids)) {
1620
1627
  types.setTypeParser(oid, Number);
1621
1628
  }
1622
1629
 
1623
- const config = {
1630
+ const knexConfig = {
1624
1631
  client: 'postgresql',
1625
1632
  connection: {
1626
1633
  host: process.env.DATABASE_HOST,
@@ -1637,12 +1644,39 @@ const config = {
1637
1644
  },
1638
1645
  } as const;
1639
1646
 
1640
- export default config;
1647
+ export default knexConfig;
1648
+ `;
1649
+ var GET_ME = `import { gql } from '@smartive/graphql-magic';
1650
+
1651
+ export const GET_ME = gql\`
1652
+ query GetMe {
1653
+ me {
1654
+ id
1655
+ }
1656
+ }
1657
+ \`;
1641
1658
  `;
1642
1659
 
1643
1660
  // src/bin/gqm/settings.ts
1644
1661
  var SETTINGS_PATH = ".gqmrc.json";
1662
+ var DEFAULT_ENV = {
1663
+ DATABASE_HOST: "localhost",
1664
+ DATABASE_NAME: "postgres",
1665
+ DATABASE_USER: "postgres",
1666
+ DATABASE_PASSWORD: "password"
1667
+ };
1645
1668
  var DEFAULTS = {
1669
+ knexfilePath: {
1670
+ question: "What is the knexfile path?",
1671
+ defaultValue: "knexfile.ts",
1672
+ init: (path) => {
1673
+ for (const [name2, value2] of Object.entries(DEFAULT_ENV)) {
1674
+ ensureFileContains(".env", `${name2}=`, `${name2}=${value2}
1675
+ `);
1676
+ }
1677
+ ensureFileExists(path, KNEXFILE);
1678
+ }
1679
+ },
1646
1680
  modelsPath: {
1647
1681
  question: "What is the models path?",
1648
1682
  defaultValue: "src/config/models.ts",
@@ -1654,6 +1688,7 @@ var DEFAULTS = {
1654
1688
  question: "What is the path for generated stuff?",
1655
1689
  defaultValue: "src/generated",
1656
1690
  init: (path) => {
1691
+ ensureFileContains(".gitignore", GITIGNORE(path));
1657
1692
  ensureFileExists(`${path}/.gitkeep`, "");
1658
1693
  ensureFileExists(`${path}/db/.gitkeep`, "");
1659
1694
  ensureFileExists(`${path}/api/.gitkeep`, "");
@@ -1664,7 +1699,7 @@ var DEFAULTS = {
1664
1699
  question: "Where to look for graphql queries?",
1665
1700
  defaultValue: "src/graphql/client/queries",
1666
1701
  init: (path) => {
1667
- ensureDirectoryExists(path);
1702
+ ensureFileExists(`${path}/get-me.ts`, GET_ME);
1668
1703
  }
1669
1704
  },
1670
1705
  gqlModule: {
@@ -1730,6 +1765,13 @@ var ensureFileExists = (filePath, content) => {
1730
1765
  (0, import_fs.writeFileSync)(filePath, content);
1731
1766
  }
1732
1767
  };
1768
+ var ensureFileContains = (filePath, content, fallback) => {
1769
+ ensureFileExists(filePath, content);
1770
+ const fileContent = (0, import_fs.readFileSync)(filePath, "utf-8");
1771
+ if (!fileContent.includes(content)) {
1772
+ (0, import_fs.writeFileSync)(filePath, fileContent + (fallback ?? content));
1773
+ }
1774
+ };
1733
1775
  var writeToFile = (filePath, content) => {
1734
1776
  ensureDirectoryExists((0, import_path.dirname)(filePath));
1735
1777
  if ((0, import_fs.existsSync)(filePath)) {
@@ -1767,6 +1809,7 @@ var generateGraphqlApiTypes = async () => {
1767
1809
  var generateGraphqlClientTypes = async () => {
1768
1810
  const generatedFolderPath = await getSetting("generatedFolderPath");
1769
1811
  const graphqlQueriesPath = await getSetting("graphqlQueriesPath");
1812
+ ensureDirectoryExists(graphqlQueriesPath);
1770
1813
  await (0, import_cli.generate)({
1771
1814
  schema: `${generatedFolderPath}/schema.graphql`,
1772
1815
  documents: [graphqlQueriesPath, `${generatedFolderPath}/client/mutations.ts`],
@@ -1790,7 +1833,8 @@ var generateGraphqlClientTypes = async () => {
1790
1833
  },
1791
1834
  scalars: {
1792
1835
  DateTime: "string"
1793
- }
1836
+ },
1837
+ ignoreNoDocuments: true
1794
1838
  }
1795
1839
  });
1796
1840
  };
@@ -2038,16 +2082,16 @@ var findDeclaration = (syntaxList, name2) => {
2038
2082
  };
2039
2083
 
2040
2084
  // src/bin/gqm/parse-knexfile.ts
2041
- var KNEXFILE_PATH = `knexfile.ts`;
2042
2085
  var parseKnexfile = async () => {
2043
2086
  const project = new import_ts_morph4.Project({
2044
2087
  manipulationSettings: {
2045
2088
  indentationText: import_ts_morph4.IndentationText.TwoSpaces
2046
2089
  }
2047
2090
  });
2048
- ensureFileExists(KNEXFILE_PATH, KNEXFILE);
2049
- const sourceFile = project.addSourceFileAtPath(KNEXFILE_PATH);
2050
- const configDeclaration = findDeclarationInFile(sourceFile, "config");
2091
+ const knexfilePath = await getSetting("knexfilePath");
2092
+ ensureFileExists(knexfilePath, KNEXFILE);
2093
+ const sourceFile = project.addSourceFileAtPath(knexfilePath);
2094
+ const configDeclaration = findDeclarationInFile(sourceFile, "knexConfig");
2051
2095
  const config2 = staticEval(configDeclaration, {});
2052
2096
  return config2;
2053
2097
  };
@@ -2077,11 +2121,8 @@ var parseModels = async () => {
2077
2121
  path: ".env.local"
2078
2122
  });
2079
2123
  import_commander.program.description("The graphql-magic cli.");
2080
- import_commander.program.command("setup").description("Set up the project").action(async () => {
2081
- await getSettings();
2082
- ensureFileExists(KNEXFILE_PATH, KNEXFILE);
2083
- });
2084
2124
  import_commander.program.command("generate").description("Generate all the things").action(async () => {
2125
+ await getSetting("knexfilePath");
2085
2126
  const models = await parseModels();
2086
2127
  const generatedFolderPath = await getSetting("generatedFolderPath");
2087
2128
  const gqlModule = await getSetting("gqlModule");
@@ -2092,36 +2133,6 @@ import_commander.program.command("generate").description("Generate all the thing
2092
2133
  await generateGraphqlApiTypes();
2093
2134
  await generateGraphqlClientTypes();
2094
2135
  });
2095
- import_commander.program.command("generate-models").description("Generate models.json").action(async () => {
2096
- await parseModels();
2097
- });
2098
- import_commander.program.command("generate-schema").description("Generate schema").action(async () => {
2099
- const models = await parseModels();
2100
- const generatedFolderPath = await getSetting("generatedFolderPath");
2101
- writeToFile(`${generatedFolderPath}/schema.graphql`, printSchemaFromModels(models));
2102
- });
2103
- import_commander.program.command("generate-mutation-queries").description("Generate mutation-queries").action(async () => {
2104
- const models = await parseModels();
2105
- const generatedFolderPath = await getSetting("generatedFolderPath");
2106
- const gqlModule = await getSetting("gqlModule");
2107
- writeToFile(`${generatedFolderPath}/client/mutations.ts`, generateMutations(models, gqlModule));
2108
- });
2109
- import_commander.program.command("generate-db-types").description("Generate DB types").action(async () => {
2110
- const models = await parseModels();
2111
- const generatedFolderPath = await getSetting("generatedFolderPath");
2112
- writeToFile(`${generatedFolderPath}/db/index.ts`, generateDBModels(models));
2113
- });
2114
- import_commander.program.command("generate-knex-types").description("Generate Knex types").action(async () => {
2115
- const models = await parseModels();
2116
- const generatedFolderPath = await getSetting("generatedFolderPath");
2117
- writeToFile(`${generatedFolderPath}/db/knex.ts`, generateKnexTables(models));
2118
- });
2119
- import_commander.program.command("generate-graphql-api-types").description("Generate Graphql API types").action(async () => {
2120
- await generateGraphqlApiTypes();
2121
- });
2122
- import_commander.program.command("generate-graphql-client-types").description("Generate Graphql client types").action(async () => {
2123
- await generateGraphqlClientTypes();
2124
- });
2125
2136
  import_commander.program.command("generate-migration [<name>] [<date>]").description("Generate Migration").action(async (name2, date) => {
2126
2137
  const git = (0, import_simple_git.simpleGit)();
2127
2138
  if (!name2) {