question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

2.x - Aliases do not work with default resolvers in merged schemas

See original GitHub issue

I opened an issue on graphql-js: https://github.com/graphql/graphql-js/issues/1530#event-1862105684


Comment by Dan https://github.com/apollographql/apollo-server/issues/1724#issuecomment-425596109

This issues doesn’t seem to be related to fragments. Aliases do not appear to be working with default resolvers in merged schemas:


OP:

Basically, when using fragments in a hierarchy like the below query, aliases don’t work and can return non-nullable errors or they simply just cause the objects to be null.

It appears that my issue may not be related to the parser itself. This is a bug that was introduced when I upgraded to Apollo Server 2.x. I even rolled back every package except apollo-server to what I had before and the issue persists.

Playground: https://pinto-hip.glitch.me

Issue the query:

{
  books {
    name
    categoryHierarchy {
      ...category
      categories {
        ...category
      }
    }
  }
}

fragment category on Category {
  abc: name
}

Error:

{
  "data": {
    "books": [
      {
        "name": "Harry Potter and the Chamber of Secrets",
        "categoryHierarchy": [
          null
        ]
      },
      {
        "name": "Jurassic Park",
        "categoryHierarchy": [
          null
        ]
      }
    ]
  },
  "errors": [
    {
      "message": "Expected Iterable, but did not find one for field Category.categories.",
      "locations": [
        {
          "line": 6,
          "column": 7
        }
      ],
      "path": [
        "books",
        0,
        "categoryHierarchy",
        0,
        "categories"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Expected Iterable, but did not find one for field Category.categories.",
            "    at invariant (/rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/jsutils/invariant.js:19:11)",
            "    at completeListValue (/rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/execution/execute.js:673:65)",
            "    at completeValue (/rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/execution/execute.js:643:12)",
            "    at completeValue (/rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/execution/execute.js:629:21)",
            "    at completeValueWithLocatedError (/rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/execution/execute.js:580:21)",
            "    at completeValueCatchingError (/rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/execution/execute.js:550:12)",
            "    at resolveField (/rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/execution/execute.js:497:10)",
            "    at /rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/execution/execute.js:364:18",
            "    at Array.reduce (<anonymous>)",
            "    at executeFields (/rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/execution/execute.js:361:42)"
          ]
        }
      }
    },
    {
      "message": "Expected Iterable, but did not find one for field Category.categories.",
      "locations": [
        {
          "line": 6,
          "column": 7
        }
      ],
      "path": [
        "books",
        1,
        "categoryHierarchy",
        0,
        "categories"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Expected Iterable, but did not find one for field Category.categories.",
            "    at invariant (/rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/jsutils/invariant.js:19:11)",
            "    at completeListValue (/rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/execution/execute.js:673:65)",
            "    at completeValue (/rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/execution/execute.js:643:12)",
            "    at completeValue (/rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/execution/execute.js:629:21)",
            "    at completeValueWithLocatedError (/rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/execution/execute.js:580:21)",
            "    at completeValueCatchingError (/rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/execution/execute.js:550:12)",
            "    at resolveField (/rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/execution/execute.js:497:10)",
            "    at /rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/execution/execute.js:364:18",
            "    at Array.reduce (<anonymous>)",
            "    at executeFields (/rbd/pnpm-volume/0897a123-e913-41b8-a689-0921f96aa33a/node_modules/.registry.npmjs.org/graphql/0.13.2/node_modules/graphql/execution/execute.js:361:42)"
          ]
        }
      }
    }
  ]
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12

github_iconTop GitHub Comments

1reaction
dncrewscommented, Sep 28, 2018

This issues doesn’t seem to be related to fragments. Aliases do not appear to be working with default resolvers in merged schemas:

This doesn’t work:

  const typeDefs = gql`
    type Query {
      book: Book
    }

    type Book {
      category: String!
    }
  `;

  const schema = makeExecutableSchema({ typeDefs });

  const resolvers = {
    Query: {
      book: () => ({ category: 'Test' })
    }
  };

  const server = new ApolloServer({
    schema: mergeSchemas({
      schemas: [ schema ],
      resolvers
    }),
  });

This does:

  const typeDefs = gql`
    type Query {
      book: Book
    }

    type Book {
      category: String!
    }
  `;

  const schema = makeExecutableSchema({ typeDefs });

  const resolvers = {
    Query: {
      book: () => ({ category: 'Test' })
    },
    Book: {
      category: (parent) => parent.category // Adding default resolver to fix bug
    }
  };

  const server = new ApolloServer({
    schema: mergeSchemas({
      schemas: [ schema ],
      resolvers
    }),
  });
1reaction
dncrewscommented, Sep 27, 2018

Expected Iterable, but did not find one for field Category.categories.

The error message is correct Your bug is here:

  const books = [
    {
      name: 'Harry Potter and the Chamber of Secrets',
      author: 'J.K. Rowling',
      categoryHierarchy: [
        { name: 'Fiction', categories: { name: 'Fantasy', categories: [] } }
      ]
    },
    {
      name: 'Jurassic Park',
      author: 'Michael Crichton',
      categoryHierarchy: [
        { name: 'Fiction', categories: { name: 'Sci-Fi', categories: [] } }
      ]
    },
  ];

You aren’t returning an Iterable (array) for the first level of categories. Should be:

  const books = [
    {
      name: 'Harry Potter and the Chamber of Secrets',
      author: 'J.K. Rowling',
      categoryHierarchy: [
        { name: 'Fiction', categories: [ { name: 'Fantasy', categories: [] } ] }
      ]
    },
    {
      name: 'Jurassic Park',
      author: 'Michael Crichton',
      categoryHierarchy: [
        { name: 'Fiction', categories: [ { name: 'Sci-Fi', categories: [] } ] }
      ]
    },
  ];
Read more comments on GitHub >

github_iconTop Results From Across the Web

API Reference: graphql-tools - Apollo GraphQL Docs
However that presents problems, because default GraphQL resolvers retrieve field from parent based on their name, not aliases. This way results with aliases...
Read more >
graphql-tools difference between mergeSchemas and ...
Schema Merging creates a new schema by merging the extracted type definitions and resolvers from them, so there will be a single execution...
Read more >
Schema Merging – GraphQL Tools
Schema merging ( @graphql-tools/merge and @graphql-tools/schema ) consolidates the type definitions and resolvers from many local schema ...
Read more >
Customizing the GraphQL Schema - Gatsby
In many cases this works very well and it is still the default mechanism for creating a GraphQL schema. There are however two...
Read more >
Resolver mapping template programming guide - AWS AppSync
This is a cookbook-style tutorial of programming with the Apache Velocity ... a simple GraphQL schema and passing a map of values to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found