Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expected Iterable, but did not find one for field #55

Open
BarryThePenguin opened this issue May 27, 2022 · 5 comments · May be fixed by #56
Open

Expected Iterable, but did not find one for field #55

BarryThePenguin opened this issue May 27, 2022 · 5 comments · May be fixed by #56

Comments

@BarryThePenguin
Copy link

When a schema defines a list using an interface, it looks like @miragejs/graphql fails to build the field for the list

interface ItemRead {
  authors: [String!]!
  id: ID!
  title: String!
}

type BookChapterRead implements ItemRead {
  authors: [String!]!
  chapter: Int
  id: ID!
  title: String!
}

type BookRead implements ItemRead {
  authors: [String!]!
  id: ID!
  rating: Int
  title: String!
}

type Query {
  booksRead: [BookRead]
  bookChapterRead: [BookChapterRead]
  itemsRead: [ItemRead]
}

Querying for..

{
  itemsRead {
    id
    title
    authors
  }
}

..fails with..

Expected Iterable, but did not find one for field "Query.itemsRead".

..while querying other fields works fine.

I put together a reproduction in the REPL https://miragejs.com/repl/v2/042996bcd5

@BarryThePenguin
Copy link
Author

Looking at the mirageGraphQLFieldResolver, it may need additional checks for lists that are a GraphQLInterfaceType or GraphQLUnionType?

export default function mirageGraphQLFieldResolver(obj, args, context, info) {
let { isList, type } = unwrapType(info.returnType);
return isInterfaceType(type)
? resolveInterface(obj, args, context, info, type)
: isUnionType(type)
? resolveUnion(obj, args, context, info, isList, type)
: !isObjectType(type)
? resolveDefault(obj, args, context, info)
: isList
? resolveList(obj, args, context, info, type)
: resolveObject(obj, args, context, info, type);
}

@jneurock
Copy link
Collaborator

I can't remember the auto-resolution logic at play here but it might be the case that the library will only try to auto-resolve types that implement an interface. I'll have to check as soon as I get a chance. In the meantime, you should be able to add your own resolver for the field, if you haven't done so already.

@BarryThePenguin
Copy link
Author

Ok, thanks! I hadn't considered adding a field resolver

@BarryThePenguin
Copy link
Author

BarryThePenguin commented May 30, 2022

I didn't have much luck making a field resolver.. But I was able to reproduce the issue in #56

@jneurock
Copy link
Collaborator

I think this will work for you https://miragejs.com/repl/v2/571a844c0b:

this.post("/graphql", createGraphQLHandler(graphQLSchema, this.schema, {
  resolvers: {
    Query: {
      itemsRead(_obj, _args, context) {
        const booksRead = context.mirageSchema.db.bookReads.map((book) => ({
          ...book,
          __typename: 'BookRead'
        }));
        const bookChaptersRead = context.mirageSchema.db.bookChapterReads.map((chapter) => ({
          ...chapter,
          __typename: 'BookChapterRead'
        }));
        
        return [...booksRead, ...bookChaptersRead];
      }
    }
  }
}))

Mirage's inflector doesn't seem to be able to handle the pluralization well in this case so the table names are a bit weird.

@BarryThePenguin BarryThePenguin linked a pull request Jun 7, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants