--- lang: ja-jp breaks: true --- # GraphQL `graphql-code-generator` で C#のコードを生成する 2022-08-07 [dotansimha](https://github.com/dotansimha)/**[graphql-code-generator](https://github.com/dotansimha/graphql-code-generator)** [Home – GraphQL Code Generator (graphql-code-generator.com)](https://www.graphql-code-generator.com/#live-demo) > C# Operations > [Installation – GraphQL Code Generator (graphql-code-generator.com)](https://www.graphql-code-generator.com/plugins/c-sharp/c-sharp-operations) ## 使用するスキーマ > パブリックスキーマ > [https://docs.github.com/ja/graphql/overview/public-schema](https://docs.github.com/ja/graphql/overview/public-schema) > https://docs.github.com/public/schema.docs.graphql ## インストール ```bash= npm install graphql npm install -D @graphql-codegen/cli ``` ```bash= npx graphql-code-generator init ``` [@graphql-codegen/c-sharp-operations - npm (npmjs.com)](https://www.npmjs.com/package/@graphql-codegen/c-sharp-operations) ```bash= npm install -D @graphql-codegen/c-sharp-operations ``` [@graphql-codegen/c-sharp - npm (npmjs.com)](https://www.npmjs.com/package/@graphql-codegen/c-sharp) ```bash= npm install -D @graphql-codegen/c-sharp ``` ## ウイザードに従ってプロジェクトを設定する ```bash= npx graphql-code-generator init ``` ## サンプルのスキーマ ### schema.graphql ```graphql= scalar Date schema { query: Query } type Query { me: User! user(id: ID!): User allUsers: [User] search(term: String!): [SearchResult!]! myChats: [Chat!]! } enum Role { USER ADMIN } interface Node { id: ID! } union SearchResult = User | Chat | ChatMessage type User implements Node { id: ID! username: String! email: String! role: Role! } type Chat implements Node { id: ID! users: [User!]! messages: [ChatMessage!]! } type ChatMessage implements Node { id: ID! content: String! time: Date! user: User! } ``` ## 設定ファイル ### codegen.yml ```yml= overwrite: true schema: schema.graphql documents: null generates: src/generated/graphql.cs: plugins: - c-sharp src/generated/graphql_operations.cs: plugins: - "c-sharp-operations" ./graphql.schema.json: plugins: - "introspection" ``` ## 出力されたソースファイル ### graphql.cs ```csharp= using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Newtonsoft.Json; namespace GraphQLCodeGen { public class Types { #region Chat public class Chat : Node { #region members [JsonProperty("id")] public string id { get; set; } [JsonProperty("messages")] public List<ChatMessage> messages { get; set; } [JsonProperty("users")] public List<User> users { get; set; } #endregion } #endregion #region ChatMessage public class ChatMessage : Node { #region members [JsonProperty("content")] public string content { get; set; } [JsonProperty("id")] public string id { get; set; } [JsonProperty("time")] public DateTime time { get; set; } [JsonProperty("user")] public User user { get; set; } #endregion } #endregion public interface Node { [JsonProperty("id")] string id { get; set; } } #region Query public class Query { #region members [JsonProperty("allUsers")] public List<User> allUsers { get; set; } [JsonProperty("me")] public User me { get; set; } [JsonProperty("myChats")] public List<Chat> myChats { get; set; } [JsonProperty("search")] public List<SearchResult> search { get; set; } [JsonProperty("user")] public User user { get; set; } #endregion } #endregion public enum Role { ADMIN, USER } #region User public class User : Node { #region members [JsonProperty("email")] public string email { get; set; } [JsonProperty("id")] public string id { get; set; } [JsonProperty("role")] public Role role { get; set; } [JsonProperty("username")] public string username { get; set; } #endregion } #endregion } } ``` :::warning `union SearchResult = User | Chat | ChatMessage`部分のソースが生成されておらず、ビルドエラーとなる。 > [\[c-sharp\] Add an implementation for GQL Union Types and Interfaces · Issue #6858 · dotansimha/graphql-code-generator (github.com)](https://github.com/dotansimha/graphql-code-generator/issues/6858) > [Add an implementation for C# composition types by cicciodm · Pull Request #6862 · dotansimha/graphql-code-generator (github.com)](https://github.com/dotansimha/graphql-code-generator/pull/6862) いろいろと頑張ってくれているようだがいまだにマージされず。。。 ::: ### graphql_operations.cs ```csharp= using System; using Newtonsoft.Json; using GraphQL; using GraphQL.Client.Abstractions; namespace GraphQLCodeGen { } ``` ## `@graphql-codegen/c-sharp` パッケージの設定 ```ts= /** * @default false * @description Emit C# 9.0+ records instead of classes * * @exampleMarkdown * ```yaml * generates: * src/main/c-sharp/my-org/my-app/Types.cs: * plugins: * - c-sharp * config: * emitRecords: true * ``` */ emitRecords?: boolean; ``` ```ts= /** * @default Newtonsoft.Json * @description Library that should be used to emit JSON attributes. Ignored when `emitJsonAttributes` is `false` or not specified * * @exampleMarkdown * ```yaml * generates: * src/main/c-sharp/my-org/my-app/Types.cs: * plugins: * - c-sharp * config: * jsonAttributesSource: System.Text.Json * ``` */ jsonAttributesSource?: JsonAttributesSource; ``` ```ts= /** * @description Allows you to override the type that unknown scalars will have. * @default any * * @exampleMarkdown * ```yaml {2} * config: * defaultScalarType: unknown * ``` */ defaultScalarType?: string; ``` ```ts= /** * @description Extends or overrides the built-in scalars and custom GraphQL scalars to a custom type. * * @exampleMarkdown * ```yaml * config: * scalars: * DateTime: Date * JSON: "{ [key: string]: any }" * ``` */ scalars?: ScalarsMap; ``` ###### tags: `GraphQL` `graphql-code-generator` `C#` `Node.js` `@graphql-codegen/c-sharp`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up