Search code examples
asp.net-coregraphqlstrawberryshake

How to utilize StrawberryShake tools for schema located in github


I am trying to add strawberry shake to a project for my first time using GraphQL in .NET and I am struggling to get the tooling to work to build the client for strawberry shake to work. The code I am trying to use is:

dotnet graphql init https://github.com/Enterprise-CMCS/managed-care-review/blob/main/services/app-graphql/src/schema.graphql TestClient

I thought the point of this command was to pull the schema but unfortunately I keep receiving a 403 forbidden error in the terminal despite this is a public repo and should not even require a token. In examples I have seen they do not reference the schema file but instead the endpoint (ie, blahblah.com/graphql) so maybe I should not be referencing the file?


Solution

  • This question has gone unanswered for quite some time. I suspect the OP has found a workaround or a solution, but for others that might find themselves here from a search engine.

    In this case, where you are not generating against a running server, you can manually download the schema and generate from that local file.

    This command:

    curl -o DownloadedSchema/ManagedCareSchema.graphql https://raw.githubusercontent.com/Enterprise-CMCS/managed-care-review/refs/heads/main/services/app-graphql/src/schema.graphql
    

    will download the schema from the original post.

    Then you can run:

    dotnet graphql init -f DownloadedSchema/ManagedCareSchema.graphql -n ManagedCareGraphQlClient -p ./ManagedCareClient
    

    This command uses the downloaded file, specified by the -f argument to initialize a client named ManagedCareGraphQlClient in the project in the folder ./ManagedCareClient.

    The complete sequence of commands to create a solution on macOS or Unix would look like this:

    # Generate the solution with a class library and console app.
    dotnet new tool-manifest
    dotnet tool install StrawberryShake.Tools --local
    dotnet new sln -n ManagedCareClient
    dotnet new classlib --name ManagedCareClient
    dotnet add ManagedCareClient package StrawberryShake.Server
    dotnet sln add ./ManagedCareClient
    dotnet new console --name ManagedCareClient.Cli
    dotnet sln add ./ManagedCareClient.Cli
    
    # Downloading schema
    
    mkdir DownloadedSchema
    curl -o DownloadedSchema/ManagedCareSchema.graphql https://raw.githubusercontent.com/Enterprise-CMCS/managed-care-review/refs/heads/main/services/app-graphql/src/schema.graphql
    
    # Fix typo in schema
    
    sed -i 's/"\\/./g' DownloadedSchema/ManagedCareSchema.graphql
    
    # Initialize the GraphQL client
    
    dotnet graphql init -f DownloadedSchema/ManagedCareSchema.graphql -n ManagedCareGraphQlClient -p ./ManagedCareClient
    
    # Build the solution
    
    dotnet build
    

    The sed command is there to remove what I suspect is a typo in the schema mentioned in the original post; it would not normally be needed.

    It should be straightforward to adapt this to Windows cmd or PowerShell