Posts

Showing posts from May, 2024

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20

Encounter this issue when running autorest (that uses nodejs).  I am using node version:  v18.20.2 After i revert to use node v18.12.0, then i was able to get my app to run without this error. Since i am working with Azure devops yaml, i was able to use the following to resolve my issue.  - task : NodeTool@0   inputs :     versionSource : 'spec'     versionSpec : '18.18.0'

Apollo federation schema version

You can get the valid and latest version of apollo federation by going into the following url  https://www.apollographql.com/docs/federation/federation-versions/  

apollo router - running customize settings from a configuration file

  To run your router from a configuration file, you only have to use the followings router --dev --supergraph supergraph.graphql --config router.yaml From a kubernetes  https://github.com/apollographql/supergraph-demo-fed2/tree/main/supergraph/router-no-code

tsc build issue file is entry point of type library specified here.

 Getting this error today and then found out that i literally have to run "npm install" to get this working again. 

Strawberry shake - Exec(1,1) - All operation must be named

 If you have a query with name then you will get this error during dotnet build. For example,  query {    <--- missing name     book {     title    } } When you change it  this below, then you will not get a compilation error. Noticed we have appended query with GetBook. query GetBook {    book {     title    } }

graphql strawberry shake console app sample

My sample code for using strawberry shake to connect to a graphql service https://github.com/mitzenjeremywoo/graphl-client-strawberry-shake Some important things to note:  - graphqlrc.json  - schema.graphql - schema.extensions.graphql  - *.graphql - In the example, the filename is not important but having GetSession query in important as the code make use of the field return in the query.  As shown in the example here

error: src refspec main does not match any - probably the silly mistake that one can bump into

 If you hit this error, all you need to do is add or commit your code into git. 😅

package-lock.json when it is corrupted.

We can use the following command to re-genearted package-lock.json  npm i --package-lock-only  

refit http logging example

This is based off this blog .  I added some a minimal api version of it. You can check it out here .  Some of the pitfalls that I encounter are documented in the code for example,  Trying to setup my HttpMessageHandler that work for me. //// Adding our new handler here refitClientBuilder . AddHttpMessageHandler ( serviceProvider   = > new HttpLoggingHandler ( serviceProvider . GetRequiredService < ILogger < HttpLoggingHandler >>())); refitClientBuilder . Services . AddSingleton < HttpLoggingHandler >(); // doésn't work for me //refitClientBuilder.AddHttpMessageHandler<HttpLoggingHandler>(); Using the injected service version of it app . MapGet ( "/hello" , async ( IRandomUserAPI client ) => {     // no don't try this - use DI instead     //var randomClient = RestService.For<IRandomUserAPI>("https://randomuser.me");             var result = await client . GetUser ();     Console . WriteLine ( result . Results [ 0 ]

aspnet core setting your webapi to listen on certain port

  You can use the following configuration to set you up quickly.   "Kestrel": {    "EndPoints": {      "Http": {        "Url": "http://localhost:4200"      }    }  }

refit - unable to find AddRefitClient extension

  To resolve this please add package run this from the command line dotnet add package Refit.HttpClientFactory --version 7.0.0

setting mitmproxy options

You can configure mitmproxy settings by using the following example  mitmproxy --set ssl_insecure=true  For a more complete list of options please refer to the link here .

AddJwtBearer understanding its purpose

