Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for OneOf input type #161

Closed
gao-artur opened this issue Aug 10, 2024 · 13 comments
Closed

Add support for OneOf input type #161

gao-artur opened this issue Aug 10, 2024 · 13 comments

Comments

@gao-artur
Copy link
Contributor

gao-artur commented Aug 10, 2024

This is a request to add support for OneOf Input Objects. Note that even though this is still in draft, many graphql servers already implement this feature, and it will be merged without further changes. Implementing this feature requires adding the isOneOf field under the FullType fragment in the introspection query.

Simple Sdl schema for example (note the @oneOf on UserSearchCriteriaInput):

schema {
  query: MyQuery
}

type MyQuery {
  user(searchCriteria: UserSearchCriteriaInput): User
}

type User {
  username: String
  email: String
}

input UserSearchCriteriaInput @oneOf {
  username: String
  email: String
}
Introspection result

Note the "isOneOf": true under UserSearchCriteriaInput type

{
"data": {
  "__schema": {
    "description": null,
    "queryType": {
      "name": "MyQuery"
    },
    "mutationType": null,
    "subscriptionType": null,
    "types": [
      {
        "kind": "OBJECT",
        "name": "__Schema",
        "description": "A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations.",
        "fields": [
          {
            "name": "description",
            "description": null,
            "args": [],
            "type": {
              "kind": "SCALAR",
              "name": "String",
              "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "types",
            "description": "A list of all types supported by this server.",
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "LIST",
                "name": null,
                "ofType": {
                  "kind": "NON_NULL",
                  "name": null,
                  "ofType": {
                    "kind": "OBJECT",
                    "name": "__Type",
                    "ofType": null
                  }
                }
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "queryType",
            "description": "The type that query operations will be rooted at.",
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "OBJECT",
                "name": "__Type",
                "ofType": null
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "mutationType",
            "description": "If this server supports mutation, the type that mutation operations will be rooted at.",
            "args": [],
            "type": {
              "kind": "OBJECT",
              "name": "__Type",
              "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "subscriptionType",
            "description": "If this server supports subscription, the type that subscription operations will be rooted at.",
            "args": [],
            "type": {
              "kind": "OBJECT",
              "name": "__Type",
              "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "directives",
            "description": "A list of all directives supported by this server.",
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "LIST",
                "name": null,
                "ofType": {
                  "kind": "NON_NULL",
                  "name": null,
                  "ofType": {
                    "kind": "OBJECT",
                    "name": "__Directive",
                    "ofType": null
                  }
                }
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          }
        ],
        "inputFields": null,
        "interfaces": [],
        "enumValues": null,
        "possibleTypes": null,
        "isOneOf": null
      },
      {
        "kind": "SCALAR",
        "name": "String",
        "description": null,
        "fields": null,
        "inputFields": null,
        "interfaces": null,
        "enumValues": null,
        "possibleTypes": null,
        "isOneOf": null
      },
      {
        "kind": "OBJECT",
        "name": "__Type",
        "description": "The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the `__TypeKind` enum.\r\n\r\nDepending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name and description, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types.",
        "fields": [
          {
            "name": "kind",
            "description": null,
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "ENUM",
                "name": "__TypeKind",
                "ofType": null
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "name",
            "description": null,
            "args": [],
            "type": {
              "kind": "SCALAR",
              "name": "String",
              "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "description",
            "description": null,
            "args": [],
            "type": {
              "kind": "SCALAR",
              "name": "String",
              "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "fields",
            "description": null,
            "args": [
              {
                "name": "includeDeprecated",
                "description": null,
                "type": {
                  "kind": "SCALAR",
                  "name": "Boolean",
                  "ofType": null
                },
                "defaultValue": "false"
              }
            ],
            "type": {
              "kind": "LIST",
              "name": null,
              "ofType": {
                "kind": "NON_NULL",
                "name": null,
                "ofType": {
                  "kind": "OBJECT",
                  "name": "__Field",
                  "ofType": null
                }
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "interfaces",
            "description": null,
            "args": [],
            "type": {
              "kind": "LIST",
              "name": null,
              "ofType": {
                "kind": "NON_NULL",
                "name": null,
                "ofType": {
                  "kind": "OBJECT",
                  "name": "__Type",
                  "ofType": null
                }
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "possibleTypes",
            "description": null,
            "args": [],
            "type": {
              "kind": "LIST",
              "name": null,
              "ofType": {
                "kind": "NON_NULL",
                "name": null,
                "ofType": {
                  "kind": "OBJECT",
                  "name": "__Type",
                  "ofType": null
                }
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "enumValues",
            "description": null,
            "args": [
              {
                "name": "includeDeprecated",
                "description": null,
                "type": {
                  "kind": "SCALAR",
                  "name": "Boolean",
                  "ofType": null
                },
                "defaultValue": "false"
              }
            ],
            "type": {
              "kind": "LIST",
              "name": null,
              "ofType": {
                "kind": "NON_NULL",
                "name": null,
                "ofType": {
                  "kind": "OBJECT",
                  "name": "__EnumValue",
                  "ofType": null
                }
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "inputFields",
            "description": null,
            "args": [],
            "type": {
              "kind": "LIST",
              "name": null,
              "ofType": {
                "kind": "NON_NULL",
                "name": null,
                "ofType": {
                  "kind": "OBJECT",
                  "name": "__InputValue",
                  "ofType": null
                }
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "ofType",
            "description": null,
            "args": [],
            "type": {
              "kind": "OBJECT",
              "name": "__Type",
              "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "isOneOf",
            "description": null,
            "args": [],
            "type": {
              "kind": "SCALAR",
              "name": "Boolean",
              "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          }
        ],
        "inputFields": null,
        "interfaces": [],
        "enumValues": null,
        "possibleTypes": null,
        "isOneOf": null
      },
      {
        "kind": "ENUM",
        "name": "__TypeKind",
        "description": "An enum describing what kind of type a given __Type is.",
        "fields": null,
        "inputFields": null,
        "interfaces": null,
        "enumValues": [
          {
            "name": "SCALAR",
            "description": "Indicates this type is a scalar.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "OBJECT",
            "description": "Indicates this type is an object. `fields` and `possibleTypes` are valid fields.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "INTERFACE",
            "description": "Indicates this type is an interface. `fields` and `possibleTypes` are valid fields.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "UNION",
            "description": "Indicates this type is a union. `possibleTypes` is a valid field.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "ENUM",
            "description": "Indicates this type is an enum. `enumValues` is a valid field.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "INPUT_OBJECT",
            "description": "Indicates this type is an input object. `inputFields` is a valid field.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "LIST",
            "description": "Indicates this type is a list. `ofType` is a valid field.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "NON_NULL",
            "description": "Indicates this type is a non-null. `ofType` is a valid field.",
            "isDeprecated": false,
            "deprecationReason": null
          }
        ],
        "possibleTypes": null,
        "isOneOf": null
      },
      {
        "kind": "OBJECT",
        "name": "__Field",
        "description": "Object and Interface types are described by a list of Fields, each of which has a name, potentially a list of arguments, and a return type.",
        "fields": [
          {
            "name": "name",
            "description": null,
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "SCALAR",
                "name": "String",
                "ofType": null
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "description",
            "description": null,
            "args": [],
            "type": {
              "kind": "SCALAR",
              "name": "String",
              "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "args",
            "description": null,
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "LIST",
                "name": null,
                "ofType": {
                  "kind": "NON_NULL",
                  "name": null,
                  "ofType": {
                    "kind": "OBJECT",
                    "name": "__InputValue",
                    "ofType": null
                  }
                }
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "type",
            "description": null,
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "OBJECT",
                "name": "__Type",
                "ofType": null
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "isDeprecated",
            "description": null,
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "SCALAR",
                "name": "Boolean",
                "ofType": null
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "deprecationReason",
            "description": null,
            "args": [],
            "type": {
              "kind": "SCALAR",
              "name": "String",
              "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          }
        ],
        "inputFields": null,
        "interfaces": [],
        "enumValues": null,
        "possibleTypes": null,
        "isOneOf": null
      },
      {
        "kind": "OBJECT",
        "name": "__InputValue",
        "description": "Arguments provided to Fields or Directives and the input fields of an InputObject are represented as Input Values which describe their type and optionally a default value.",
        "fields": [
          {
            "name": "name",
            "description": null,
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "SCALAR",
                "name": "String",
                "ofType": null
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "description",
            "description": null,
            "args": [],
            "type": {
              "kind": "SCALAR",
              "name": "String",
              "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "type",
            "description": null,
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "OBJECT",
                "name": "__Type",
                "ofType": null
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "defaultValue",
            "description": "A GraphQL-formatted string representing the default value for this input value.",
            "args": [],
            "type": {
              "kind": "SCALAR",
              "name": "String",
              "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          }
        ],
        "inputFields": null,
        "interfaces": [],
        "enumValues": null,
        "possibleTypes": null,
        "isOneOf": null
      },
      {
        "kind": "SCALAR",
        "name": "Boolean",
        "description": null,
        "fields": null,
        "inputFields": null,
        "interfaces": null,
        "enumValues": null,
        "possibleTypes": null,
        "isOneOf": null
      },
      {
        "kind": "OBJECT",
        "name": "__EnumValue",
        "description": "One possible value for a given Enum. Enum values are unique values, not a placeholder for a string or numeric value. However an Enum value is returned in a JSON response as a string.",
        "fields": [
          {
            "name": "name",
            "description": null,
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "SCALAR",
                "name": "String",
                "ofType": null
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "description",
            "description": null,
            "args": [],
            "type": {
              "kind": "SCALAR",
              "name": "String",
              "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "isDeprecated",
            "description": null,
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "SCALAR",
                "name": "Boolean",
                "ofType": null
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "deprecationReason",
            "description": null,
            "args": [],
            "type": {
              "kind": "SCALAR",
              "name": "String",
              "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          }
        ],
        "inputFields": null,
        "interfaces": [],
        "enumValues": null,
        "possibleTypes": null,
        "isOneOf": null
      },
      {
        "kind": "OBJECT",
        "name": "__Directive",
        "description": "A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document.\r\n\r\nIn some cases, you need to provide options to alter GraphQL's execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor.",
        "fields": [
          {
            "name": "name",
            "description": null,
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "SCALAR",
                "name": "String",
                "ofType": null
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "description",
            "description": null,
            "args": [],
            "type": {
              "kind": "SCALAR",
              "name": "String",
              "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "locations",
            "description": null,
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "LIST",
                "name": null,
                "ofType": {
                  "kind": "NON_NULL",
                  "name": null,
                  "ofType": {
                    "kind": "ENUM",
                    "name": "__DirectiveLocation",
                    "ofType": null
                  }
                }
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "args",
            "description": null,
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "LIST",
                "name": null,
                "ofType": {
                  "kind": "NON_NULL",
                  "name": null,
                  "ofType": {
                    "kind": "OBJECT",
                    "name": "__InputValue",
                    "ofType": null
                  }
                }
              }
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "onOperation",
            "description": null,
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "SCALAR",
                "name": "Boolean",
                "ofType": null
              }
            },
            "isDeprecated": true,
            "deprecationReason": "Use 'locations'."
          },
          {
            "name": "onFragment",
            "description": null,
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "SCALAR",
                "name": "Boolean",
                "ofType": null
              }
            },
            "isDeprecated": true,
            "deprecationReason": "Use 'locations'."
          },
          {
            "name": "onField",
            "description": null,
            "args": [],
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "SCALAR",
                "name": "Boolean",
                "ofType": null
              }
            },
            "isDeprecated": true,
            "deprecationReason": "Use 'locations'."
          }
        ],
        "inputFields": null,
        "interfaces": [],
        "enumValues": null,
        "possibleTypes": null,
        "isOneOf": null
      },
      {
        "kind": "ENUM",
        "name": "__DirectiveLocation",
        "description": "A Directive can be adjacent to many parts of the GraphQL language, a __DirectiveLocation describes one such possible adjacencies.",
        "fields": null,
        "inputFields": null,
        "interfaces": null,
        "enumValues": [
          {
            "name": "QUERY",
            "description": "Location adjacent to a query operation.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "MUTATION",
            "description": "Location adjacent to a mutation operation.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "SUBSCRIPTION",
            "description": "Location adjacent to a subscription operation.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "FIELD",
            "description": "Location adjacent to a field.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "FRAGMENT_DEFINITION",
            "description": "Location adjacent to a fragment definition.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "FRAGMENT_SPREAD",
            "description": "Location adjacent to a fragment spread.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "INLINE_FRAGMENT",
            "description": "Location adjacent to an inline fragment.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "VARIABLE_DEFINITION",
            "description": "Location adjacent to a variable definition.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "SCHEMA",
            "description": "Location adjacent to a schema definition.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "SCALAR",
            "description": "Location adjacent to a scalar definition.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "OBJECT",
            "description": "Location adjacent to an object type definition.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "FIELD_DEFINITION",
            "description": "Location adjacent to a field definition.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "ARGUMENT_DEFINITION",
            "description": "Location adjacent to an argument definition.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "INTERFACE",
            "description": "Location adjacent to an interface definition.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "UNION",
            "description": "Location adjacent to a union definition.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "ENUM",
            "description": "Location adjacent to an enum definition",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "ENUM_VALUE",
            "description": "Location adjacent to an enum value definition",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "INPUT_OBJECT",
            "description": "Location adjacent to an input object type definition.",
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "INPUT_FIELD_DEFINITION",
            "description": "Location adjacent to an input object field definition.",
            "isDeprecated": false,
            "deprecationReason": null
          }
        ],
        "possibleTypes": null,
        "isOneOf": null
      },
      {
        "kind": "OBJECT",
        "name": "MyQuery",
        "description": null,
        "fields": [
          {
            "name": "user",
            "description": null,
            "args": [
              {
                "name": "searchCriteria",
                "description": null,
                "type": {
                  "kind": "INPUT_OBJECT",
                  "name": "UserSearchCriteriaInput",
                  "ofType": null
                },
                "defaultValue": null
              }
            ],
            "type": {
              "kind": "OBJECT",
              "name": "User",
              "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          }
        ],
        "inputFields": null,
        "interfaces": [],
        "enumValues": null,
        "possibleTypes": null,
        "isOneOf": null
      },
      {
        "kind": "OBJECT",
        "name": "User",
        "description": null,
        "fields": [
          {
            "name": "username",
            "description": null,
            "args": [],
            "type": {
              "kind": "SCALAR",
              "name": "String",
              "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          {
            "name": "email",
            "description": null,
            "args": [],
            "type": {
              "kind": "SCALAR",
              "name": "String",
              "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          }
        ],
        "inputFields": null,
        "interfaces": [],
        "enumValues": null,
        "possibleTypes": null,
        "isOneOf": null
      },
      {
        "kind": "INPUT_OBJECT",
        "name": "UserSearchCriteriaInput",
        "description": null,
        "fields": null,
        "inputFields": [
          {
            "name": "username",
            "description": null,
            "type": {
              "kind": "SCALAR",
              "name": "String",
              "ofType": null
            },
            "defaultValue": null
          },
          {
            "name": "email",
            "description": null,
            "type": {
              "kind": "SCALAR",
              "name": "String",
              "ofType": null
            },
            "defaultValue": null
          }
        ],
        "interfaces": null,
        "enumValues": null,
        "possibleTypes": null,
        "isOneOf": true
      }
    ],
    "directives": [
      {
        "name": "include",
        "description": "Directs the executor to include this field or fragment only when the 'if' argument is true.",
        "locations": [
          "FIELD",
          "FRAGMENT_SPREAD",
          "INLINE_FRAGMENT"
        ],
        "args": [
          {
            "name": "if",
            "description": "Included when true.",
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "SCALAR",
                "name": "Boolean",
                "ofType": null
              }
            },
            "defaultValue": null
          }
        ]
      },
      {
        "name": "skip",
        "description": "Directs the executor to skip this field or fragment when the 'if' argument is true.",
        "locations": [
          "FIELD",
          "FRAGMENT_SPREAD",
          "INLINE_FRAGMENT"
        ],
        "args": [
          {
            "name": "if",
            "description": "Skipped when true.",
            "type": {
              "kind": "NON_NULL",
              "name": null,
              "ofType": {
                "kind": "SCALAR",
                "name": "Boolean",
                "ofType": null
              }
            },
            "defaultValue": null
          }
        ]
      },
      {
        "name": "deprecated",
        "description": "Marks an element of a GraphQL schema as no longer supported.",
        "locations": [
          "FIELD_DEFINITION",
          "ENUM_VALUE"
        ],
        "args": [
          {
            "name": "reason",
            "description": "Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted in [Markdown](https://daringfireball.net/projects/markdown/).",
            "type": {
              "kind": "SCALAR",
              "name": "String",
              "ofType": null
            },
            "defaultValue": "\"No longer supported\""
          }
        ]
      }
    ]
  }
}

What's the expected result?

I expect the generator to create a type that can be instantiated by providing one and only one of the possible fields. For example, using static factory methods:

public class UserSearchCriteriaInput
{
    private UserSearchCriteriaInput()
    {
    }

    public string Username { get; private set; }

    public string Email { get; private set; }

    public static UserSearchCriteriaInput FromUsername(string username)
        => new() { Username = username };

    public static UserSearchCriteriaInput FromEmail(string email)
        => new() { Email = email };
}
@gao-artur
Copy link
Contributor Author

Note that querying the isOneOf field from introspection will break users with older servers. Of course, implementing this feature can be postponed to a later time when the new spec is released, but after adding it, users with older servers will be stuck with an old version of the tool until their server is upgraded. I'd suggest (and would appreciate it) that you add a way to select the server spec version and/or pass a flag to select supported draft features.

Husqvik added a commit that referenced this issue Aug 17, 2024
@Husqvik
Copy link
Owner

Husqvik commented Aug 17, 2024

Added some new extension points that help to achieve what you need:

class OneOfDirectiveGenerationContext(GraphQlSchema schema, TextWriter writer, GeneratedObjectType objectTypes = GeneratedObjectType.All, byte indentationSize = 0)
    : SingleFileGenerationContext(schema, writer, objectTypes, indentationSize)
{
    private readonly string _indentation = GraphQlGenerator.GetIndentation(indentationSize);

    public override void OnDataClassConstructorGeneration(ObjectGenerationContext context)
    {
        if (!IsOneOfInput(context.GraphQlType))
            return;
        
        Writer.WriteLine($"{_indentation}    private {context.CSharpTypeName}()");
        Writer.WriteLine($"{_indentation}    {{");
        Writer.WriteLine($"{_indentation}    }}");
        Writer.WriteLine();
    }

    public override void BeforeDataPropertyGeneration(PropertyGenerationContext context) =>
        context.SetterAccessibility =
            IsOneOfInput(context.ObjectContext.GraphQlType)
                ? PropertyAccessibility.Private
                : PropertyAccessibility.Public;

    public override void AfterDataPropertyGeneration(PropertyGenerationContext context)
    {
        if (!IsOneOfInput(context.ObjectContext.GraphQlType))
            return;

        var parameterName = $"{Char.ToLowerInvariant(context.PropertyName[0])}{context.PropertyName[1..]}";
        Writer.WriteLine($"{_indentation}    public static {context.ObjectContext.CSharpTypeName} From{context.PropertyName}({context.PropertyCSharpTypeName} {parameterName})");
        Writer.WriteLine($"{_indentation}        => new() {{ {context.PropertyName} = {parameterName} }};");
        Writer.WriteLine();
    }

    private static bool IsOneOfInput(GraphQlType graphQlType)
    {
        // TODO: use graphQlType.Extensions to determine if the type is annotaded with @oneOf
    }
}

var generator = new GraphQlGenerator(configuration);
var builder = new StringBuilder();
using var writer = new StringWriter(builder);
generator.Generate(new OneOfDirectiveGenerationContext(schema, writer, GeneratedObjectType.All, 0));

@gao-artur
Copy link
Contributor Author

Hey @Husqvik, sorry for the late response. Will try it next week. Thank you!

@gao-artur
Copy link
Contributor Author

Hey @Husqvik. I'm trying to sync your repo with our private fork to try out the solution, but I encountered a couple of problems:

  1. The trinary condition at the end seems to be reversed
    ? ScalarFieldTypeDescription.FromNetTypeName($"{context.GetFullyQualifiedNetTypeName(NamingHelper.ToPascalCase(unwrappedType.Name), unwrappedType.Kind)}{(isTypeNotNull ? "?" : null)}")

    It should be this instead
isTypeNotNull ? null : "?" 

Otherwise, it generates the wrong enum nullability in QueryBuilderParameter<>

  1. We have multiple union types that have common graph type definitions. The generator prints these warnings
WARNING: duplicate union "DereferenceValue" possible type "User"
WARNING: duplicate union "DereferenceValue" possible type "UsersGroup"
WARNING: duplicate union "Identity" possible type "User"
WARNING: duplicate union "Identity" possible type "UsersGroup"
WARNING: duplicate union "NotificationSubscriptionIdentity" possible type "User"
WARNING: duplicate union "NotificationSubscriptionIdentity" possible type "UsersGroup"

Also, the generated types don't implement all the union interfaces. See what we had with our private implementation and what we have with the latest code

- public class User : IAssignee, IDereferenceValue, IIdentity, INotificationSubscriptionIdentity
+ public class User : IAssignee

- public class UsersGroup : IAssignee, IDereferenceValue, IIdentity, INotificationSubscriptionIdentity
+ public class UsersGroup : IAssignee
  1. Not really an issue, just a comment: adding // ReSharper disable InconsistentNaming is not required when there is // <auto-generated> comment in the header. ReSharper won't analyze generated code.

@Husqvik
Copy link
Owner

Husqvik commented Aug 31, 2024

duplicate union just indicates a duplicate in the JSON metadata. It's not an issue I just wasn't sure if that is a bug in the GQL server or schema or something else.

@gao-artur
Copy link
Contributor Author

This is not a duplicate but a legal use case. Consider these union definitions

union Assignee = User | UsersGroup
union NotificationSubscriptionIdentity = User | UsersGroup | Department

Both User and UserGroup are part of these unions. So I expect both User and UserGroup to implement IAssignee and INotificationSubscriptionIdentity. Removing this duplicate check produced the expected result

.Where(
t =>
{
if (duplicateCheck.Add(t.Name))
return true;
Warn($"duplicate union \"{u.Name}\" possible type \"{t.Name}\"");
return false;
})

@Husqvik
Copy link
Owner

Husqvik commented Aug 31, 2024

Ok fixed that one too. I encountered a real duplicate within single UNION possible types from some server.

@gao-artur
Copy link
Contributor Author

Yeah, that works too. Thanks!

Husqvik added a commit that referenced this issue Aug 31, 2024
@Husqvik
Copy link
Owner

Husqvik commented Aug 31, 2024

Is there anything left open on this issue?

@gao-artur
Copy link
Contributor Author

I didn't test the extension points yet, sorry. Will test it tomorrow and reply.

@gao-artur
Copy link
Contributor Author

I checked the extension points. Unfortunately, to use OneOfDirectiveGenerationContext, I need to replace the SingleFileGenerationContext everywhere in GraphQlGenerator or duplicate the code from the WriteFullClientCSharpFile. Ideally, I should be able to alter only code in GraphQlClientGenerator.Console project without making any changes in GraphQlClientGenerator.

Husqvik added a commit that referenced this issue Sep 1, 2024
@Husqvik
Copy link
Owner

Husqvik commented Sep 1, 2024

Moved the generation part from WriteFullClientCSharpFile into SingleFileGenerationContext so it gets automatically executed when inherited.

Husqvik added a commit that referenced this issue Sep 1, 2024
@gao-artur
Copy link
Contributor Author

gao-artur commented Sep 1, 2024

That did the job! Thanks!
Here is the updated OneOfDirectiveGenerationContext. Note the Indentation property and IsOneOfInput implementation

public class OneOfDirectiveGenerationContext(GraphQlSchema schema, TextWriter writer, string @namespace, GeneratedObjectType objectTypes = GeneratedObjectType.All)
    : SingleFileGenerationContext(schema, writer, @namespace, objectTypes)
{
    private string Indentation => GraphQlGenerator.GetIndentation(IndentationSize);

    public override void OnDataClassConstructorGeneration(ObjectGenerationContext context)
    {
        if (!IsOneOfInput(context.GraphQlType))
            return;

        Writer.WriteLine($"{Indentation}    private {context.CSharpTypeName}()");
        Writer.WriteLine($"{Indentation}    {{");
        Writer.WriteLine($"{Indentation}    }}");
        Writer.WriteLine();
    }

    public override void BeforeDataPropertyGeneration(PropertyGenerationContext context) =>
        context.SetterAccessibility =
            IsOneOfInput(context.ObjectContext.GraphQlType)
                ? PropertyAccessibility.Private
                : PropertyAccessibility.Public;

    public override void AfterDataPropertyGeneration(PropertyGenerationContext context)
    {
        if (!IsOneOfInput(context.ObjectContext.GraphQlType))
            return;

        var parameterName = $"{char.ToLowerInvariant(context.PropertyName[0])}{context.PropertyName[1..]}";
        Writer.WriteLine($"{Indentation}    public static {context.ObjectContext.CSharpTypeName} From{context.PropertyName}({context.PropertyCSharpTypeName} {parameterName})");
        Writer.WriteLine($"{Indentation}        => new() {{ {context.PropertyName} = {parameterName} }};");
        Writer.WriteLine();
    }

    private static bool IsOneOfInput(GraphQlType graphQlType)
    {
        return graphQlType.Extensions.TryGetValue("isOneOf", out var isOneOf)
               && isOneOf is true;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants