0

I am trying to create a GraphQL implementation in my node project. I have created a schema and resolver in separate js and I am calling them from the index.js file. Below are the codes in the schema, resolver, and index js files

schema

const schema = `      
  type Query {
    testFunction(): String!    
  }    
  schema {
    query: Query    
  }
`;

module.exports = schema; 

resolver

const resolvers = ()=>({
Query:{
    testFunction(){
        return "returned from custom test function";
    }
}
});

module.exports = resolvers;

index

const graphqlSchema = require('./graphql/schema');
const createResolvers = require('./graphql/resolvers');

const executableSchema = makeExecutableSchema({
    typeDefs: [graphqlSchema],
    resolvers: createResolvers()
 });

const server=hapi.server({
    port: 4000,
    host:'localhost'
});

server.register({
    register: apolloHapi,
    options: {
      path: '/graphql',
      apolloOptions: () => ({
        pretty: true,
        schema: executableSchema,
      }),
    },
});

server.register({
   register: graphiqlHapi,
   options: {
     path: '/graphiql',
     graphiqlOptions: {
       endpointURL: '/graphql',
      },
    },
});

const init= async()=>{
    routes(server);
    await server.start();
    console.log(`Server is running at: ${server.info.uri}`);
}

init();

I am getting the below error when starting the server

node_modules\graphql\language\parser.js:1463 throw (0, _error.syntaxError)(lexer.source, token.start, "Expected >".concat(kind, ", found ").concat((0, _lexer.getTokenDesc)(token)));

Please help me out in understanding where I am going wrong and how to overcome the issue.

Yashwardhan Pauranik
  • 5,370
  • 5
  • 42
  • 65

1 Answers1

3

Any field in a schema can have arguments. If a field includes arguments, they should be wrapped in a pair of parentheses, like this:

type Query {
  testFunction(someArg: Int): String!    
}

However, when there are no arguments, it's not valid syntax to just have an empty pair of parentheses. You should omit them altogether, like this:

type Query {
    testFunction: String!    
}
Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • Thank you @Daniel Rearden, I removed the parenthesis. But now the console is throwing me error that "register" is missing, whereas i have register in my register function. I tried renaming the register key to plugin as well, but still the same issue i am using the following dependencies apollo-server-hapi": "^2.3.1", "graphql": "^14.0.2", "graphql-tools": "^4.0.3", "hapi": "^17.8.1", – Tech de Enigma Jan 06 '19 at 07:26
  • That sounds like a separate issue and should probably be a separate question that includes the full error you're seeing. Fwiw, both `graphiqlHapi` and `apolloHapi` are undefined in the code you posted. This shows how to correctly setup `apollo-server-hapi` for Hapi 17: https://www.apollographql.com/docs/apollo-server/v1/servers/hapi.html#Usage – Daniel Rearden Jan 06 '19 at 13:53