Queries

Lets, look at how retrieving data works in GraphQL. The following snippet is a sample query for all hotels in our systems, and moreover just the IDs of the hotels. This will be sent from the client to the server:

{ 
  allHotels { 
    id 
  } 
} 

The allHotels field in this query is called the root field of the query. Everything under the root field is the payload of the query.

The server will respond with a JSON detailing all the hotels, ids in the database, like so:

{ 
  "allHotels": [ 
    { "id": "xyz" }, 
    { "id": "abc" }, 
    { "id": "pqr" } 
  ] 
} 

If the client needs the display name (or any other field), it has to ask for it explicitly in the query, like so:

{ 
  allHotels { 
    Id 
    displayName 
  } 
} 

This exact specification of fields is possible even for nested fields. Thus, to get the name of the hotel chain and the display name of each hotel in the chain, the following query will work:

{ 
  allHotelsinChain { 
    name 
    hotels { 
         displayName 
    } 
  } 
} 

Queries are explicitly specified by the root field name, and also can take arguments as follows:

{ 
  allHotels (city: Delhi) { 
    id 
    displayName 
  } 
} 

The preceding is a query that takes the city name as an argument, and returns all the hotels in a specific city, and the ID and the display name for each hotel.

In graphql-go, the rootQuery structure that we defined previously handles all the queries. We create it using the graphql.NewObject() function. This creates a graphql-go terminology object that has fields, a name, and a resolver function, which describes what to do when such an object is invoked:

var rootQuery = graphql.NewObject(graphql.ObjectConfig{ 
   Name: "RootQuery", 
   Fields: graphql.Fields{ 
 
         "hotel": &graphql.Field{ 
               Type:        hotelType, 
               Description: "Get a hotel with this id", 
               Args: graphql.FieldConfigArgument{ 
                     "id": &graphql.ArgumentConfig{ 
                           Type: graphql.String, 
                     }, 
               }, 
               Resolve: func(params graphql.ResolveParams) 
(interface{}, error) { id, _:= params.Args["id"].(string) return hotels[id], nil }, }, }, })

Here we describe a query that takes in the hotel ID and gives the hotel object from an in-memory map called hotels. In real life, of course, one would use a database to store and retrieve entities, but the map serves the purpose of simplifying things and helping us to focus on the API semantics. Like the example in the REST API section, map is just a global variable:

// repository 
var hotels map[string]Hotel 
 
func init() { 
   hotels = make(map[string]Hotel) 
} 

The following CURL request demonstrates how to query a hotel with a specific ID:

curl -g 'http://localhost:8080/graphql?query={hotel(id:"XVlBzgba"){displayName,city,noRooms,starRating}}' 

See the following mutation example for actually creating hotel objects.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset