'Apollo GraphQL resolver is not executed, returns null

I'm having an issue with Apollo GraphQL. Consider the following code:

import { ApolloServer } from 'apollo-server';
import path from 'path';
import { loadSchemaSync } from '@graphql-tools/load'
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'
...
const schema = loadSchemaSync(path.join(__dirname, './schema/*.graphql'), { 
  loaders: [
      new GraphQLFileLoader()
  ]
})

const resolvers: Resolvers = {
  Query: {
    taskResults: (root, args, context) => {
      console.log('taskResults')
      return []
    }
  }
}

;(async () => {
  const server = new ApolloServer({
    schema,
    resolvers,
    csrfPrevention: true
  });
  const { url } = await server.listen()
  console.log(`🚀  Server ready at ${url}`);
})().catch(console.error)

When I execute this query:

query {
  taskResults {
    id
  }
}

It returns (actual)

{
  "data": {
    "taskResults": null
  }
}

instead of (expected)

{
  "data": {
    "taskResults": []
  }
}

Also, it does not print "taskResults" to the console, which means that the resolver is not actually executed.



Solution 1:[1]

The problem was that I was assigning the result of loadSchemaSync() to schema and not typeDefs:

const typeDefs = loadSchemaSync(path.join(__dirname, './schema/*.graphql'), { 
  loaders: [
      new GraphQLFileLoader()
  ]
})

...

const server = new ApolloServer({
  typeDefs,
  ...
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mitchkman