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

Resolvers for input object types on mutations? #652

Closed
ngbrown opened this issue Feb 22, 2018 · 4 comments
Closed

Resolvers for input object types on mutations? #652

ngbrown opened this issue Feb 22, 2018 · 4 comments

Comments

@ngbrown
Copy link

ngbrown commented Feb 22, 2018

I have a mutation that takes an input object.

  type Mutation {
    updateEntry(input: UpdateEntryInput!): CommandResult
  }

  input UpdateEntryInput {
    clientMutationId: String
    entryId: ID!
    newVersion: EntryChanges!
  }

  input EntryChanges {
    #...
  }

The Ids like entryId in the GraphQL system are Base64 obfuscated strings and I need to convert them back to integers to talk to the back-end services. EntryChanges is another input object with even more Ids that I need to convert back to integers.

I tried to add a resolver for UpdateEntryInput, Apollo Server crashed:

...\node_modules\graphql-tools\dist\schemaGenerator.js:297
                throw new SchemaError(typeName + " was defined in resolvers, but it's not an object");
                ^

Error: UpdateEntryInput was defined in resolvers, but it's not an object

What is wrong? I didn't find any good examples on how to handle this.

@negezor
Copy link

negezor commented Apr 28, 2018

I join the question. This is very useful if you could transform the result from the input and perform the additionally validation of the data. In addition, it will be possible to simplify the validation code in the mutation itself.

@stubailo
Copy link
Contributor

Yeah, unfortunately right now there is no way to process input types before they get passed to the resolver. That would probably be out of scope for being built into this library until it's added to the upstream graphql-js implementation this is based on.

It would take a lot of work, but I bet you could implement something like this using schema directives: https://dev-blog.apollodata.com/reusable-graphql-schema-directives-131fb3a177d1

@arizonatribe
Copy link

Yeah, too much work. But there were at least two suggestions on this thread which lean on custom directives instead.

@alexstrat
Copy link
Contributor

For anyone interested, I implemented the idea of a resolver (called coercer) for input object in graphql-field-arguments-coercion. It can be used with graphql-tools to install the coercer functions and process the inputs before being passed to the field resolver.

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

5 participants