Sample Code
C# Sample Client:
using GraphQL;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.SystemTextJson;
class Program
{
static async Task Main(string[] args)
{
string apiUrl = "https://graph-prod-sandbox.liv.championdata.io/graphql";
string apiKey = "YOUR_API_KEY";
// create the graphql client
var graphQLClient = new GraphQLHttpClient(apiUrl, new SystemTextJsonSerializer());
graphQLClient.HttpClient.DefaultRequestHeaders.Add("x-api-key", apiKey);
// create a query for all players
var playersRequest = new GraphQLRequest
{
Query = @"{
players {
playerId
firstName
lastName
displayName
code
country {
name
}
dob
}
}"
};
// fetch the all players data
var playersResponse = await graphQLClient.SendQueryAsync<PlayersResponse>(playersRequest);
// create a query for a specific player id
var playerByIdRequest = new GraphQLRequest
{
Query = @"{
player(playerId: 12) {
playerId
firstName
lastName
displayName
code
country {
name
}
dob
}
}"
};
// fetch the player data
var playerByIdResponse = await graphQLClient.SendQueryAsync<PlayerResponse>(playerByIdRequest);
// create a query for a specific player id, this time using a variable
var playerWithVarRequest = new GraphQLRequest
{
Query = @"
query getPlayer($playerId: Int) {
player(playerId: $playerId) {
playerId
firstName
lastName
displayName
code
country {
name
}
dob
}
}",
OperationName = "getPlayer",
Variables = new
{
playerId = 12
}
};
// fetch the player data
var playerWithVarResponse = await graphQLClient.SendQueryAsync<PlayerResponse>(playerWithVarRequest);
}
public class PlayersResponse
{
public List<Player> players { get; set; }
}
public class PlayerResponse
{
public Player player { get; set; }
}
public class Player
{
public int playerId { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string displayName { get; set; }
public Country country { get; set; }
}
public class Country
{
public string name { get; set; }
}
}
Node.js Sample Client:
const axios = require('axios');
const apiUrl =
'https://graph-prod-sandbox.liv.championdata.io/graphql';
const apiKey = 'YOUR_API_KEY';
async function executeQuery(query, variables) {
try {
const response = await axios.post(
apiUrl,
{
query,
variables: JSON.stringify(variables),
},
{
headers: {
'x-api-key': apiKey,
},
}
);
console.log(response.data);
} catch (error) {
console.error(error);
}
}
// Query: Get list of players
const playersQuery = `
{
players {
playerId
firstName
lastName
displayName
code
country {
name
}
dob
}
}
`;
// Query: Get player details by ID
const playerQuery = `
query($playerId: Int) {
player(playerId: $playerId) {
playerId
firstName
lastName
displayName
code
country {
name
}
dob
}
}
`;
// Execute queries
executeQuery(playersQuery);
executeQuery(playerQuery, { playerId: 12 });
Make sure to replace "YOUR_API_KEY"
with your API key.
These sample clients use client libraries (GraphQL.Client in C#, axios in Node.js) to send POST requests to the GraphQL API endpoint. The API key is added to the request headers as "x-api-key" for authentication. The queries are sent as JSON payloads, and the responses are printed to the console.
Please note that these examples are meant to serve as a starting point and may need modifications based on your specific requirements or the libraries and frameworks you're using in your project.