Troy
07/07/2020, 9:14 PMconnect field on a mutation? I have a datamodel with "Items" that can optionally be contained in "Lists":
datamodel.prisma :
type Item {
id: ID!
name: String
lists: [List]
}
type List {
id: ID!
name: String
items: [Item]
}
with the following frontend `schema.graphql`:
# 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`:
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?
[GraphQL error]: Message: You provided an invalid argument for the where selector on List. Please provide exactly one unique field and value., Location: , Path: createItemRyan
07/08/2020, 8:00 AMcreateItem(
data: ItemCreateInput!
): Item!
You will find this in the docs in the Playground.Troy
07/08/2020, 10:00 AMconst 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?Ryan
07/08/2020, 10:05 AMTroy
07/08/2020, 10:08 AM