TransWikia.com

Apollo/Express server is not working in nx.dev environment

Stack Overflow Asked by user3142695 on November 22, 2021

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'
    }
  }
}

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP