Query Optimizely Data Platform (ODP) GraphQL with Strawberry Shake
This blog post will demonstrate how to get ODP data using its GraphQL endpoint using the Strawberry Shake package as client which will reduce the overall complexity for the implementation. So without further due. Lets begin.
First, you must go to your project folder through a command line terminal, we are going to use PowerShell. and execute this command which will install the tool manifest required by the package
dotnet new tool-manifest
After that install the Strawberry tools as local in the project
dotnet tool install StrawberryShake.Tools
Now, we can add some NuGet packages to the project through the Visual studio interface or command line. The packages that we need to add are:
- StrawberryShake.CodeGeneration.CSharp.Analyzers
- StrawberryShake.Transport.Http
Now, we are going to initialize the GraphQL client using the schema from ODP
dotnet graphql init https://api.zaius.com/v3/graphql --headers x-api-key=YOUR_API_KEY -p .\Infrastructure\Odp\ -n OdpClient
The first parameter of the init command is the URL where we are going to get the schema, the headers parameters allow us to set an API-Key which is required in order to read the schema in ODP, the path parameter specifies where the schema and all the auto generated code will be saved and the name parameter specifies how the client class is going to be named.
Do not modify any file inside the generated folder, nor the schema.extensions.graphql, .graphqlrc.json or schema.graphql files.
As soon as this is done, you can create a queries folder where you can add GraphQL queries with .grapqh extension which will be automatically added to your ODP client when the project is built.
For instance, to get a customer data through an email, you can use this query
query OdpCustomer($email: String) {
customer(email: $email) {
zaius_id
customer_id
name
first_name
last_name
email
image_url
phone
street1
street2
city
state
zip
country
timezone
last_observed_timezone
gender
ts_gdpr_opted_out
vuid
data_source_type
data_source
data_source_instance
data_source_version
data_source_details
age
dob_day
dob_month
dob_year
ccpa_opted_out
last_modified_at
marital_status
employee_status
level_of_studies
job_type
homeowner
children
annual_income
}
}
Which when the project is build can be accessed using the client in this way
public class OdpManager
{
public IOdpClient OdpClient { get; set; }
public OdpManager(IOdpClient odpClient)
{
OdpClient = odpClient;
}
public async Task<IOdpCustomer_Customer> GetCustomerByEmailGraphAsync(string email)
{
var result = await OdpClient.OdpCustomer.ExecuteAsync(email);
return result.Data?.Customer;
}
}
The method name in the client will correspond to the name of the query in the graphql file. And to get the ODP client Initialized you can use constructor dependency injection.
Finally, you must add to your startup.cs or program.cs file the initialization of the ODP client so it can be properly injected.
services.AddOdpClient().ConfigureHttpClient(client =>
{
client.BaseAddress = new Uri(_configuration["Odp:Url"]);
client.DefaultRequestHeaders.Add("x-api-key", _configuration["Odp:ApiKey"]);
});
The ODP URL and ODP Api key comes from the app.settings.json files
"Odp": {
"Url": "https://api.zaius.com/v3/graphql",
"ApiKey": "YOUR_API_KEY"
}
And that is it. You can now query any ODP data using the GraphQL endpoint in your CMS site with autogenerated code for new queries. If you have any questions or suggestions please let me know in the comments. I hope this can help someone and as always keep learning !!!
Leave a Reply