Graphql: User Error: expected iterable, but did not find one for field XXX - laravel

This is movie schema:
type Book {
id: ID!
title: String
author: [Author] #belongsTo(relation: "author")
}
This is how I related book and author
public function author()
{
return $this->belongsTo('App\Models\Author', 'id', 'book_id');
}
This is schema for author
type Author {
id: ID!
title: String!
book_id: Int!
}
This is my query for Book:
extend type Query {
bookCriteria(
orderBy: _ #orderBy
where: _ #whereConditions
): [Book!]! #paginate
}
This is how I query:
{
bookCriteria
(
first: 1, page: 1
)
{
data
{
id
uuid
author
{
id
title
}
}
}
}
Finally, this is what I get as error message:
"User Error: expected iterable, but did not find one for field Book.author."
If I use hasMany instead of belongsTo, it works fine.
Please let me know what is wrong here?

Your type should be
type Book {
id: ID!
title: String
author: Author #belongsTo(relation: "author")
}

Related

Prisma 2 delete mutation is returning null

I am using ApolloServer/Prisma2/GraphQL/Typescript/MySQL
I have created two models, User, and Post. My createUser and createPost mutations are working fine. However, I am having trouble getting my delete mutations working. Focusing on the deletePost, here is what I have in my code.
<<schema.graphql>>
type Query {
posts: [Post!]!
users: [User!]!
}
type Mutation {
createPost(title: String!, body: String!): Post!
createUser(name: String!, email: String!, password: String!): User!
deletePost(postId: ID!): Post
deleteAllPosts: [Post!]!
}
type Post {
id: ID!
title: String!
body: String!
author: User
published: Boolean!
}
type User {
id: ID!
name: String!
email: String!
posts: [Post!]!
}
<<schema.prisma>>
model Post {
id Int #id #default(autoincrement())
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt #default(now())
title String
body String
published Boolean #default(false)
postedBy User? #relation(fields: [authorId], references: [id])
authorId Int?
}
model User {
id Int #id #default(autoincrement())
name String
email String #unique
password String
posts Post[]
}
<<Mutation.ts>>
async function deletePost(parent, { postId }, context, info) {
return await context.prisma.post.delete(
{
where {
id: parseInt(postId)
}
},
info
)
}
Note that the createPost and createUser are also in the Mutation.ts file and are working correctly. So I'm assuming there is no issue with the Apollo server.
When I use the GraphQL playground I use the following:
mutation {
deletePost(postId: "1") {
id
}
}
with the following result:
{
"data": {
"deletePost": null
}
}
I want the mutation to return the deleted post (at least the id if nothing else). In addition, the database is not deleting anything. I'd appreciate any help.

Entity Association syntax issue with graphql queries

I am trying to understand graphql's query and mutation syntax. Given this example:
type Author {
id: Int!
firstName: String
lastName: String
posts: [Post]
}
type Post {
id: Int!
title: String
author: Author
votes: Int
}
type Query {
posts: [Post]
author(id: Int!): Author
}
What should the queries look like to associate a post with the author? Is this where connections come into play or is that something else? Here is my attempt at trying to solve the problem but to avail.
mutation createAuthor {
createAuthor(input: {
id: 123
firstName: "Bob"
lastName: "Smith"
}) {
id
firstName
lastName
}
}
query listAuthors {
listAuthors {
items {
id
firstName
lastName
}
}
}
mutation createPost {
createPost(input: {
id: 12345
title: "Title"
votes: 345
author: {
lastName: {
contains: "Bob"
}
}
}) {
id
title
votes
author {
id
firstName
lastName
}
}
}
Any help on this would be appreciated. My Goal is to Query an author and return all of the post's associated with that author as well as create a Post Mutation that adds a post to an author.
There are 2 questions in one, so I'll answer in the order there were asked.
1. return all of the posts associated with an author
Your schema looks correct. The query would look like:
query {
author(id: 1) {
id
posts {
id
title
}
}
}
2. create Post and attach to an author
In your example if you want to expose an interface to create a Post, then you would have to expose a mutation field in your schema
e.g:
type Mutation {
createPost(input: CreatePostInput): Post
}
if you want at the same time of creating a post, to also attach it to an author then you could add the authorId as part of the input, here we only want to attach the Post to an existing Author:
input CreatePostInput {
title: String
authorId: ID!
votes: Int
}
of course this is only the interface definition. We need to actually create the Post and link it to the Author inside the resolver.
The mutation query will look like:
mutation createPost {
createPost(input: {
title: "Title"
votes: 345
authorId: "authorId1"
}) {
id
title
votes
author {
id
firstName
lastName
}
}
}
Hope that helps!

