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

Wrong GraphQL Type for optional fields #6

Closed
aequi42 opened this issue Dec 26, 2018 · 1 comment
Closed

Wrong GraphQL Type for optional fields #6

aequi42 opened this issue Dec 26, 2018 · 1 comment

Comments

@aequi42
Copy link
Contributor

aequi42 commented Dec 26, 2018

Hi,

I'm new to cockpit and I've encountered a strange behaviour regarding "optional fields".
Currently i have a model in Cockpit, which involves Fields that might be empty. The API then responds with an empty string, regardles of the field type (In my case the fields are of the type set, the fields object in the response contains the correct definitions, but the actual items contain an empty string).

{
  "fields": {
    "Name": {
      "name": "Name",
      "type": "text",
      "localize": false,
      "options": []
    },
    "Doors": {
      "name": "Doors",
      "type": "set",
      "localize": false,
      "options": {
        "fields": [
          {
            "name": "Date",
            "type": "date"
          },
          {
            "name": "Time",
            "type": "time"
          }
        ]
      }
    } 
  },
  "entries": [
    {
      "Name": "With Value",
      "Doors": {
        "Date": "2018-05-27",
        "Time": "18:30"
      }
    },
    {
      "Name": "Without Value",
      "Doors": ""
    }
  ]
}

These strange empty Strings confuse the schema-generation of gatsby and result in an GqlType String for Doors, which in turn isn't queryable correctly.
The empty strings are returned for all kind of fields. In my model I also have the types image, markdown and repeater that behave the same way.

I'm wondering, if theres a way to tell Cockpit to return null instead of empty strings or to tell the plugin to ignore those empty strings.

Cheers
Flo

--- edit ---
Looking through the sources of the plugin i came across the createCollectionItem method, which checks if the provided field is an array with values or null before transforming it.

if (
      !(
        Array.isArray(collectionEntry[collectionFieldName]) &&
        collectionEntry[collectionFieldName].length === 0
      ) &&
      collectionEntry[collectionFieldName] != null
    )

This leads me to the conclusion that there might a way to tell cockpit to return more appropiate values for "empty" fields.

If there's no way to configure the return value in cockpit I'd be happy to create a PR which extends this condition to include a check for empty strings (or provide a extension point to inject some middleware into this transformation)

@WhippetsAintDogs
Copy link
Collaborator

WhippetsAintDogs commented Jan 2, 2019

Hi @aequi42, you're right, it would be feasible with a custom Cockpit plugin, but I would prefer that this project remains compatible with any vanilla Cockpit setups as long as there is no other means to deal with a problem. So, for now, you could extend the condition :)

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