Search code examples
node.jsgraphqlhapi

Simple graphql schema throwing syntax error, also unable to trace the issue line from logs


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.


Solution

  • 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!    
    }