Next.js Discord

Discord Forum

Error using prisma with pothos in graphql api

Answered
coder2000 posted this in #help-forum
Open in Discord
I have the following code:

builder.mutationField("createOrganization", (t) =>
  t.prismaField({
    type: "Organization",
    args: {
      name: t.arg.string({ required: true }),
      userId: t.arg.id({ required: true }),
    },
    resolve: async (query, root, args) => {
      return prisma.organization.create({
        ...query,
        data: {
          name: args.name,
          users: {
            connect: {
              id: args.userId,
            },
          },
        },
      });
    },
  }),
);

import { prisma } from "@/db";
import SchemaBuilder from "@pothos/core";
import PrismaPlugin from "@pothos/plugin-prisma";
import type PrismaTypes from "@pothos/plugin-prisma/generated";
import { getDatamodel } from "@pothos/plugin-prisma/generated";

export const builder = new SchemaBuilder<{
  PrismaTypes: PrismaTypes;
}>({
  plugins: [PrismaPlugin],
  prisma: {
    client: prisma,
    filterConnectionTotalCount: true,
    onUnusedQuery: process.env.NODE_ENV === "production" ? null : "warn",
    dmmf: getDatamodel(),
  },
});

builder.queryType({});
builder.mutationType({});


When I try to execute the mutation I get this error:

ERR GraphQLError: PrismaClient is unable to run in this browser environment, or has been bundled for the browser (running in ``).
If this is unexpected, please open an issue: https://pris.ly/prisma-prisma-bug-report
    at resolve (src/graphql/schema/organization.ts:28:17)
    at resolve (../src/field-builder.ts:56:8)
  26 |          },
  27 |          resolve: async (query, root, args) => {
> 28 |                  return prisma.organization.create({
     |                               ^
  29 |                          ...query,
  30 |                          data: {
  31 |                                  name: args.name, {
  path: [ 'a6d2e9' ],
  locations: [ { line: 1, column: 38 } ],
  extensions: [Object: null prototype] {}
}
 POST /api/graphql 200 in 139ms
Error [GQtyError]: Unexpected error.
    at async queryFetcher (src/graphql/client.ts:36:8)
  34 |  });
  35 |
> 36 |  return await defaultResponseHandler(response);
     |        ^
  37 | };
  38 |
  39 | const cache = new Cache( {
  graphQLErrors: [Array],
  otherError: undefined
}


Any suggestions on what to look at?
Answered by coder2000
I resolved the issue myself. Apparently I was trying to run server code in the browser. My bad.
View full answer

1 Reply

I resolved the issue myself. Apparently I was trying to run server code in the browser. My bad.
Answer