From 7df39daeeffe74054437b98bf98b784029439ffd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jay=20P=C3=A9clard?= Date: Wed, 7 Feb 2024 16:40:17 +0100 Subject: [PATCH] feat: pothos prisma dialect --- packages/dataprovider/.prisma/index.js | 2 +- packages/dataprovider/README.md | 40 ++++++++++++++--- packages/dataprovider/src/buildGqlQuery.ts | 15 +++++++ .../src/buildVariables/buildOrderBy.ts | 9 +++- .../src/buildVariables/buildVariables.test.ts | 45 +++++++++++++++++++ packages/dataprovider/src/types.ts | 2 +- .../src/utils/makeIntrospectionOptions.ts | 14 ++++++ 7 files changed, 116 insertions(+), 11 deletions(-) diff --git a/packages/dataprovider/.prisma/index.js b/packages/dataprovider/.prisma/index.js index 37d8b8f..4a5a96b 100644 --- a/packages/dataprovider/.prisma/index.js +++ b/packages/dataprovider/.prisma/index.js @@ -209,7 +209,7 @@ const config = { "value": "prisma-client-js" }, "output": { - "value": "/Users/macrozone/git/panter/ra-data-prisma/packages/dataprovider/.prisma", + "value": "/Users/jay/Work/Panter/Projekte/ra-data-prisma/packages/dataprovider/.prisma", "fromEnvVar": null }, "config": { diff --git a/packages/dataprovider/README.md b/packages/dataprovider/README.md index 45db7c2..9c1c266 100644 --- a/packages/dataprovider/README.md +++ b/packages/dataprovider/README.md @@ -552,7 +552,15 @@ const dataProvider = useDataProvider({ You can override operation names depending on the version of typegraphql-prisma you are using: ```ts -import { CREATE, UPDATE, DELETE, GET_LIST, GET_MANY, GET_MANY_REFERENCE, GET_ONE } from "react-admin"; +import { + CREATE, + UPDATE, + DELETE, + GET_LIST, + GET_MANY, + GET_MANY_REFERENCE, + GET_ONE, +} from "react-admin"; import { Options } from "@ra-data-prisma/dataprovider"; import camelCase from "lodash/camelCase"; import pluralize from "pluralize"; @@ -570,10 +578,11 @@ const options: Options = { typegraphql: { [GET_LIST]: (resource) => `${pluralize(camelCase(resource.name))}`, [GET_MANY]: (resource) => `${pluralize(camelCase(resource.name))}`, - [GET_MANY_REFERENCE]: (resource) => `${pluralize(camelCase(resource.name))}`, + [GET_MANY_REFERENCE]: (resource) => + `${pluralize(camelCase(resource.name))}`, [GET_ONE]: (resource) => `${camelCase(resource.name)}`, }, - } + }, }; ``` @@ -599,11 +608,28 @@ const options: Options = { }, queryOperationNames: { typegraphql: { - [GET_LIST]: (resource) => prefix(`${pluralize(camelCase(resource.name))}`), - [GET_MANY]: (resource) => prefix(`${pluralize(camelCase(resource.name))}`), - [GET_MANY_REFERENCE]: (resource) => prefix(`${pluralize(camelCase(resource.name))}`), + [GET_LIST]: (resource) => + prefix(`${pluralize(camelCase(resource.name))}`), + [GET_MANY]: (resource) => + prefix(`${pluralize(camelCase(resource.name))}`), + [GET_MANY_REFERENCE]: (resource) => + prefix(`${pluralize(camelCase(resource.name))}`), [GET_ONE]: (resource) => prefix(`${camelCase(resource.name)}`), }, - } + }, }; ``` + +## Usage with pothos-prisma + +(beta) + +You can use the dataprovider to connect to a backend that was created using https://www.npmjs.com/package/@pothos/plugin-prisma-utils. +It has slightly different OrderBy values. Pass the following option to the dataprovider: + +```ts +const dataProvider = useDataProvider({ + clientOptions: { uri: "/graphql" } + queryDialect: "pothos-prisma" // 👈 +}) +``` diff --git a/packages/dataprovider/src/buildGqlQuery.ts b/packages/dataprovider/src/buildGqlQuery.ts index 0364073..60741b9 100644 --- a/packages/dataprovider/src/buildGqlQuery.ts +++ b/packages/dataprovider/src/buildGqlQuery.ts @@ -302,6 +302,21 @@ export default ( arguments: supportsCountArgs ? countArgs : undefined, }); }, + "pothos-prisma": (): FieldNode => { + const countName = `${queryType.name}Count`; + + const supportsCountArgs = + ( + introspectionResults.queries.find( + (query) => query.name === countName, + ) as any + )?.args.length > 0; + + return gqlTypes.field(gqlTypes.name(countName), { + alias: gqlTypes.name("total"), + arguments: supportsCountArgs ? countArgs : undefined, + }); + }, typegraphql: (): FieldNode => { const resourceName = upperFirst(resource.type.name); diff --git a/packages/dataprovider/src/buildVariables/buildOrderBy.ts b/packages/dataprovider/src/buildVariables/buildOrderBy.ts index 4520a24..67ecb08 100644 --- a/packages/dataprovider/src/buildVariables/buildOrderBy.ts +++ b/packages/dataprovider/src/buildVariables/buildOrderBy.ts @@ -1,7 +1,6 @@ import { IntrospectionInputObjectType } from "graphql"; import set from "lodash/set"; import type { GetListParams } from "."; -import { IntrospectionResult, Resource } from "../constants/interfaces"; import { BuildVariablesContext } from "./types"; function getOrderType({ @@ -33,6 +32,12 @@ export const buildOrderBy = ( return null; } const selector = {}; - set(selector, field, sort.order === "ASC" ? "asc" : "desc"); + + if (context.options.queryDialect === "pothos-prisma") { + set(selector, field, sort.order === "ASC" ? "Asc" : "Desc"); + } else { + set(selector, field, sort.order === "ASC" ? "asc" : "desc"); + } + return [selector]; }; diff --git a/packages/dataprovider/src/buildVariables/buildVariables.test.ts b/packages/dataprovider/src/buildVariables/buildVariables.test.ts index f14b19b..356d808 100644 --- a/packages/dataprovider/src/buildVariables/buildVariables.test.ts +++ b/packages/dataprovider/src/buildVariables/buildVariables.test.ts @@ -88,6 +88,51 @@ describe("buildVariables", () => { }); }); + it("returns first, where, skip and orderBy for pothos-prisma", async () => { + // + + const params = { + pagination: { page: 10, perPage: 10 }, + sort: { field: "email", order: "ASC" }, + filter: { + yearOfBirth: 1879, + firstName: "fooBar", + }, + }; + const result = buildVariables({ + ...userContext, + options: { + ...userContext.options, + queryDialect: "pothos-prisma", + }, + })(GET_LIST, params); + + expect(result).toEqual({ + where: { + AND: [ + { + yearOfBirth: { + equals: 1879, + }, + }, + { + firstName: { + contains: "fooBar", + mode: "insensitive", + }, + }, + ], + }, + take: 10, + orderBy: [ + { + email: "Asc", + }, + ], + skip: 90, + }); + }); + it("allows sorting by direct properties", async () => { // diff --git a/packages/dataprovider/src/types.ts b/packages/dataprovider/src/types.ts index 76f7f41..c1d009b 100644 --- a/packages/dataprovider/src/types.ts +++ b/packages/dataprovider/src/types.ts @@ -65,7 +65,7 @@ export type MutationOperationNames = Partial< Record >; -export type QueryDialect = "nexus-prisma" | "typegraphql"; +export type QueryDialect = "nexus-prisma" | "typegraphql" | "pothos-prisma"; type Filter = Record; diff --git a/packages/dataprovider/src/utils/makeIntrospectionOptions.ts b/packages/dataprovider/src/utils/makeIntrospectionOptions.ts index 083745e..f15e377 100644 --- a/packages/dataprovider/src/utils/makeIntrospectionOptions.ts +++ b/packages/dataprovider/src/utils/makeIntrospectionOptions.ts @@ -22,6 +22,11 @@ export const makeIntrospectionOptions = (options: OurOptions) => { [UPDATE]: (resource: Resource) => prefix(`updateOne${resource.name}`), [DELETE]: (resource: Resource) => prefix(`deleteOne${resource.name}`), }, + "pothos-prisma": { + [CREATE]: (resource: Resource) => prefix(`createOne${resource.name}`), + [UPDATE]: (resource: Resource) => prefix(`updateOne${resource.name}`), + [DELETE]: (resource: Resource) => prefix(`deleteOne${resource.name}`), + }, typegraphql: { [CREATE]: (resource: Resource) => prefix(`create${resource.name}`), [UPDATE]: (resource: Resource) => prefix(`update${resource.name}`), @@ -40,6 +45,15 @@ export const makeIntrospectionOptions = (options: OurOptions) => { [GET_MANY_REFERENCE]: (resource: Resource) => prefix(`${pluralize(camelCase(resource.name))}`), }, + "pothos-prisma": { + [GET_LIST]: (resource: Resource) => + prefix(`${pluralize(camelCase(resource.name))}`), + [GET_ONE]: (resource: Resource) => prefix(`${camelCase(resource.name)}`), + [GET_MANY]: (resource: Resource) => + prefix(`${pluralize(camelCase(resource.name))}`), + [GET_MANY_REFERENCE]: (resource: Resource) => + prefix(`${pluralize(camelCase(resource.name))}`), + }, typegraphql: { [GET_LIST]: (resource: Resource) => { const pluralName = pluralize(resource.name);