Integrating Apollo Studio with GraphQL for .NET - Part 3
Tags: dotnet, graphql, apollo, protobuf
Published: 2021-05-29 07:39:31

# Setting Up Apollo Studio
Now that we have [code to push metrics](https://dev.to/mattjhosking/integrating-apollo-studio-with-graphql-for-net-part-2-ln4), we need to set up Apollo Studio and get our API key. Navigate to [Apollo Studio](https://studio.apollographql.com/) and create a free account. You'll need to add a new graph - be sure to select "Deployed" as "Development" doesn't support reporting of query metrics.
![Create Graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/45odiqobgx8y17alr636.png)

# Uploading Your Schema
Now that we have an account and Graph, we'll need to upload our schema. The easiest way to get started is to do a manually triggered introspection of your local server:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ucs28w04wjvml6qujyls.png)
Make sure you include "/api/graphql" in your URL. Once this is uploaded, you'll have access to a number of tabs - go to the "Schema" tab. From there, the first thing you'll need is the "Graph Ref". This is used as an input to the GraphQL trace logger we created in the earlier articles:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dab8h92bpytbdfcswnkz.png)

# Creating an API Key
From the "Settings" tab, if you scroll down you can see the "API Keys" section. Create a new key (name is optional) and you'll be provided with an API key. As per usual with API keys, if you don't copy it somewhere, you won't be able to get it back and you'll have to delete the key and create a new one.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4y8ekrn3eolgspe8d6jx.png)
This API key is the other setting required by the trace uploader.

# Using the Trace Logger
Presuming you have your background service running, all you need is to register the trace logger in a way that provides these values. You can see how this has been done in the [GitHub](https://github.com/mattjhosking/graphql-dotnet-apollo-studio) supporting code. Since the signature for the constructor is:
```csharp
ApolloReportingTraceLogger(IHttpClientFactory httpClientFactory, ISchema schema, string apiKey, string graphRef, ILogger<ApolloReportingTraceLogger> logger)
```
I like to register it as follows:
```csharp
services.AddSingleton<IGraphQLTraceLogger>(provider =>
  ActivatorUtilities.CreateInstance<ApolloReportingTraceLogger>(provider, Configuration["ApolloStudio:ApiKey"], Configuration["ApolloStudio:GraphRef"])
);
```
This approach will automatically resolve `IHttpClientFactory` (provided you have used `services.AddHttpClient()`), `ISchema` and the `ILogger` from DI and use the first string as `apiKey` and the second as `graphRef`. Then all you need is the following setting in your `appsettings.json` file, with an override for your `appsettings.Development.json` for your local dev values (so you don't accidentally commit them to source, you should have the development version in `.gitignore`).
```json
  "ApolloStudio": {
    "ApiKey": "",
    "GraphRef": ""
  }
```

# Testing It Out!
Now that we have everything hooked up, we should be able to run a query and have it show in the Apollo Studio UI. Run your solution, and execute a query. In Apollo Studio, open the "Operations" tab and keep refreshing. After 30 seconds or so, your query should appear:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4at8sjv2za55et5b7eij.png)
You can get general performance statistics for your queries, but the features to break down errors and drill down into resolver-level performance is reserved for the paid version. Team version (at time of writing) is USD$59/mth per user (you can have unlimited read only users for free, though), or USD$49/mth if paid annually. There's a 14 day trial, which I've used to generate resolver level stats for my sample below:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a58sv5dsb4mzxmg79ykw.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20grtvu4j1ks5laf60lg.png)