3

I'm trying to create a simple graphql backend using apollo-server-express.

Doing this in a expressJS application (Github repo) is working as expected:

Executing the query

query {
    search {
        result
    }
}

at localhost:4000/graphql returns result as expected.

resolver

module.exports = {
  Query: {
    search: async (obj, { name, value }) => {
      return 'result'
    }
  }
}

Now I would like to use this expressJS backend in a nx.dev environment (Github repo). I set up the same thing here, but running the server and executing the query in the graphQL playground returns null.

I don't see what is going wrong.

This is my structure:

main.ts

const express = require('express');
const { ApolloServer } = require('apollo-server-express');

const typeDefs = require('./graphql/types')
const resolvers = require('./graphql/resolvers')

const app = express();

const server = new ApolloServer({ typeDefs, resolvers });
server.applyMiddleware({ app });

app.listen({ port: 4000 }, () =>
  console.log(` Server ready at http://localhost:4000${server.graphqlPath}`)
)

structure

+-- graphql
|   +-- author
|   |   +-- author.resolvers.js
|   |   +-- author.graphql
|   +-- search
|   |   +-- search.resolvers.js
|   |   +-- search.graphql
|   +-- resolvers.js <<< Merges all `*.resolvers.*` files
|   +-- types.js <<< Merges all `*.graphql` files

resolvers.js

const path = require('path');
const { mergeResolvers } = require('@graphql-tools/merge');
const { loadFilesSync } = require('@graphql-tools/load-files');

const resolversArray = loadFilesSync(path.join(__dirname, "./**/*.resolvers.*"));
module.exports = mergeResolvers(resolversArray);

types.js

const path = require('path');
const { loadFilesSync } = require('@graphql-tools/load-files');
const { mergeTypeDefs } = require('@graphql-tools/merge');

const typesArray = loadFilesSync(path.join(__dirname, './**/*.graphql'))

module.exports = mergeTypeDefs(typesArray, { all: true })

graphql/search/search.graphql

type Query {
  search(name: String, value: String): [String]
}

graphql/search/search.resolvers.js

module.exports = {
  Query: {
    search: async (obj, { name, value }) => {
      return 'result'
    }
  }
}
user3142695
  • 15,844
  • 47
  • 176
  • 332
  • That code should work as-is. I tested it locally with the latest version of graphql-tools and it works fine. This could be an issue related to nx.dev. You might want to try using an absolute path for the globs instead of using __dirname and see if that fixes the issue. – Daniel Rearden Jul 09 '20 at 01:47
  • @DanielRearden Could you please have a look at the repo? https://github.com/jaqua/nx – user3142695 Jul 09 '20 at 15:37
  • Works fine for me except I had to fix the missing `path.join`. – Daniel Rearden Jul 09 '20 at 16:21
  • @DanielRearden What did you have to fix? Because I'm also running in this problem now – user3142695 Jul 09 '20 at 16:50
  • you changed path.join above to just path in the repo – Daniel Rearden Jul 09 '20 at 17:05
  • @DanielRearden Oh, that was a typo. Thanks. But I do not understand the problem. Running `nx serve backend` throws me `Error: Query root type must be provided.` I just created a separate express app with exactly the same structure and it is working perfectly. The nx project is not working for me, but it is working for you?!? – user3142695 Jul 09 '20 at 18:42
  • @DanielRearden Could you please test, if you get an result at the playground `localhost:4200/graphql` with the query `query { search }`? I do get `null` instead of the string – user3142695 Jul 09 '20 at 19:47

0 Answers0