Tech de Enigma
Tech de Enigma

Reputation: 161

GraphQL schema query not recognizing passed input parameters in the resolver function

I defined the below query in my GraphQL schema

type Query {        
    getShowByName(showSchemaName: String!): String!        
}

with the corresponding resolver function as shown below

const resolvers = ()=>({
    Query:{

        getShowByName: function(args){
            console.log("out1"+args);
            console.log("out3"+args.showSchemaName);            
            return "hardcoded return from getShowByName";           
        },        
    },    
});

In the graphql playground, i provided the below inputs

{
  getShowByName(showSchemaName:"input to getShowByName")  
}

The graphql playground provides hardcoded return from getShowByName as output in the playground page but in the terminal i get args as undefined. Hence, i am unable to parse the input fed in from the graphql playground.

Please help me in understanding where i am going wrong and how i can rectify the issue.

Upvotes: 1

Views: 1658

Answers (1)

xwlee
xwlee

Reputation: 1099

The first argument in the resolver is an object that contains the result of parent resolver. In your case, the root level Query will receive undefined. Instead you should extract the args from the second argument.

const resolvers = ()=> ({
    Query: {
        getShowByName: function(_, args) {
            console.log("out1" + args);
            console.log("out3" + args.showSchemaName);            
            return "hardcoded return from getShowByName";           
        },        
    },    
});

Upvotes: 3

Related Questions