OneGraph / graphiql-explorer

Explorer plugin for GraphiQL
MIT License
907 stars 97 forks source link

Custom argument inputs #31

Open baohouse opened 4 years ago

baohouse commented 4 years ago

Due to my company's deadline being fairly short, I had to fork graphiql-explorer in order to implement custom argument input and check it into our codebase. I'd prefer to stick with the official plugin instead maintaining it internally.

One of the problem with using GraphQL enums is that you don't necessarily get a human-readable enum. Or we have fields that are String, but we want to have them select from a list. So it would look something like this (assetTypes):

I do this by exposing a getScalarArgInput prop, and pass in a function that feels similar to getDefaultScalarArgValue.

export function getScalarArgInput(parentField, arg, argType, argValue, onChange, styleConfig) {
  const unwrappedType = unwrapOutputType(parentField.type);
  if (
    unwrappedType.name === 'AccountBalance' &&
    arg.name === 'assetTypes' &&
    argType.name === 'String'
  ) {
    return (
      <select
        onChange={onChange}
        style={{ color: styleConfig.colors.builtin }}
        value={argValue ? argValue.value : undefined}
      >
        {_.map(FinancialAssetType, ({ code, description }) => (
          <option key={code} value={code}>
            {description}
          </option>
        ))}
      </select>
    );
  }
  return;
}

Wondering if this would be useful to check in.

FluorescentHallucinogen commented 4 years ago

PTAL at #47. :wink: