Git Product home page Git Product logo

Comments (4)

hayes avatar hayes commented on September 3, 2024

Not sure whats going on here, can you share the generated GraphQL schema.

Part of the issue with types is that the prisma utils plugin claims to return the full create input with the required relation properties, but the validation schema can't validate that (since they aren't actually part of the input). Working around this would require significantly better type inference for the prisma input fields, which isn't likely to be added soon.

I am not clear on whats happening with the runtime error.

Is the issue that the input is being converted from an array to an object, or is the prisma error just rendering it that way?

the input plugin just ignores it, expecting a keyless object :)

I don't know what this means, what is the input plugin, and where is it "expecting" and keyless object?

having the GraphQL schema output, and the JSON version of the input args received in the resolver might help clarify what the issue is here

from pothos.

SpeedySH avatar SpeedySH commented on September 3, 2024

UP

The problem is partially solved, and it was my mistake.

  1. Changing type from array to object [SOLVED]
  2. You can send an object instead of an array and it will not be considered an error. Although it is specifically described in inputType that only an array is expected.
  3. It is impossible to validate such an input without an error using zod, because it writes that wrong fields/incorrect types .

As I understand point 3 is difficult to fix now, because of the complexity of type casting.

from pothos.

hayes avatar hayes commented on September 3, 2024

Changing type from array to object [SOLVED]

Glad you got this figured out

You can send an object instead of an array and it will not be considered an error. Although it is specifically described in inputType that only an array is expected.

If I am understanding this correctly, I think it might be a GraphQL issue, GraphQL automatically allows list inputs to be passed as an object, which it will treat as an list with 1 item, but it also shouldn't cause any issues

It is impossible to validate such an input without an error using zod, because it writes that wrong fields/incorrect types .

I think impossible might be an exaggeration here, I think if you do validate: { schema: { ... } as {} } it will avoid the type errors, but getting complete type-safety would be challenging.

from pothos.

hayes avatar hayes commented on September 3, 2024

createMany is tricky to use correctly. I think you are better of using create which can create multiple relations:

const CreateUserInput = builder.prismaCreate('User', {
  fields: () => ({
    email: 'String',
    name: 'String',
    posts: CreateUserPostsInput,
  }),
});

const CreateUserPostsInput = builder.prismaCreateRelation('User', 'posts', {
  fields: () => ({
    create: CreateUserPostInput,
  }),
});

const CreateUserPostInput = builder.prismaCreate('Post', {
  name: 'CreateUserPostsInput',
  fields: () => ({
    title: 'String',
  }),
});

builder.mutationType({
  fields: (t) => ({
    createUser: t.prismaField({
      type: 'User',
      args: {
        data: t.arg({ type: CreateUserInput, required: true }),
      },
      resolve: (query, _, args) => prisma.user.create({ ...query, data: args.data }),
    }),
  }
})
mutation {
  createUser(data: { 
    name: "test"
    email: "[email protected]"
    posts: {
      create: [{
        title: "post 1"
      }, {
        title: "post 2"
      }]
    }
  }) {
    id
    posts {
      title
    }
  }
}

from pothos.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.