Indexing List Type field in a GraphQL type from within a Query

Say I have the following GraphQL Schema
query {
allAuthors: [Author]
}
type Author {
id: ID!
name: String!
books: [Book]
}
type Book {
id: ID!
name: String!
author: Author!
}
Now I can successfully run the following query to get all the authors and their associated books
query {
allAuthors {
name,
books {
name
}
}
}
However, if I only want to get the first three books for all authors, how would I go about doing something like that? Can we index the books field in the Author type from within a query? If so, how?
I tried something like this and it doesn't work
query {
allAuthors {
name,
books[3] {
name
}
}
}
GraphQL doesn't have syntax for this.
You can add a "limit" parameter to a field, and this is common enough:
type Query {
allAuthors(limit: Int, offset: Int): [Author!]!
}
type Author {
id: ID!
name: String!
books(limit: Int, offset: Int): [Book!]!
}
If you add parameters like this to the schema, then the query you want (for all authors, get the first three books) could look like
{
allAuthors {
name
books(limit: 3) {
name
}
}
}

Query elements where relationship is null

If I have a simple schema like:
type Author #model {
id: ID! #isUnique
posts: [Post!]! #relation(name: "AuthorOfPost")
}
type Post #model {
id: ID! #isUnique
author: Author #relation(name: "AuthorOfPost")
}
How can I query all Posts that do not have an author?
Unfortunately, I cannot find something in the authorFilter like id_is_null.
try this!
query PostsWithAuthor {
allPosts(filter: { author: null }) {
id
author {
id
}
}
}

Graphql with mutation spring boot

My schema file is
type Mutation {
createCustomer(name: String!, email: String!, product: [Product]): Customer
}
input Product {
id: ID!
name: String!
price: Int
}
interface Person {
id: ID!
name: String!
email: String!
}
type Customer implements Person {
id: ID!
name: String!
email: String!
product: [Product]
}
I want to insert customer detail here which has product list as input. My query is
mutation {
createCustomer(
name: "kitte",
email: "kitte#gmail.com",
product: [
{
name: "soap",
price: 435,
}
]
)
{
id
name
email
product{name}
}
}
But I am getting exception
{
"data": null,
"errors": [
{
"validationErrorType": "WrongType",
"message": "Validation error of type WrongType: argument value ArrayValue{values=[ObjectValue{objectFields=[ObjectField{name='name', value=StringValue{value='dars76788hi'}}, ObjectField{name='price', value=IntValue{value=123}}]}, ObjectValue{objectFields=[ObjectField{name='name', value=StringValue{value='darr'}}, ObjectField{name='price', value=IntValue{value=145}}]}]} has wrong type",
"locations": [
{
"line": 5,
"column": 5
}
],
"errorType": "ValidationError"
}
]
}
I don't understand what is the error. And how to pass list to mutation. I have referred some examples but not able to insert product as list.
Make sure you are passing the right type of objects to your mutation. GraphQL needs separate types for input fields. In your schema, Product types should be something like this and you should change the mutation accordingly.
type Product {
id: ID!
name: String!
price: Int
}
input ProductInput {
name: String!
price: Int
}
input CustomerInput {
...
products: [ProductInput]
}
There are couple of very useful examples in the docs, see Mutations and Input Types

Resources