How to generate C# types from GraphQL schema
Asked Answered
T

2

7

i am new for GraphQL and i am trying to convert the graphql schema into c# classes GraphQL:- input Element{ id: Int name: String! } C#:-

 class Element 
{ 
    public int id {get;set;} 
    public string name {get;set;} 
}

Is this possible. if yes then how, because manually creating the class is not solution in my case because i have big schema. also special paste is not working in visual studio. Thanks

Tutto answered 18/4, 2019 at 12:41 Comment(2)
Have you looked at the Github issues/solutions on this?Cuyler
If you have an xml schema the download from msdn xsd.exe which will create classes from schema.Reisch
C
2

With "Strawberry Shake" you can generate a complete C# client out of a schema and a query: https://chillicream.com/docs/strawberryshake/#introduction

Your question will be easier to answer when you give a few more details about your usecase. (e.g.: do your queries change at runtime? How much & how accurate do you expect the types to be?)

I can already spot a mistake in your translation: id is nullable in the schema. And you can only leverage the graphql schema safety when you include the queries in the generator. (Imagine you use that class with a query like query { id } - the name won't come back but it is even non-nullable in the schema!)

Chickenlivered answered 14/5, 2021 at 11:35 Comment(0)
T
0

Maybe GraphQlClientGenerator could help: https://github.com/Husqvik/GraphQlClientGenerator/tree/master. it is a simple console app that generates C# GraphQL query builder and data classes for simple, compiler checked, usage of a GraphQL API.

Tawana answered 25/4, 2024 at 14:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.