The purpose of using AddJwtBearer is to secure your API endpoint (the ones decorated with Authorization attribute) and setting those constraints for example audience must have certain value before it can access your API.  In example configuration below, we only allow JWT token with audience "https://kepungapp.auth0.com/api/v2/" access. All other request will get 401.  You need to setup options.Authority and it must be a valid token issuer. builder . Services . AddAuthentication ()               . AddJwtBearer ( options =>         {                     options . Audience = "https://kepungapp.auth0.com/api/v2/" ;             options . Authority = "https://kepungapp.auth0.com/" ;                       //options.RequireHttpsMetadata = false;         }); If you run the following curl it will work and get a 200 response from /hello endpoint. curl -- location -- request GET 'http://localhost:5019/hello' \ -- header 'Content-Type: applica

aspnet core AddJwtBearer unable to find extension method

This might be abit too easy but sometimes it happens and then we need a proper solution. So just posting for so it can be helpful. If you trying to do something like  builder.Services.AddAuthentication() .AddCookie(options => { options.LoginPath = "/Account/Unauthorized/" ; options.AccessDeniedPath = "/Account/Forbidden/" ; }) .AddJwtBearer(options => { options.Audience = "http://localhost:5001/" ; options.Authority = "http://localhost:5000/" ; }); But getting unable to reference AddJwtBearer, just do this  dotnet add package  Microsoft.AspNetCore.Authentication.JwtBearer

hotchoc mongodb example code to add update and delete from a collection.

I have an simple example to add, update and remove mongodb using hotchoc graphql.   https://github.com/mitzenjeremywoo/hotchoc-graphql-db

hotchoc IMongoCollection returns only 1 record because of UseFirstOrDefaultAttribute

Figure i post the intention out before someone think it is a different issues. After some copy and paste job, I had this attribute in my graphql query method - which literally would return only 1 row or default eventhough I have many records in my collections. 

TypeLoadException: Could not load type 'Microsoft.IdentityModel.Json.JsonConvert' from assembly 'Microsoft.IdentityModel.Tokens, Version=7.0.3.0

Was getting this error, trying to add authentication feature into my app. Due to the assembly issues, I had to use the following assembly versions to make it work. 1st attempt <PackageReference Include="Microsoft.AspNetCore. Authentication.OpenIdConnect" Version="7.0.5" /> <PackageReference Include="Microsoft.AspNetCore. OpenApi" Version="7.0.5" /> <PackageReference Include="Swashbuckle. AspNetCore" Version="6.4.0" /> <PackageReference Include="System.IdentityModel. Tokens.Jwt" Version="7.0.3" /> 2nd attempt (more recent version of the assemblies)   <PackageReference Include="Microsoft.AspNetCore. Authentication.OpenIdConnect" Version="8.0.4" /> <PackageReference Include="Microsoft.AspNetCore. OpenApi" Version="7.0.5" /> <PackageReference Include="Swashbuckle. AspNetCore" Version="6.4.0" /> <PackageRefer

hotchoc graphlq setting paging nation defaults programmatically

  Use the following code to configure your paginations sizes: public class Startup {     public void ConfigureServices ( IServiceCollection services )     {         services             . AddGraphQLServer ()             . SetPagingOptions ( new PagingOptions             {                 MaxPageSize = 100             });     } }

Mongodb - Element '_id' does not match any field or property of class -

  Getting this error when trying to query from my mongodb collection.  Element '_id' does not match any field or property of class.  Since I still wanted to work with the extra field _id, i would want to include additional field in there. public class Restaurant {     [ BsonId ]     public MongoDB . Bson . ObjectId _id { get ; set ; }       public string borough { get ; set ; }     public string cuisine { get ; set ; } } Sometimes you might want to consider ignoring additional field.  [ BsonIgnoreExtraElements ] public class Restaurant {     [ BsonId ]     public MongoDB . Bson . ObjectId _id { get ; set ; }       public string borough { get ; set ; }     public string cuisine { get ; set ; } }

Mongodb - Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1

 As stated in the stackoverflow, there could be a number of reason for this - but for my case, i ensure that the connection credential (username/password) is correct. 

postgres - when table size takes up more and more space

Some quick command that can be helpful to clear off some of the table spaces. -- Step 2. Check Table Size SELECT pg_size_pretty(   pg_total_relation_size( 'your-table-name' ) ) as table_size; SELECT   schemaname,   relname,   n_dead_tup FROM pg_catalog.pg_stat_all_tables WHERE relname = 'your-table-name' ; -- before autovacuum SELECT   schemaname,   relname,   n_dead_tup FROM pg_catalog.pg_stat_all_tables --- run vaccum vacuum your - table - name -- after autovacuum SELECT   schemaname,   relname,   n_dead_tup FROM pg_catalog.pg_stat_all_tables

graphql hotchoc using ResolveWith

  Using resolveWith might be a good use-case for you. You can use it with the following code example,  Then to run your query  query {     foo ( arg : "" )   }

hotchoc - supporting multiple object for resolving your object in graphql

Let's say you need to organize your code and instead of placing all the query method into a single object like you can do with AddQueryType<> in hotchoc graphql, you can place this into multiple object for easy maintenance.  For the rest of your classes you can have the followings   That's it!   

AKS best practices

To get all the best practices for AKS, please check out https://learn.microsoft.com/en-us/azure/aks/best-practices

AKS - Service connector setup and wiring up that to your app

To setup your service connector in Azure AKS Cluster https://learn.microsoft.com/en-us/azure/service-connector/quickstart-portal-aks-connection?tabs=UMI One you have your connection registered, it is time to used that with your applications https://learn.microsoft.com/en-us/azure/service-connector/tutorial-python-aks-storage-workload-identity  

AKS updates and other new features

One of the best place to get more additional info about AKS new features can be found here. https://github.com/Azure/AKS/tree/master Then you branch out to the followings interesting link - AKS Roadmap (https://github.com/Azure/AKS/projects/1) - Features (https://github.com/Azure/AKS/projects/1#column-5273286)

keycloak token exchange - sample request - internal to internal client

Image
  Ensure you have keycloak token-exchange feature turned on docker run --name mykeycloak -v c:\work\keycloak\conf:/opt/keycloak/conf -p 8080:8080 -e KEYCLOAK_ADMIN=admin -e KC_METRICS_ENABLED=true -e KC_HEALTH_ENABLED=true -e KC_FEATURES="token-exchange, admin-fine-grained-authz" -e KEYCLOAK_ADMIN_PASSWORD=admin quay.io/keycloak/keycloak:24.0.3 start-dev --cache-config-file=cache.xml Simply using token-exchange might not be adequate. To get started with token exchange, we need to set up 2 internal clients.  1. client to generate the token - Client will be called "selfservice_bff_unprivledge". This is the source of the token. This client is just a confidential client.  2. Client that will allow token-exchange to happen. You need to configure this client here - to say I am going to allow token exchange here with client "selfservice_bff_unprivledge". Create a confidential client called selfservice_bff_privledge" To setup token exchange, follow these step