In the generated prisma.grapql, `RecipeCreateinstr...
# orm-help
d
In the generated prisma.grapql,
RecipeCreateinstructionsInput
(mentioned in the first error) is:
Copy code
input RecipeCreateinstructionsInput {
  set: [String!]
}
n
you are missing the
set
part in your mutation.
instructions: [ 'Step 1', 'Step 2' ],
needs to be
instructions: { set: [ 'Step 1', 'Step 2' ] },
d
Hi, @nilan. Thanks for the reply. I’ve tried that but it says it’s expecting type “String”. schema.graphql
Copy code
# import Category from './generated/prisma.graphql'
# import Recipe from './generated/prisma.graphql'
# import User from './generated/prisma.graphql'

enum Categories {
  BREAKFAST
  LUNCH
  DINNER
  SNACK
}

input RecipeInput {
  name: String!
  ingredients: [IngredientInput!]!
  instructions: [String!]!
  categories: [CategoryInput!]!
}

input IngredientInput {
  name: String!
  quantity: String!
}

input CategoryInput {
  name: Categories
}

type Query {
  recipes: [Recipe!]!
  categories: [Category!]!
  user(id: ID!): User
}

type Mutation {
  createUser(name: String!, email: String, password: String!): Recipe
  createCategory(name: Categories!): Category
  deleteCategory(categoryId: ID!): Category
  createRecipe(
    name: String!
    ingredients: [IngredientInput!]!
    instructions: [String!]!
    categories: [CategoryInput!]!
  ): Recipe
}
index.js (mutation)
Copy code
createRecipe: (_, args, context, info) => {
      return context.prisma.mutation.createRecipe(
        {
          data: {
            name: args.name,
            ingredients: args.ingredients,
            instructions: args.instructions,
            categories: args.categories
          }
        },
        info
      );
    }
I’m totally new to Prisma and GraphQL so I’m just playing around, trying to get my head around it with a real-use case.
n
Please share the exact error message.
d
datamodel.graphql
Copy code
type User @model {
  id: ID! @unique
  name: String!
  email: String! @unique
  password: String
  recipes: [Recipe!]!
  createdAt: DateTime!
  updatedAt: DateTime!
}

type Recipe @model {
  id: ID! @unique
  name: String! @unique
  ingredients: [Ingredient!]!
  instructions: [String!]!
  categories: [Category!]! @relation(name: "CategoryOnRecipe")
}

type Category @model {
  id: ID! @unique
  name: Categories! @unique
  recipes: [Recipe!]! @relation(name: "CategoryOnRecipe")
}

type Ingredient {
  name: String!
  quantity: String!
}

enum Categories {
  BREAKFAST
  LUNCH
  DINNER
  SNACK
}
Sure:
Copy code
Error: Variable "$_v0_data" got invalid value {"name":"New Recipe","ingredients":[{"name":"Ingredient 1","quantity":"1"},{"name":"Ingredient 2","quantity":"2"}],"instructions":["Step 1","Step 2"],"categories":[{"name":"SNACK"},{"name":"DINNER"}]}; Field "0" is not defined by type RecipeCreateinstructionsInput at value.instructions.
Variable "$_v0_data" got invalid value {"name":"New Recipe","ingredients":[{"name":"Ingredient 1","quantity":"1"},{"name":"Ingredient 2","quantity":"2"}],"instructions":["Step 1","Step 2"],"categories":[{"name":"SNACK"},{"name":"DINNER"}]}; Field "1" is not defined by type RecipeCreateinstructionsInput at value.instructions.
Variable "$_v0_data" got invalid value {"name":"New Recipe","ingredients":[{"name":"Ingredient 1","quantity":"1"},{"name":"Ingredient 2","quantity":"2"}],"instructions":["Step 1","Step 2"],"categories":[{"name":"SNACK"},{"name":"DINNER"}]}; Field "0" is not defined by type IngredientCreateManyInput at value.ingredients.
Variable "$_v0_data" got invalid value {"name":"New Recipe","ingredients":[{"name":"Ingredient 1","quantity":"1"},{"name":"Ingredient 2","quantity":"2"}],"instructions":["Step 1","Step 2"],"categories":[{"name":"SNACK"},{"name":"DINNER"}]}; Field "1" is not defined by type IngredientCreateManyInput at value.ingredients.
Variable "$_v0_data" got invalid value {"name":"New Recipe","ingredients":[{"name":"Ingredient 1","quantity":"1"},{"name":"Ingredient 2","quantity":"2"}],"instructions":["Step 1","Step 2"],"categories":[{"name":"SNACK"},{"name":"DINNER"}]}; Field "0" is not defined by type CategoryCreateManyWithoutRecipesInput at value.categories.
Variable "$_v0_data" got invalid value {"name":"New Recipe","ingredients":[{"name":"Ingredient 1","quantity":"1"},{"name":"Ingredient 2","quantity":"2"}],"instructions":["Step 1","Step 2"],"categories":[{"name":"SNACK"},{"name":"DINNER"}]}; Field "1" is not defined by type CategoryCreateManyWithoutRecipesInput at value.categories.
    at new CombinedError (/Users/Darryl/Projects/server/node_modules/graphql-tools/dist/stitching/errors.js:83:28)
    at Object.checkResultAndHandleErrors (/Users/Darryl/Projects/server/node_modules/graphql-tools/dist/stitching/errors.js:101:15)
    at CheckResultAndHandleErrors.transformResult (/Users/Darryl/Projects/server/node_modules/graphql-tools/dist/transforms/CheckResultAndHandleErrors.js:10:25)
    at /Users/Darryl/Projects/server/node_modules/graphql-tools/dist/transforms/transforms.js:19:54
    at Array.reduce (<anonymous>)
    at applyResultTransforms (/Users/Darryl/Projects/server/node_modules/graphql-tools/dist/transforms/transforms.js:18:23)
    at /Users/Darryl/Projects/server/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:82:50
    at step (/Users/Darryl/Projects/server/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:32:23)
    at Object.next (/Users/Darryl/Projects/server/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:13:53)
    at fulfilled (/Users/Darryl/Projects/server/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:4:58)
n
I don't see the message that String is expected.
anyway, please use the Playground against your API to figure out the correct syntax
instead of
"categories":[{"name":"SNACK"},{"name":"DINNER"}]
you need
"categories":[create: {"name":"SNACK"},{"name":"DINNER"}]}
or something like that
d
Sorry, that error was before I changed to using set. When using set in the playground, though, it’s underlined and says it expects type String for “instructions”.
Copy code
mutation CreateRecipe {
  createRecipe(
    name: "New Recipe"
    instructions: { set: [ "Step 1", "Step 2" ] }
    ingredients: [
      {
        name: "Ingredient 1"
        quantity: "1"
      }
      {
        name: "Ingredient 2"
        quantity: "2"
      }
    ]
    categories: [
      {
        name: SNACK
      }
      {
        name: DINNER
      }
    ]
  ) {
    id
    name
  }
}
The { set … } is all underlined.
n
can you share your endpoint?
d
It’s running locally in docker.
n
The abstract error description is that you are not following the expected syntax. the best way to figure it out is to use autocompletion in your Playground.
d
I’ve tried that and. When I type instructions in the mutation (in the playground) the tooltip shows [String!].
n
ok, I see what is happening
you redefined the mutation structure for your application schema to accept a list of strings
however, when implementing it using prisma-bindings, you still need to use
set
It wasn't clear to me that you are talking about the application schema, and not the Prisma API.
d
Oh, I’m sorry.
The Prisma API works perfectly.
n
In general, this is a very inefficient way of discussing this topic 🙂 It would be easier to talk about this in the Forum, where all the context is preserved in one big conversation. Anyway, this part is wrong:
Copy code
createRecipe: (_, args, context, info) => {
      return context.prisma.mutation.createRecipe(
        {
          data: {
            name: args.name,
            ingredients: args.ingredients,
            instructions: args.instructions,
            categories: args.categories
          }
        },
        info
      );
    }
it needs to say
instructions: set: { args.instructions },
for sure, and I'm not sure about
ingredients
and
categories
.
d
Ah, I see.
Thank you! I’ll have a play around with that. Also, in future, I’ll create forum posts.
💯 1
n
awesome! I always start with the Prisma API, when that works I think about the API I want to expose in
schema.graphql
, now the resolver itself is the missing link which takes care of the mapping
in this case the mapping logic is wrong - and the error message is indeed not helpful at all. this is a thing in
graphql-tools
and I'm not sure if this is already being tracked somewhere. It might be worth to raise this problem here: https://github.com/apollographql/graphql-tools/.
👍 1
d
The suggestion you gave doesn’t seem right unfortunately. Replacing instructions: args.instructions with what you said, I mean.
Ah, is it meant to be:
Copy code
...
set: { instructions: args.instructions },
...
At least that way the editor doesn’t complain.
n
try it out in the Playground for the Prisma API.
👍 1
if you share your project in a repo to be run locally, someone can also take a "live" look - this might be easier than back and forth here 🙂
d
Thanks. I’ll do that.
Added to a temp public repo here: https://gitlab.com/darrylyoung/server Thank you!
n
Copy code
mutation a {
  createRecipe(data: {
    name: "Cake"
    instructions: {
      set: ["Step 1", "Step 2"]
    },
    ingredients: {
      create: [{
        name: "Butter"
        quantity: "two"
      }, {
        name: "Sugar"
        quantity: "twelve"
      }]
    }
    categories: {
      create: [{
        name: BREAKFAST
      }, {
        name: LUNCH
      }]
    }
  }) {
    id
  }
}
this is a valid mutation against your Prisma API.
d
Cool. Yeah, I have that working on the Prisma API.
The only difference was that I was using connect in categories as I’ve already created the main categories. Other than that, it’s the same.
But, as you know, it’s when creating a recipe from the application schema I have the issue.
n
Copy code
createRecipe: (_, args, context, info) => {
  return context.prisma.mutation.createRecipe(
    {
      data: {
        name: args.name,
        instructions: {
          set: args.instructions
        },
        ingredients: {
          create: [...args.ingredients]
        },
        categories: {
          connect: [...args.categories]
        }
      }
    },
    info
  );
}
this is the resolver you need.
🦜 1
d
Thank you so much, @nilan. I just missed spreading the ingredients and categories. 🙄
I really appreciate the support.
If I ever see you at a Berlin meetup I’ll get you a beer. 😉
n
have you tried that it works? I've written it from the top off my head 🙈
it would be awesome if you can push the feedback about the error message to
graphql-tools
, it should be much more helpful
d
I did, yeah. It works. 😄
Sure, I’ll give the feedback.
n
I'm frequenting GraphQL Berlin, would love to meet you in person: https://www.meetup.com/de-DE/graphql-berlin/ 🙂
d
That’d be great. Yeah, I’ll join that meetup. It was only recently I saw that Prisma was a Berlin (and SF) company. I’m also in Berlin, working at Zalando.
👍 1
Anyway, thanks again, Nilan. I really appreciate it.
n
you're very welcome 🙌
❤️ 1