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

__typename field on Input types #31

Open
lorado opened this issue Sep 26, 2018 · 4 comments
Open

__typename field on Input types #31

lorado opened this issue Sep 26, 2018 · 4 comments

Comments

@lorado
Copy link
Contributor

lorado commented Sep 26, 2018

Hi, I just installed a plugin on my PhpStorm: JS GraphQL. It has a great support for working with graphql. When I parsed my schema with this plugin, it threw me an error:

Error: Name "__typename" must not begin with "__", which is reserved by GraphQL introspection.

So my question - Is there any sense in appending __typename field of type "string" to all input types for definitions? I didn't see this in other gql schemas before.

Can we get rid of it? I also didn't find any example for creation Input types with __typename field.

@cmizzi
Copy link
Contributor

cmizzi commented Sep 27, 2018

We used (before) GraphiQL for debugging purpose and the schema was broken due to union types (not supported by GraphiQL). So, we moved to insomnia and no problem with schema parsing.

I've got to look at some documentation/docs about __typename field on Input. Thanks for feedback!

@cmizzi
Copy link
Contributor

cmizzi commented Sep 28, 2018

GraphQL spec. tells that __typename is only used for Object, Interface, or Union.

Next, you're error is not really clear. typename should be called as __typename (into query or objects). Can you try an other plugins/softwares to try the schema introspection like insomnia ?

sudo snap install insomnia

and try the following GraphQL query ?

  query IntrospectionQuery {
    __schema {
      queryType { name }
      mutationType { name }
      subscriptionType { name }
      types {
        ...FullType
      }
      directives {
        name
        description
        locations
        args {
          ...InputValue
        }
      }
    }
  }

  fragment FullType on __Type {
    kind
    name
    description
    fields(includeDeprecated: true) {
      name
      description
      args {
        ...InputValue
      }
      type {
        ...TypeRef
      }
      isDeprecated
      deprecationReason
    }
    inputFields {
      ...InputValue
    }
    interfaces {
      ...TypeRef
    }
    enumValues(includeDeprecated: true) {
      name
      description
      isDeprecated
      deprecationReason
    }
    possibleTypes {
      ...TypeRef
    }
  }

  fragment InputValue on __InputValue {
    name
    description
    type { ...TypeRef }
    defaultValue
  }

  fragment TypeRef on __Type {
    kind
    name
    ofType {
      kind
      name
      ofType {
        kind
        name
        ofType {
          kind
          name
          ofType {
            kind
            name
            ofType {
              kind
              name
              ofType {
                kind
                name
                ofType {
                  kind
                  name
                }
              }
            }
          }
        }
      }
    }
  }

@lorado
Copy link
Contributor Author

lorado commented Sep 28, 2018

Sorry, I didn't notice, that markdown parsed __ away, I edited the error, displayed by plugin.

I guess you misunderstood me =) So I'll try to describe what is going on.

When I create a Definition, then this lib generates much types, among other things also Input types. E.g. I have a definition for a user table. With an empty array in getMutable(), UserInput type looks like this: (this is a part of an output to your query above)

{
    "kind": "INPUT_OBJECT",
    "name": "UserInput",
    "description": null,
    "fields": null,
    "inputFields": [
        {
            "name": "id",
            "description": null,
            "type": {
                "kind": "SCALAR",
                "name": "ID",
                "ofType": null
            },
            "defaultValue": null
        },
        {
            "name": "__typename",
            "description": null,
            "type": {
                "kind": "SCALAR",
                "name": "String",
                "ofType": null
            },
            "defaultValue": null
        }
    ],
    "interfaces": null,
    "enumValues": null,
    "possibleTypes": null
}

So clearly for user mutation (user or users - so for creation of users), I can set following fields in input: id and __typename. The JS GraphQL Plugin on WebStorm reads the defined schema, and thinks: "What the heck, why do you want to set __typename in input, this is reserved by GraphQL" and don't work properly. When I removed __typename on input types, everything works like a charm.

Also for me looks this __typename on input pretty weird - I think no one will ever set this field in input... When you creates objects for you predefined table, you define only stuff for the DB. And there is no __typename. I guess even if you define something in __typename on input - it has no effect at all, or am I wrong?

Also here is how I understand docs. "It returns the name of the object type currently being queried." In docs is mentioned, that __typename is used on querying, and actually you can't query an input. Also: "This field is implicit and does not appear in the fields list in any defined type.". So even if you try introspect defined queried types, in my example User type (see below), there is no __typename field listed.

{
  "data": {
    "__type": {
      "kind": "OBJECT",
      "name": "User",
      "description": "Represents a User",
      "fields": [
        {
          "name": "id",
          "description": null,
          "args": [],
          "type": {
            "kind": "SCALAR",
            "name": "ID",
            "ofType": null
          },
          "isDeprecated": false,
          "deprecationReason": null
        },
        {
          "name": "email",
          "description": null,
          "args": [],
          "type": {
            "kind": "SCALAR",
            "name": "String",
            "ofType": null
          },
          "isDeprecated": false,
          "deprecationReason": null
        },
        {
          "name": "created_at",
          "description": null,
          "args": [],
          "type": {
            "kind": "SCALAR",
            "name": "Datetime",
            "ofType": null
          },
          "isDeprecated": false,
          "deprecationReason": null
        },
        {
          "name": "updated_at",
          "description": null,
          "args": [],
          "type": {
            "kind": "SCALAR",
            "name": "Datetime",
            "ofType": null
          },
          "isDeprecated": false,
          "deprecationReason": null
        }
      ],
      "inputFields": null,
      "interfaces": [],
      "enumValues": null,
      "possibleTypes": null
    }
  }
}

Summarised: I think, that __typename should not be explicitly defined in input Types, so it should be removed. It would solve compatibility problem with JS GraphQL plugin on PhpStorm, and also follow docs, at least how I understood it.

@cmizzi
Copy link
Contributor

cmizzi commented Oct 1, 2018

Ok. __typename is used to resolve morphTo relationship. We should probably not use GraphQL reserved words to do that. I'll check that and remove it from InputObject.

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