Vincit / objection-graphql

GraphQL schema generator for objection.js
MIT License
307 stars 35 forks source link

Support `GraphQLInputObject` as argument #35

Open vjpr opened 6 years ago

vjpr commented 6 years ago

Seems like objection-graphql cannot handle objects as custom arguments...

{ Error: objection-graphql cannot handle argument value {"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"name","loc":{"start":28,"end":32}},"value":{"kind":"StringValue","value":"containerId","loc":{"start":33,"end":46}},"loc":{"start":28,"end":46}},{"kind":"ObjectField","name":{"kind":"Name","value":"value","loc":{"start":48,"end":53}},"value":{"kind":"StringValue","value":"asd","loc":{"start":54,"end":59}},"loc":{"start":48,"end":59}}],"loc":{"start":27,"end":60}}
    at SchemaBuilder._argValue (xxx\node_modules\.github.com\Vincit\objection-graphql\69e09be0192a19ae52585847b4e032641eec0675\node_modules\objection-graphql\lib\SchemaBuilder.js:370:13)
    at SchemaBuilder._filterForArgs (xxx\node_modules\.github.com\Vincit\objection-graphql\69e09be0192a19ae52585847b4e032641eec0675\node_modules\objection-graphql\lib\SchemaBuilder.js:346:26)

This is set in the custom argument builder:

args.fieldEq = {
    type: ParamInput,
    query: (query, value) => {
        query.whereRaw("data->>'"+value.name+"' = '" + value.value + "'")
    },
}
const ParamInput = new GraphQLInputObjectType({
    name: 'ParamInput',
    fields: {
        name: {type: GraphQLString},
        value: {type: GraphQLString},
    },
})

Error source

_argValue(value, variables) {
    if (value.kind === KIND_VARIABLE) {
      return variables[value.name.value];
    } else if ('value' in value) {
      return value.value;
    } else if (Array.isArray(value.values)) {
      return value.values.map(value => this._argValue(value, variables));
    } else {
      throw new Error(`objection-graphql cannot handle argument value ${JSON.stringify(value)}`);
    }
  }

@denizunal3