Skip to content

Commit

Permalink
feat: pothos prisma dialect
Browse files Browse the repository at this point in the history
  • Loading branch information
jaymaycry authored and macrozone committed Feb 7, 2024
1 parent 8560731 commit 2e0607c
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/dataprovider/.prisma/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 33 additions & 7 deletions packages/dataprovider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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)}`,
},
}
},
};
```
Expand All @@ -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" // 👈
})
```
15 changes: 15 additions & 0 deletions packages/dataprovider/src/buildGqlQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 7 additions & 2 deletions packages/dataprovider/src/buildVariables/buildOrderBy.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -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];
};
45 changes: 45 additions & 0 deletions packages/dataprovider/src/buildVariables/buildVariables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
//

Expand Down
2 changes: 1 addition & 1 deletion packages/dataprovider/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export type MutationOperationNames = Partial<
Record<QueryDialect, MutationOperationNameMap>
>;

export type QueryDialect = "nexus-prisma" | "typegraphql";
export type QueryDialect = "nexus-prisma" | "typegraphql" | "pothos-prisma";

type Filter = Record<string, unknown>;

Expand Down
14 changes: 14 additions & 0 deletions packages/dataprovider/src/utils/makeIntrospectionOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`),
Expand All @@ -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);
Expand Down

0 comments on commit 2e0607c

Please sign in to comment.