Hi! Does anyone know if it is possible to have an ...
# orm-help
t
Hi! Does anyone know if it is possible to have an optional
connect
field on a mutation? I have a datamodel with "Items" that can optionally be contained in "Lists":
datamodel.prisma
:
Copy code
type Item {
 id: ID!
 name: String
 lists: [List]
}

type List {
 id: ID!
 name: String
 items: [Item]
}
with the following frontend `schema.graphql`:
Copy code
# import * from './generated/prisma.graphql'
type Mutation {
  createItem(name: String, lists: ListCreateManyWithoutItemsInput): Item!
}
I set up a mutation as follows in the react component: `NewItem.js`:
Copy code
const CREATE_ITEM = gql`
  mutation CREATE_ITEM(
    $name: String
    $listID: ID
  ) {
    createItem(
      name: $name
      lists: { connect: { id: $listID } } 
    ) {
      id
    }
  }
`;
I would expect that I don't have to pass a
listID
but when I try to use that mutation with only a name param I get the following error which makes me think it always expects me to pass a listID?
Copy code
[GraphQL error]: Message: You provided an invalid argument for the where selector on List. Please provide exactly one unique field and value., Location: , Path: createItem
r
Hey @Troy 👋 Your schema looks fine. Could you try using this signature as given by the Prisma server Playground
Copy code
createItem(
data: ItemCreateInput!
): Item!
You will find this in the docs in the Playground.
t
Thanks! Ok yeah I had the frontend graphql server set up like this because I am passing the user connection separately. Here is what my Mutation implementation looks like:
Copy code
const Mutation = {
  async createItem(parent, args, ctx, info) {
    if (!ctx.request.userId) {
      throw new Error("You must be logged in");
    }
    const item = await ctx.db.mutation.createItem(
      {
        data: {
          user: {
            connect: {
              id: ctx.request.userId,
            },
          },
          ...args,
        },
      },
      info
    );
    return item;
  },
  ...
}
I omitted the User relationship just for simplicity of the problem statement but it looks relevant. So I will mess around with this a bit and see if I can find a solution. The way I currently have it set up with the User connection passing separately I think means I need to read and pass the userId from the react component using AsyncStorage. Is reading and passing the UserID in the component like that secure?
👍 1
r
Yes it's fine to read from AsyncStorage.
t
cool cool. Thank you for your help @Ryan !
💯 1