How to generate c# types from graphql schema?

Member

by mac , in category: Javascript , 6 months ago

How to generate c# types from graphql schema?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , 6 months ago

@mac 

There are several ways to generate C# types from a GraphQL schema. One popular tool for accomplishing this task is the GraphQL Code Generator.


Here is how you can generate C# types from a GraphQL schema using the GraphQL Code Generator:

  1. Install the GraphQL Code Generator package in your project:
1
npm install @graphql-codegen/cli @graphql-codegen/typescript @graphql-codegen/typescript-operations @graphql-codegen/typescript-react-apollo --save-dev


  1. Create a codegen.yml file in the root of your project with the following configuration:
1
2
3
4
5
6
7
8
schema: http://localhost:4000/graphql
documents: null
generates:
  ./src/types.ts:
    plugins:
      - typescript
      - typescript-operations
      - typescript-react-apollo


  1. Run the GraphQL Code Generator to generate the C# types:
1
npx graphql-codegen


The generated C# types will be saved in the types.ts file located in the src directory of your project.


Alternatively, you can also use tools like GraphQL Genie or GraphQL to C# types to generate C# types from a GraphQL schema.