Nexus-plugin-prisma: Add custom query/mutation custom work

Created on 18 Feb 2019  路  4Comments  路  Source: graphql-nexus/nexus-plugin-prisma

Is it possible to add a custom query/mutation without any relationship with datamodelInfo? I mean, I would like to add a query to can be executed through playground but only to do some internal work, generate, re-deploy, executing npm scripts basically..

Like this, where "GenerateInvoices" doesn't exists as an object in its schema, but i would like to publish it and in its resolver, do internal work.

image

Any idea?

Thanks!

Most helpful comment

Here is how I dit it, declaring a new objectType and passing as type in a custom mutation.

````

// Query root node
const Query = prismaObjectType({
name: 'Query',
definition: t => t.prismaFields(['*'])
});

// Custom objectType
const GeneratedInvoices= objectType({
name: "GeneratedInvoices",
definition: (t) => {
t.boolean("success", { description: "Indicates if process of invoice generation has been executed successfully" });
},
});

// Mutation root node
const Mutation = prismaObjectType({
name: 'Mutation',
definition(t) {
// Keep all the mutations
t.prismaFields(['*'])
t.field('GenerateInvoices', {
type: 'GeneratedInvoices',
args: {
startDate: stringArg(),
endDate: stringArg({ nullable: true })
},
resolve: (parent, { startDate, endDate }, ctx) => {
console.log(startDate, endDate);
return { success: true };
}
})
}
})

const schema = makePrismaSchema({
types: [Query, Mutation, GeneratedInvoices],
````

I share it if can be helpful to someone.

P.D: By the way, I get this warning..

Nexus Warning: Interface Node is not implemented by any object types

Does anyone know why it's generating this warning?

On the other hand, when I execute

npx nexus-prisma-generate --client ./generated/prisma-client --output ./generated/nexus-prisma

I get

Path must be a string. Received undefined npx: installed 85 in 11.689s EEXIST: file already exists, mkdir 'D:\test-prisma-nexus\generated\nexus-prisma'
Many thanks!

All 4 comments

Yes, it is absolutely possible to generate "virtual" fields which bear no relation to your Prisma schema.

You will want to forgo the use of prismaObjectType which requires a type which _does_ exist in your Prisma schema (i.e. a not-virtual field) to back it. Instead, substitute the base Nexus objectType function for prismaObjectType because plain objectType does not have the Prisma-object-backing requirement.

For example:

const GeneratedInvoice = objectType({
  name: "GeneratedInvoice",
  definition(t) {
    // virtual fields for this virtual type go here
  },
});

This is exactly how this is supposed to be done! This helps clearly separating the types from your prisma api and the custom types. Also, if we allowed prismaObjectType to accept any type name, we'd loose the autocompletion for the type names

Here is how I dit it, declaring a new objectType and passing as type in a custom mutation.

````

// Query root node
const Query = prismaObjectType({
name: 'Query',
definition: t => t.prismaFields(['*'])
});

// Custom objectType
const GeneratedInvoices= objectType({
name: "GeneratedInvoices",
definition: (t) => {
t.boolean("success", { description: "Indicates if process of invoice generation has been executed successfully" });
},
});

// Mutation root node
const Mutation = prismaObjectType({
name: 'Mutation',
definition(t) {
// Keep all the mutations
t.prismaFields(['*'])
t.field('GenerateInvoices', {
type: 'GeneratedInvoices',
args: {
startDate: stringArg(),
endDate: stringArg({ nullable: true })
},
resolve: (parent, { startDate, endDate }, ctx) => {
console.log(startDate, endDate);
return { success: true };
}
})
}
})

const schema = makePrismaSchema({
types: [Query, Mutation, GeneratedInvoices],
````

I share it if can be helpful to someone.

P.D: By the way, I get this warning..

Nexus Warning: Interface Node is not implemented by any object types

Does anyone know why it's generating this warning?

On the other hand, when I execute

npx nexus-prisma-generate --client ./generated/prisma-client --output ./generated/nexus-prisma

I get

Path must be a string. Received undefined npx: installed 85 in 11.689s EEXIST: file already exists, mkdir 'D:\test-prisma-nexus\generated\nexus-prisma'
Many thanks!

Hey,

1)
The Nexus Warning: Interface Node is not implemented by any object types warning is because the generated schema doesn't implement the Node interface on the object types.
We'll address this soon !

2)

Path must be a string. Received undefined
npx: installed 85 in 11.689s
EEXIST: file already exists, mkdir 'D:\test-prisma-nexus\generated\nexus-prisma'

This is probably a duplicated of #87

In the future, please try to create several issues for different problems so that we can easily track everything.

Closing now as the main topic was solved 馃檪

Was this page helpful?
0 / 5 - 0 ratings

Related issues

narciero picture narciero  路  3Comments

franjohn21 picture franjohn21  路  4Comments

jasonkuhrt picture jasonkuhrt  路  4Comments

outerlook picture outerlook  路  5Comments

colinhacks picture colinhacks  路  5Comments