0

I have gone through this blog followed the instructions in the blog post http://graphql.org/blog/rest-api-graphql-wrapper/ to create a graphQL endpoint over my own REST API. If I log the calls in the console I can see the correct response getting generated, but the data is always NULL in GraphiQL IDE. What could be the reason?

Here is my code:

import {
 GraphQLSchema,
 GraphQLObjectType,
 GraphQLString,
} from 'graphql'

import fetch from 'node-fetch'

const BASE_URL = 'http://localhost/my.test.web/api/v1/customer/91/reservation'

const ReservationType = new  GraphQLObjectType({
 name: 'Reservation',
 description: 'This is reservation details',
 
 
 fields: () => ({
  id: {type: GraphQLString},
  confirmationNumber: {
   type: GraphQLString,
   resolve: (reservation) => reservation.confirmationNumber
  },
  status: {
   type: GraphQLString,
   resolve: (reservation) => reservation.status
  }
  
 })
});


const QueryType = new GraphQLObjectType(
{
 name: "query",
 description: "This is query by Id",
 
 fields: () => ({
   reservation: {
    type: ReservationType,
    args: {
     id: {type: GraphQLString}
    },
    resolve: (root, args) => {
     var url = BASE_URL+ '/' + args.id;
     console.log(url);
     var options = {
      headers: {
      'Accept': 'application/json',
      'Accept-Language':'en-US'
      }
     };     
     fetch(url,options)
       .then(function(res) {
        return res.json();
       }).then(function(json) {
        console.log(json);
        return json;
       });
     }
   }
  }
 )
});

export default new GraphQLSchema(
  {
   query: QueryType,
  }
 )

When I run this using graphiQL and express, I can see that the log is correctly generated by this part of the code -

.then(function(json) {
                          console.log(json);
                          return json;
                      }

But in the GraphiQL UI the data is null GraphiQL IDE query screenshot

Kiran
  • 33
  • 4
  • In the log statement I am able to see the correct JSON returned by the API but it is null in GraphiQL UI – Kiran Jan 20 '17 at 11:41

1 Answers1

0

Finally I was able to find the cause - It is the syntax and not the JSON returned. Notice the "," at the end of each block and also removed the wrapper around the resolve:

The QueryType should be defined as follows and it works like a charm

const QueryType = new GraphQLObjectType({
  name: "query",
  description: "This is person query by Id",
  fields: () => ({
    person: {
      type: PersonType,
      args: {
        id: { type: GraphQLString },
      },
      resolve: (root, args) =>     
     fetch(BASE_URL +'/people/' +args.id)
      .then(function(res) {
        return res.json()
      })
       .then(function(json) {
         console.log(json)
         return json
       }),    
    },
  }),
});
Kiran
  • 33
  • 4