graphql-nexus / nexus-plugin-prisma

Deprecated
MIT License
828 stars 118 forks source link

[Question] I'm not able to add a second custom mutation #183

Closed marticrespi closed 5 years ago

marticrespi commented 5 years ago

Well, I have a question and I don't know why it doesn't work..

Two weeks ago, I added a custom mutation using nexus adding a new custom type

update-schema.ts

import { objectType } from 'nexus';

export const UpdatedSchema = objectType({
    name: "UpdatedSchema",
    definition: (t) => {
      t.boolean("success", { description: "Indicates if update operation has been executed successfully" });
      t.string("error", { description: "Return an error if updated doesn't finish successfully", nullable: true });
    },
  });

objectTypes/index.ts

import { UpdatedSchema } from './update-schema';

export const objectTypes = {
    UpdatedSchema: UpdatedSchema
};

And then in my src/index.ts

const Mutation = prismaObjectType({ 
    name: 'Mutation',
    definition(t) {
      // Keep all the mutations
      t.prismaFields(['*']);
      // Add a custom mutation
      t.field('updateSchema', {
        type: 'UpdatedSchema',
        args: argsTypes.environmentArgs,
        resolve: (parent, { environment }, ctx) => {
         return null; 
        }
      })
    }
  });

  const schema = makePrismaSchema({
    types: [Query, Mutation, objectTypes, prismaObjectTypes, enumTypes],
    // Configure the interface to Prisma
    prisma: {
      datamodelInfo,
      client: prisma,
    },
    outputs: {
      schema: path.join(__dirname, './generated/nexus/schema.graphql'),
      typegen: path.join(__dirname, './generated/nexus/nexus.ts'),
    },
  });

And it works perfectly. But now, I want to add a second custom mutation and I repeat the same process..

Add a new file generated-invoices.ts

import { objectType } from 'nexus';

export const GeneratedInvoices = objectType({
    name: "GeneratedInvoices",
    definition: (t) => {
      t.boolean("success", { description: "Indicates if all invoices has been generated successfully" });
      t.string("error", { description: "Return an error if generated invoices doesn't finish successfully", nullable: true });
    },
  });

Modify my objectTypes/index.ts adding this new objectType

import { GeneratedInvoices } from './generated-invoices';
import { UpdatedSchema } from './update-schema';

export const objectTypes = {
    UpdatedSchema: UpdatedSchema,
    GeneratedInvoices: GeneratedInvoices
};

But when i try to add a new custom mutation, it doesn't recognize this new type.

  const Mutation = prismaObjectType({ 
    name: 'Mutation',
    definition(t) {
      // Keep all the mutations
      t.prismaFields(['*']);

      t.field('generateInvoices', {
        type: 'GeneratedInvoices',
        args: argsTypes.generateInvoicesArgs,
        resolve: (parent, { customerCode, contractCode, itemCode}, ctx) => {
            return null;
        }
      })
      // Add a custom mutation
      t.field('updateSchema', {
        type: 'UpdatedSchema',
        args: argsTypes.environmentArgs,
        resolve: (parent, { environment }, ctx) => {
           return null;
        }
      })
    }
  });

  const schema = makePrismaSchema({
    types: [Query, Mutation, objectTypes, prismaObjectTypes, enumTypes],
    // Configure the interface to Prisma
    prisma: {
      datamodelInfo,
      client: prisma,
    },
    outputs: {
      schema: path.join(__dirname, './generated/nexus/schema.graphql'),
      typegen: path.join(__dirname, './generated/nexus/nexus.ts'),
    },
  });

  const server = new GraphQLServer({
    schema,
    context: { prisma }
  });

image

What am I doing wrong? I have tried to generate prisma and nexus client but it doesn't recognize this new objectType as available, objectTypes is an object with two properties, I don't think that it was the problem because my prismaObjectTypes has the same structure and it works with all its properties hiding some object properties of each one.

export const prismaObjectTypes = {
    // Objects
    chargeTypeObject: chargeTypeObject,
    contractTypeObject : contractTypeObject,
    paymentMethodObject: paymentMethodObject,
    paymentStatusObject: paymentStatusObject,
    customerTypeObject: customerTypeObject,
    productTypeObject: productTypeObject,
    productSubTypeObject: productSubTypeObject,
};

Thanks!

Weakky commented 5 years ago

Hey there,

Does your type appear in the outputted schema ? Generally, this seems like you have just forgotten to re-run the server so that nexus generates the typings, but I may be wrong

marticrespi commented 5 years ago

Hi @Weakky , After above process I have tried:

prisma generate then rm -r ./generated/nexus-prisma npx nexus-prisma-generate --client ./generated/prisma-client --output ./generated/nexus-prisma.

After that, I haven't this type in any of these files

      schema: path.join(__dirname, './generated/nexus/schema.graphql'),
      typegen: path.join(__dirname, './generated/nexus/nexus.ts'),

But without adding the custom mutation, because if I have this part of code uncommented it doesn't compile when i try to serve.

My question is, if I don't add this custom mutation but the new type is declared and included in objectTypes const, it should be added although is not used?

Many thanks!

Weakky commented 5 years ago

AFAIK, nexus doesn't remove unused types, it should remain in the schema even though it's not used. I don't see the point of having a type that isn't used at all though.

prisma generate and nexus-prisma-generate have nothing to do with the type error you’re receiving anyway here. The missing type should be generated by nexus itself at runtime in the nexus.ts file. Therefore, in order to compile your server, you first need to run your server and let nexus generate the nexus.ts file, in which it should contain the type definition for that new type you're trying to add.

Only then you'll be able to compile

marticrespi commented 5 years ago

Well, I was trying to serve with node, and first of all, I transpile all my code to .js and then serving it with node. When I have a change like this, I have to serve first with ts-node and then nexus is able to create my missing type. Then, I can serve with node, transpiling first and serving after.

Strange..

marticrespi commented 5 years ago

Hi @Weakky is something about nexus or nexus-prisma can do something about? I'm asking to know if I have to close this issue or not.

Thanks!

Weakky commented 5 years ago

Hey @marticrespi,

https://github.com/prisma/yoga2 should solve that problem by making sure that your the codegen is regenerated before building your app. It's still WIP though.

Beisdes that, there's an issue here https://github.com/prisma/nexus/issues/77 to discuss about that topic

For now, there's not much we can do to solve that issue, it's fundamentally linked to how nexus works under the hood

marticrespi commented 5 years ago

Ok, thanks for the info. Closing.