Nexus: Use Prisma generated input type as arg

Created on 5 Mar 2019  路  2Comments  路  Source: graphql-nexus/nexus

I am trying to add my own custom mutation on top of the mutations generated by Prisma. As one of the args, I would like to use a prisma generated input type. Currently it seems that unless I create my own input type, I can only use scalars as args for my mutation? Is that correct? Still trying to get a hang of how nexus and prisma interact.

Currently, in our custom resolver definitions, we have:
createNavigationItems(data: [NavigationItemCreateInput!]!): [NavigationItem]!

How can I do the same thing with nexus?

const Mutation = prismaObjectType({ 
  name: 'Mutation',
  definition(t) { 
    t.prismaFields(['*'])

    t.list.field('createManyNavigationItems', {
      type: 'NavigationItem',
      args: {
        data: NavigationItemCreateInput[] // THIS IS WHERE I WANT TO USE THE PRISMA GENERATED INPUT TYPE BUT IT THROWS AN ERROR
      },
      resolve(root, args, ctx, info) {
        let createdNavigationItems: NavigationItem[] = [];

        for (let navItemInput of args.data) {
          let createdNavigationItem: NavigationItem = ctx.prisma.createNavigationItem(navItemInput);
          createdNavigationItems.push(createdNavigationItem);
        }

        return createdNavigationItems;
      }
    })
  }
});

Most helpful comment

Hey there,

Simply use the arg function to reference other InputObjectTypes 馃檪

eg:

args: {
  data: arg({ type: 'NavigationItemCreateInput', list: true })
}

You can also use t.prismaType by doing the following:

args: {
  data: t.prismaType.<field_of_the_type>.args.<the_arg_you_wanna_pick>
}

All 2 comments

Hey there,

Simply use the arg function to reference other InputObjectTypes 馃檪

eg:

args: {
  data: arg({ type: 'NavigationItemCreateInput', list: true })
}

You can also use t.prismaType by doing the following:

args: {
  data: t.prismaType.<field_of_the_type>.args.<the_arg_you_wanna_pick>
}

Great, thanks for the help. That worked perfectly. 馃憤

Was this page helpful?
0 / 5 - 0 ratings